Search in sources :

Example 6 with CommandResponse

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CommandResponse(io.atomix.protocols.raft.protocol.CommandResponse) PublishRequest(io.atomix.protocols.raft.protocol.PublishRequest) Test(org.junit.Test)

Example 7 with CommandResponse

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueryResponse(io.atomix.protocols.raft.protocol.QueryResponse) CommandResponse(io.atomix.protocols.raft.protocol.CommandResponse) Test(org.junit.Test)

Example 8 with CommandResponse

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);
}
Also used : PrimitiveOperation(io.atomix.primitive.operation.PrimitiveOperation) ThreadContext(io.atomix.utils.concurrent.ThreadContext) CommandResponse(io.atomix.protocols.raft.protocol.CommandResponse) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test)

Aggregations

CommandResponse (io.atomix.protocols.raft.protocol.CommandResponse)8 Test (org.junit.Test)7 PublishRequest (io.atomix.protocols.raft.protocol.PublishRequest)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 CompletableFuture (java.util.concurrent.CompletableFuture)3 PrimitiveOperation (io.atomix.primitive.operation.PrimitiveOperation)2 ThreadContext (io.atomix.utils.concurrent.ThreadContext)2 RaftException (io.atomix.protocols.raft.RaftException)1 OperationResult (io.atomix.protocols.raft.impl.OperationResult)1 PendingCommand (io.atomix.protocols.raft.impl.PendingCommand)1 CommandRequest (io.atomix.protocols.raft.protocol.CommandRequest)1 QueryResponse (io.atomix.protocols.raft.protocol.QueryResponse)1 RaftSession (io.atomix.protocols.raft.session.RaftSession)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1