Search in sources :

Example 6 with ThreadContext

use of io.atomix.utils.concurrent.ThreadContext 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 7 with ThreadContext

use of io.atomix.utils.concurrent.ThreadContext 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)

Example 8 with ThreadContext

use of io.atomix.utils.concurrent.ThreadContext 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 9 with ThreadContext

use of io.atomix.utils.concurrent.ThreadContext 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

ThreadContext (io.atomix.utils.concurrent.ThreadContext)9 PrimitiveOperation (io.atomix.primitive.operation.PrimitiveOperation)7 Test (org.junit.Test)7 CompletableFuture (java.util.concurrent.CompletableFuture)6 RaftException (io.atomix.protocols.raft.RaftException)4 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 Endpoint (io.atomix.messaging.Endpoint)1 PrimitiveProxy (io.atomix.primitive.proxy.PrimitiveProxy)1 OpenSessionRequest (io.atomix.protocols.raft.protocol.OpenSessionRequest)1 Scheduler (io.atomix.utils.concurrent.Scheduler)1 SingleThreadContext (io.atomix.utils.concurrent.SingleThreadContext)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1