Search in sources :

Example 1 with TestClusterClock

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

the class ConsensusModuleAgentTest method shouldCloseInactiveSession.

@Test
public void shouldCloseInactiveSession() {
    final TestClusterClock clock = new TestClusterClock(TimeUnit.MILLISECONDS);
    final long startMs = SLOW_TICK_INTERVAL_MS;
    clock.update(startMs, TimeUnit.MILLISECONDS);
    ctx.epochClock(clock).clusterClock(clock);
    final ConsensusModuleAgent agent = new ConsensusModuleAgent(ctx);
    final long correlationId = 1L;
    agent.state(ConsensusModule.State.ACTIVE);
    agent.role(Cluster.Role.LEADER);
    Tests.setField(agent, "appendPosition", mock(ReadableCounter.class));
    agent.onSessionConnect(correlationId, 2, PROTOCOL_SEMANTIC_VERSION, RESPONSE_CHANNEL_ONE, new byte[0]);
    agent.doWork();
    verify(mockLogPublisher).appendSessionOpen(any(ClusterSession.class), anyLong(), eq(startMs));
    verify(mockTimeConsumer).accept(clock.time());
    final long timeMs = startMs + TimeUnit.NANOSECONDS.toMillis(ConsensusModule.Configuration.sessionTimeoutNs());
    clock.update(timeMs, TimeUnit.MILLISECONDS);
    agent.doWork();
    final long timeoutMs = timeMs + SLOW_TICK_INTERVAL_MS;
    clock.update(timeoutMs, TimeUnit.MILLISECONDS);
    agent.doWork();
    verify(mockTimeConsumer).accept(clock.time());
    verify(mockTimedOutClientCounter).incrementOrdered();
    verify(mockLogPublisher).appendSessionClose(any(ClusterSession.class), anyLong(), eq(timeoutMs));
    verify(mockEgressPublisher).sendEvent(any(ClusterSession.class), anyLong(), anyInt(), eq(EventCode.CLOSED), eq(CloseReason.TIMEOUT.name()));
}
Also used : ReadableCounter(io.aeron.status.ReadableCounter) TestClusterClock(io.aeron.test.cluster.TestClusterClock) Test(org.junit.jupiter.api.Test)

Example 2 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 3 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 4 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 5 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