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));
}
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());
}
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());
}
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));
}
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));
}
Aggregations