use of io.atomix.protocols.raft.protocol.CommandResponse in project atomix by atomix.
the class RaftProxySequencerTest method testSequenceEventAtCommand.
/**
* Tests sequencing an event that arrives before a command response.
*/
@Test
public void testSequenceEventAtCommand() throws Throwable {
RaftProxySequencer sequencer = new RaftProxySequencer(new RaftProxyState("test", SessionId.from(1), UUID.randomUUID().toString(), new TestPrimitiveType(), 1000));
long sequence = sequencer.nextRequest();
PublishRequest request = PublishRequest.builder().withSession(1).withEventIndex(2).withPreviousIndex(0).withEvents(Collections.emptyList()).build();
CommandResponse response = CommandResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(2).withEventIndex(2).build();
AtomicInteger run = new AtomicInteger();
sequencer.sequenceResponse(sequence, response, () -> assertEquals(run.getAndIncrement(), 1));
sequencer.sequenceEvent(request, () -> assertEquals(run.getAndIncrement(), 0));
assertEquals(run.get(), 2);
}
use of io.atomix.protocols.raft.protocol.CommandResponse in project atomix by atomix.
the class RaftProxySequencerTest method testSequenceResponses.
/**
* Tests sequencing callbacks with the sequencer.
*/
@Test
public void testSequenceResponses() throws Throwable {
RaftProxySequencer sequencer = new RaftProxySequencer(new RaftProxyState("test", SessionId.from(1), UUID.randomUUID().toString(), new TestPrimitiveType(), 1000));
long sequence1 = sequencer.nextRequest();
long sequence2 = sequencer.nextRequest();
assertTrue(sequence2 == sequence1 + 1);
CommandResponse commandResponse = CommandResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(2).withEventIndex(0).build();
QueryResponse queryResponse = QueryResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(2).withEventIndex(0).build();
AtomicBoolean run = new AtomicBoolean();
sequencer.sequenceResponse(sequence2, queryResponse, () -> run.set(true));
sequencer.sequenceResponse(sequence1, commandResponse, () -> assertFalse(run.get()));
assertTrue(run.get());
}
use of io.atomix.protocols.raft.protocol.CommandResponse in project atomix by atomix.
the class RaftProxyInvokerTest method testResequenceCommand.
/**
* Test resequencing a command response.
*/
@Test
public void testResequenceCommand() throws Throwable {
CompletableFuture<CommandResponse> future1 = new CompletableFuture<>();
CompletableFuture<CommandResponse> future2 = new CompletableFuture<>();
RaftProxyConnection connection = mock(RaftProxyConnection.class);
Mockito.when(connection.command(any(CommandRequest.class))).thenReturn(future1).thenReturn(future2);
RaftProxyState state = new RaftProxyState("test", SessionId.from(1), UUID.randomUUID().toString(), new TestPrimitiveType(), 1000);
RaftProxyManager manager = mock(RaftProxyManager.class);
ThreadContext threadContext = new TestContext();
RaftProxyInvoker submitter = new RaftProxyInvoker(connection, mock(RaftProxyConnection.class), state, new RaftProxySequencer(state), manager, threadContext);
CompletableFuture<byte[]> result1 = submitter.invoke(new PrimitiveOperation(COMMAND, HeapBytes.EMPTY));
CompletableFuture<byte[]> result2 = submitter.invoke(new PrimitiveOperation(COMMAND, HeapBytes.EMPTY));
future2.complete(CommandResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(10).withResult("Hello world again!".getBytes()).build());
assertEquals(state.getCommandRequest(), 2);
assertEquals(state.getCommandResponse(), 0);
assertEquals(state.getResponseIndex(), 1);
assertFalse(result1.isDone());
assertFalse(result2.isDone());
future1.complete(CommandResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(9).withResult("Hello world!".getBytes()).build());
assertTrue(result1.isDone());
assertTrue(Arrays.equals(result1.get(), "Hello world!".getBytes()));
assertTrue(result2.isDone());
assertTrue(Arrays.equals(result2.get(), "Hello world again!".getBytes()));
assertEquals(state.getCommandRequest(), 2);
assertEquals(state.getCommandResponse(), 2);
assertEquals(state.getResponseIndex(), 10);
}
Aggregations