Search in sources :

Example 6 with PrimitiveOperation

use of io.atomix.primitive.operation.PrimitiveOperation 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)

Example 7 with PrimitiveOperation

use of io.atomix.primitive.operation.PrimitiveOperation in project atomix by atomix.

the class RaftProxyInvokerTest method testResequenceQuery.

/**
 * Tests resequencing a query response.
 */
@Test
public void testResequenceQuery() throws Throwable {
    CompletableFuture<QueryResponse> future1 = new CompletableFuture<>();
    CompletableFuture<QueryResponse> future2 = new CompletableFuture<>();
    RaftProxyConnection connection = mock(RaftProxyConnection.class);
    Mockito.when(connection.query(any(QueryRequest.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(mock(RaftProxyConnection.class), connection, state, new RaftProxySequencer(state), manager, threadContext);
    CompletableFuture<byte[]> result1 = submitter.invoke(new PrimitiveOperation(QUERY, HeapBytes.EMPTY));
    CompletableFuture<byte[]> result2 = submitter.invoke(new PrimitiveOperation(QUERY, HeapBytes.EMPTY));
    future2.complete(QueryResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(10).withResult("Hello world again!".getBytes()).build());
    assertEquals(state.getResponseIndex(), 1);
    assertFalse(result1.isDone());
    assertFalse(result2.isDone());
    future1.complete(QueryResponse.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.getResponseIndex(), 10);
}
Also used : PrimitiveOperation(io.atomix.primitive.operation.PrimitiveOperation) ThreadContext(io.atomix.utils.concurrent.ThreadContext) CompletableFuture(java.util.concurrent.CompletableFuture) QueryResponse(io.atomix.protocols.raft.protocol.QueryResponse) Test(org.junit.Test)

Aggregations

PrimitiveOperation (io.atomix.primitive.operation.PrimitiveOperation)7 ThreadContext (io.atomix.utils.concurrent.ThreadContext)7 Test (org.junit.Test)7 CompletableFuture (java.util.concurrent.CompletableFuture)5 RaftException (io.atomix.protocols.raft.RaftException)3 QueryResponse (io.atomix.protocols.raft.protocol.QueryResponse)3 CommandRequest (io.atomix.protocols.raft.protocol.CommandRequest)2 CommandResponse (io.atomix.protocols.raft.protocol.CommandResponse)2 QueryRequest (io.atomix.protocols.raft.protocol.QueryRequest)2