refactor: add ut

This commit is contained in:
chuntaojun 2020-06-07 19:25:10 +08:00
parent 5191838d77
commit d916d221bf
2 changed files with 70 additions and 6 deletions

View File

@ -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) {

View File

@ -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<Response> 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());
}
}