use of com.alipay.sofa.jraft.core.NodeImpl in project nacos by alibaba.
the class JRaftServerTest method testCommit.
@Test
public void testCommit() throws NoSuchFieldException, IllegalAccessException, TimeoutException, InterruptedException {
WriteRequest.Builder writeRequestBuilder = WriteRequest.newBuilder();
WriteRequest writeRequest = writeRequestBuilder.build();
// No group is set, and make sure that an IllegalArgumentException will be thrown.
CompletableFuture<Response> future = server.commit(groupId, writeRequest, this.future);
verify(future).completeExceptionally(any(IllegalArgumentException.class));
// Set an group.
Collection<RequestProcessor4CP> processors = Collections.singletonList(mockProcessor4CP);
server.createMultiRaftGroup(processors);
Field cliClientServiceField = JRaftServer.class.getDeclaredField("cliClientService");
cliClientServiceField.setAccessible(true);
cliClientServiceField.set(server, cliClientServiceMock);
// Make the node leader and verify the invokeToLeader is never called.
NodeImpl node = (NodeImpl) server.findNodeByGroup(groupId);
Field stateField = NodeImpl.class.getDeclaredField("state");
stateField.setAccessible(true);
stateField.set(node, State.STATE_LEADER);
server.commit(groupId, writeRequest, future);
verify(cliClientServiceMock, never()).getRpcClient();
// make the node follower and verify the invokeToLeader is called.
node = (NodeImpl) server.findNodeByGroup(groupId);
stateField.setAccessible(true);
stateField.set(node, State.STATE_FOLLOWER);
RouteTable.getInstance().updateLeader(groupId, peerId1);
server.commit(groupId, writeRequest, future);
verify(cliClientServiceMock).getRpcClient();
}
use of com.alipay.sofa.jraft.core.NodeImpl in project sofa-jraft by sofastack.
the class JRaftUtils method bootstrap.
/**
* Bootstrap a non-empty raft node.
*
* @param opts options of bootstrap
* @return true if bootstrap success
*/
public static boolean bootstrap(final BootstrapOptions opts) throws InterruptedException {
final NodeImpl node = new NodeImpl();
final boolean ret = node.bootstrap(opts);
node.shutdown();
node.join();
return ret;
}
Aggregations