Search in sources :

Example 6 with TestClusterClock

use of io.aeron.test.cluster.TestClusterClock in project aeron by real-logic.

the class ConsensusModuleAgentTest method shouldThrowClusterTerminationExceptionUponShutdown.

@Test
public void shouldThrowClusterTerminationExceptionUponShutdown() {
    final TestClusterClock clock = new TestClusterClock(TimeUnit.MILLISECONDS);
    final CountedErrorHandler countedErrorHandler = mock(CountedErrorHandler.class);
    final MutableLong stateValue = new MutableLong();
    final Counter mockState = mock(Counter.class);
    when(mockState.get()).thenAnswer((invocation) -> stateValue.value);
    doAnswer((invocation) -> {
        stateValue.value = invocation.getArgument(0);
        return null;
    }).when(mockState).set(anyLong());
    ctx.countedErrorHandler(countedErrorHandler).moduleStateCounter(mockState).epochClock(clock).clusterClock(clock);
    final ConsensusModuleAgent agent = new ConsensusModuleAgent(ctx);
    agent.state(ConsensusModule.State.QUITTING);
    assertThrows(ClusterTerminationException.class, () -> agent.onServiceAck(1024, 100, 0, 55, 0));
}
Also used : MutableLong(org.agrona.collections.MutableLong) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) ReadableCounter(io.aeron.status.ReadableCounter) TestClusterClock(io.aeron.test.cluster.TestClusterClock) Test(org.junit.jupiter.api.Test)

Example 7 with TestClusterClock

use of io.aeron.test.cluster.TestClusterClock in project aeron by real-logic.

the class ConsensusModuleAgentTest method shouldGenerateRoleNameWhenNotSet.

@Test
public void shouldGenerateRoleNameWhenNotSet() {
    final TestClusterClock clock = new TestClusterClock(TimeUnit.MILLISECONDS);
    ctx.epochClock(clock).clusterClock(clock);
    final ConsensusModuleAgent agent = new ConsensusModuleAgent(ctx);
    assertEquals("consensus-module_0_0", agent.roleName());
}
Also used : TestClusterClock(io.aeron.test.cluster.TestClusterClock) Test(org.junit.jupiter.api.Test)

Example 8 with TestClusterClock

use of io.aeron.test.cluster.TestClusterClock in project Aeron by real-logic.

the class ConsensusModuleAgentTest method shouldUseAssignedRoleName.

@Test
public void shouldUseAssignedRoleName() {
    final String expectedRoleName = "test-role-name";
    final TestClusterClock clock = new TestClusterClock(TimeUnit.MILLISECONDS);
    ctx.agentRoleName(expectedRoleName).epochClock(clock).clusterClock(clock);
    final ConsensusModuleAgent agent = new ConsensusModuleAgent(ctx);
    assertEquals(expectedRoleName, agent.roleName());
}
Also used : TestClusterClock(io.aeron.test.cluster.TestClusterClock) Test(org.junit.jupiter.api.Test)

Example 9 with TestClusterClock

use of io.aeron.test.cluster.TestClusterClock in project Aeron by real-logic.

the class ConsensusModuleAgentTest method shouldSuspendThenResume.

