Search in sources :

Example 1 with PrimitiveOperation

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

the class RaftProxyInvokerTest method testExpireSessionOnCommandFailure.

/**
 * Tests that the client's session is expired when an UnknownSessionException is received from the cluster.
 */
@Test
public void testExpireSessionOnCommandFailure() throws Throwable {
    CompletableFuture<CommandResponse> future = new CompletableFuture<>();
    RaftProxyConnection connection = mock(RaftProxyConnection.class);
    Mockito.when(connection.command(any(CommandRequest.class))).thenReturn(future);
    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[]> result = submitter.invoke(new PrimitiveOperation(COMMAND, HeapBytes.EMPTY));
    assertEquals(state.getResponseIndex(), 1);
    assertFalse(result.isDone());
    future.completeExceptionally(new RaftException.UnknownSession("unknown session"));
    assertTrue(result.isCompletedExceptionally());
}
Also used : PrimitiveOperation(io.atomix.primitive.operation.PrimitiveOperation) RaftException(io.atomix.protocols.raft.RaftException) ThreadContext(io.atomix.utils.concurrent.ThreadContext) CommandResponse(io.atomix.protocols.raft.protocol.CommandResponse) CompletableFuture(java.util.concurrent.CompletableFuture) CommandRequest(io.atomix.protocols.raft.protocol.CommandRequest) Test(org.junit.Test)

Example 2 with PrimitiveOperation

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

the class RaftProxyInvokerTest method testSkippingOverFailedQuery.

/**
 * Tests skipping over a failed query attempt.
 */
@Test
public void testSkippingOverFailedQuery() 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));
    assertEquals(state.getResponseIndex(), 1);
    assertFalse(result1.isDone());
    assertFalse(result2.isDone());
    future1.completeExceptionally(new RaftException.QueryFailure("failure"));
    future2.complete(QueryResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(10).withResult("Hello world!".getBytes()).build());
    assertTrue(result1.isCompletedExceptionally());
    assertTrue(result2.isDone());
    assertTrue(Arrays.equals(result2.get(), "Hello world!".getBytes()));
    assertEquals(state.getResponseIndex(), 10);
}
Also used : PrimitiveOperation(io.atomix.primitive.operation.PrimitiveOperation) RaftException(io.atomix.protocols.raft.RaftException) ThreadContext(io.atomix.utils.concurrent.ThreadContext) CompletableFuture(java.util.concurrent.CompletableFuture) QueryResponse(io.atomix.protocols.raft.protocol.QueryResponse) Test(org.junit.Test)

Example 3 with PrimitiveOperation

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

the class RaftProxyInvokerTest method testSubmitQuery.

/**
 * Tests submitting a query to the cluster.
 */
@Test
public void testSubmitQuery() throws Throwable {
    RaftProxyConnection connection = mock(RaftProxyConnection.class);
    when(connection.query(any(QueryRequest.class))).thenReturn(CompletableFuture.completedFuture(QueryResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(10).withResult("Hello world!".getBytes()).build()));
    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);
    assertTrue(Arrays.equals(submitter.invoke(new PrimitiveOperation(QUERY, HeapBytes.EMPTY)).get(), "Hello world!".getBytes()));
    assertEquals(state.getResponseIndex(), 10);
}
Also used : PrimitiveOperation(io.atomix.primitive.operation.PrimitiveOperation) QueryRequest(io.atomix.protocols.raft.protocol.QueryRequest) ThreadContext(io.atomix.utils.concurrent.ThreadContext) Test(org.junit.Test)

Example 4 with PrimitiveOperation

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

the class RaftProxyInvokerTest method testSubmitCommand.

/**
 * Tests submitting a command to the cluster.
 */
@Test
public void testSubmitCommand() throws Throwable {
    RaftProxyConnection connection = mock(RaftProxyConnection.class);
    when(connection.command(any(CommandRequest.class))).thenReturn(CompletableFuture.completedFuture(CommandResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(10).withResult("Hello world!".getBytes()).build()));
    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);
    assertArrayEquals(submitter.invoke(new PrimitiveOperation(COMMAND, HeapBytes.EMPTY)).get(), "Hello world!".getBytes());
    assertEquals(state.getCommandRequest(), 1);
    assertEquals(state.getCommandResponse(), 1);
    assertEquals(state.getResponseIndex(), 10);
}
Also used : PrimitiveOperation(io.atomix.primitive.operation.PrimitiveOperation) CommandRequest(io.atomix.protocols.raft.protocol.CommandRequest) ThreadContext(io.atomix.utils.concurrent.ThreadContext) Test(org.junit.Test)

Example 5 with PrimitiveOperation

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

the class RaftProxyInvokerTest method testExpireSessionOnQueryFailure.

/**
 * Tests that the client's session is expired when an UnknownSessionException is received from the cluster.
 */
@Test
public void testExpireSessionOnQueryFailure() throws Throwable {
    CompletableFuture<QueryResponse> future = new CompletableFuture<>();
    RaftProxyConnection connection = mock(RaftProxyConnection.class);
    Mockito.when(connection.query(any(QueryRequest.class))).thenReturn(future);
    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[]> result = submitter.invoke(new PrimitiveOperation(QUERY, HeapBytes.EMPTY));
    assertEquals(state.getResponseIndex(), 1);
    assertFalse(result.isDone());
    future.completeExceptionally(new RaftException.UnknownSession("unknown session"));
    assertTrue(result.isCompletedExceptionally());
}
Also used : PrimitiveOperation(io.atomix.primitive.operation.PrimitiveOperation) RaftException(io.atomix.protocols.raft.RaftException) QueryRequest(io.atomix.protocols.raft.protocol.QueryRequest) 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