From d916d221bf52e892cbe854b0f60de32068f40886 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sun, 7 Jun 2020 19:25:10 +0800 Subject: [PATCH] refactor: add ut --- .../core/distributed/raft/JRaftServer.java | 17 ++++-- .../raft/processor/AbstractProcessorTest.java | 59 +++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 core/src/test/java/com/alibaba/nacos/core/distributed/raft/processor/AbstractProcessorTest.java diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java index 5d8b619f3..3bb81e676 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.core.distributed.raft; +import com.alibaba.nacos.common.JustForTest; import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.LoggerUtils; @@ -140,7 +141,7 @@ public class JRaftServer { String.valueOf(256 * 1024 * 1024)); } - public JRaftServer() throws Exception { + public JRaftServer() { this.conf = new Configuration(); } @@ -555,12 +556,16 @@ public class JRaftServer { return cliService; } - public class RaftGroupTuple { + public static class RaftGroupTuple { - private final LogProcessor processor; - private final Node node; - private final RaftGroupService raftGroupService; - private final NacosStateMachine machine; + private LogProcessor processor; + private Node node; + private RaftGroupService raftGroupService; + private NacosStateMachine machine; + + @JustForTest + public RaftGroupTuple() { + } public RaftGroupTuple(Node node, LogProcessor processor, RaftGroupService raftGroupService, NacosStateMachine machine) { diff --git a/core/src/test/java/com/alibaba/nacos/core/distributed/raft/processor/AbstractProcessorTest.java b/core/src/test/java/com/alibaba/nacos/core/distributed/raft/processor/AbstractProcessorTest.java new file mode 100644 index 000000000..8ed84b8b2 --- /dev/null +++ b/core/src/test/java/com/alibaba/nacos/core/distributed/raft/processor/AbstractProcessorTest.java @@ -0,0 +1,59 @@ +package com.alibaba.nacos.core.distributed.raft.processor; + +import com.alibaba.nacos.consistency.SerializeFactory; +import com.alibaba.nacos.consistency.entity.Log; +import com.alibaba.nacos.consistency.entity.Response; +import com.alibaba.nacos.core.distributed.raft.JRaftServer; +import com.alibaba.nacos.core.distributed.raft.utils.FailoverClosure; +import com.alipay.sofa.jraft.Node; +import com.alipay.sofa.jraft.Status; +import com.alipay.sofa.jraft.error.RaftError; +import com.alipay.sofa.jraft.rpc.Connection; +import com.alipay.sofa.jraft.rpc.RpcContext; +import com.google.protobuf.Message; +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.atomic.AtomicReference; + +public class AbstractProcessorTest { + + private JRaftServer server = new JRaftServer() { + @Override + public void applyOperation(Node node, Message data, FailoverClosure closure) { + closure.setResponse(Response.newBuilder().setSuccess(false).setErrMsg("Error message transmission").build()); + closure.run(new Status(RaftError.UNKNOWN, "Error message transmission")); + } + }; + + @Test + public void testErrorThroughRPC() { + final AtomicReference reference = new AtomicReference<>(); + + RpcContext context = new RpcContext() { + @Override + public void sendResponse(Object responseObj) { + reference.set((Response) responseObj); + } + + @Override + public Connection getConnection() { + return null; + } + + @Override + public String getRemoteAddress() { + return null; + } + }; + AbstractProcessor processor = new NacosLogProcessor(server, SerializeFactory.getDefault()); + processor.execute(server, context, Log.newBuilder().build(), new JRaftServer.RaftGroupTuple()); + + Response response = reference.get(); + Assert.assertNotNull(response); + + Assert.assertEquals("Error message transmission", response.getErrMsg()); + Assert.assertFalse(response.getSuccess()); + } + +} \ No newline at end of file