@Test
public void shouldSuspendThenResume() {
    final TestClusterClock clock = new TestClusterClock(TimeUnit.MILLISECONDS);
    final MutableLong stateValue = new MutableLong();
    final Counter mockState = mock(Counter.class);
    when(mockState.get()).thenAnswer((invocation) -> stateValue.value);
    doAnswer((invocation) -> {
        stateValue.value = invocation.getArgument(0);
        return null;
    }).when(mockState).set(anyLong());
    final MutableLong controlValue = new MutableLong(NEUTRAL.code());
    final Counter mockControlToggle = mock(Counter.class);
    when(mockControlToggle.get()).thenAnswer((invocation) -> controlValue.value);
    doAnswer((invocation) -> {
        controlValue.value = invocation.getArgument(0);
        return null;
    }).when(mockControlToggle).set(anyLong());
    doAnswer((invocation) -> {
        final long expected = invocation.getArgument(0);
        if (expected == controlValue.value) {
            controlValue.value = invocation.getArgument(1);
            return true;
        }
        return false;
    }).when(mockControlToggle).compareAndSet(anyLong(), anyLong());
    ctx.moduleStateCounter(mockState);
    ctx.controlToggleCounter(mockControlToggle);
    ctx.epochClock(clock).clusterClock(clock);
    final ConsensusModuleAgent agent = new ConsensusModuleAgent(ctx);
    Tests.setField(agent, "appendPosition", mock(ReadableCounter.class));
    assertEquals(ConsensusModule.State.INIT.code(), stateValue.get());
    agent.state(ConsensusModule.State.ACTIVE);
    agent.role(Cluster.Role.LEADER);
    assertEquals(ConsensusModule.State.ACTIVE.code(), stateValue.get());
    SUSPEND.toggle(mockControlToggle);
    clock.update(SLOW_TICK_INTERVAL_MS, TimeUnit.MILLISECONDS);
    agent.doWork();
    assertEquals(ConsensusModule.State.SUSPENDED.code(), stateValue.get());
    assertEquals(SUSPEND.code(), controlValue.get());
    RESUME.toggle(mockControlToggle);
    clock.update(SLOW_TICK_INTERVAL_MS * 2, TimeUnit.MILLISECONDS);
    agent.doWork();
    assertEquals(ConsensusModule.State.ACTIVE.code(), stateValue.get());
    assertEquals(NEUTRAL.code(), controlValue.get());
    final InOrder inOrder = Mockito.inOrder(mockLogPublisher);
    inOrder.verify(mockLogPublisher).appendClusterAction(anyLong(), anyLong(), eq(ClusterAction.SUSPEND));
    inOrder.verify(mockLogPublisher).appendClusterAction(anyLong(), anyLong(), eq(ClusterAction.RESUME));
}
Also used : ReadableCounter(io.aeron.status.ReadableCounter) MutableLong(org.agrona.collections.MutableLong) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) ReadableCounter(io.aeron.status.ReadableCounter) InOrder(org.mockito.InOrder) TestClusterClock(io.aeron.test.cluster.TestClusterClock) Test(org.junit.jupiter.api.Test)

Example 10 with TestClusterClock

use of io.aeron.test.cluster.TestClusterClock in project Aeron by real-logic.

the class ConsensusModuleAgentTest method shouldLimitActiveSessions.

@Test
public void shouldLimitActiveSessions() {
    final TestClusterClock clock = new TestClusterClock(TimeUnit.MILLISECONDS);
    ctx.maxConcurrentSessions(1).epochClock(clock).clusterClock(clock);
    final ConsensusModuleAgent agent = new ConsensusModuleAgent(ctx);
    final long correlationIdOne = 1L;
    agent.state(ConsensusModule.State.ACTIVE);
    agent.role(Cluster.Role.LEADER);
    Tests.setField(agent, "appendPosition", mock(ReadableCounter.class));
    agent.onSessionConnect(correlationIdOne, 2, PROTOCOL_SEMANTIC_VERSION, RESPONSE_CHANNEL_ONE, new byte[0]);
    clock.update(17, TimeUnit.MILLISECONDS);
    agent.doWork();
    verify(mockTimeConsumer).accept(clock.time());
    verify(mockLogPublisher).appendSessionOpen(any(ClusterSession.class), anyLong(), anyLong());
    final long correlationIdTwo = 2L;
    agent.onSessionConnect(correlationIdTwo, 3, PROTOCOL_SEMANTIC_VERSION, RESPONSE_CHANNEL_TWO, new byte[0]);
    clock.update(clock.time() + 10L, TimeUnit.MILLISECONDS);
    agent.doWork();
    verify(mockTimeConsumer).accept(clock.time());
    verify(mockEgressPublisher).sendEvent(any(ClusterSession.class), anyLong(), anyInt(), eq(EventCode.ERROR), eq(SESSION_LIMIT_MSG));
}
Also used : ReadableCounter(io.aeron.status.ReadableCounter) TestClusterClock(io.aeron.test.cluster.TestClusterClock) Test(org.junit.jupiter.api.Test)

Aggregations

TestClusterClock (io.aeron.test.cluster.TestClusterClock)14 Test (org.junit.jupiter.api.Test)14 ReadableCounter (io.aeron.status.ReadableCounter)10 MutableLong (org.agrona.collections.MutableLong)4 AtomicCounter (org.agrona.concurrent.status.AtomicCounter)4 InOrder (org.mockito.InOrder)2