use of org.agrona.concurrent.CachedEpochClock in project aeron by real-logic.
the class SequencerAgentTest method shouldSuspendThenResume.
@Test
public void shouldSuspendThenResume() {
final CachedEpochClock clock = new CachedEpochClock();
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());
ctx.moduleStateCounter(mockState);
ctx.controlToggleCounter(mockControlToggle);
ctx.epochClock(clock);
final SequencerAgent agent = newSequencerAgent();
agent.commitPositionCounter(mock(Counter.class));
agent.logRecordingPositionCounter(mock(ReadableCounter.class));
assertThat((int) stateValue.get(), is(ConsensusModule.State.INIT.code()));
agent.state(ConsensusModule.State.ACTIVE);
agent.role(Cluster.Role.LEADER);
assertThat((int) stateValue.get(), is(ConsensusModule.State.ACTIVE.code()));
controlValue.value = SUSPEND.code();
clock.update(1);
agent.doWork();
assertThat((int) stateValue.get(), is(ConsensusModule.State.SUSPENDED.code()));
assertThat((int) controlValue.get(), is(NEUTRAL.code()));
controlValue.value = RESUME.code();
clock.update(2);
agent.doWork();
assertThat((int) stateValue.get(), is(ConsensusModule.State.ACTIVE.code()));
assertThat((int) controlValue.get(), is(NEUTRAL.code()));
final InOrder inOrder = Mockito.inOrder(mockLogPublisher);
inOrder.verify(mockLogPublisher).appendClusterAction(eq(ClusterAction.SUSPEND), anyLong(), anyLong(), anyLong());
inOrder.verify(mockLogPublisher).appendClusterAction(eq(ClusterAction.RESUME), anyLong(), anyLong(), anyLong());
}
use of org.agrona.concurrent.CachedEpochClock in project aeron by real-logic.
the class SequencerAgentTest method shouldLimitActiveSessions.
@Test
public void shouldLimitActiveSessions() {
final CachedEpochClock clock = new CachedEpochClock();
ctx.maxConcurrentSessions(1);
ctx.epochClock(clock);
final SequencerAgent agent = newSequencerAgent();
final long correlationIdOne = 1L;
agent.state(ConsensusModule.State.ACTIVE);
agent.role(Cluster.Role.LEADER);
agent.commitPositionCounter(mock(Counter.class));
agent.logRecordingPositionCounter(mock(ReadableCounter.class));
agent.onSessionConnect(correlationIdOne, 2, RESPONSE_CHANNEL_ONE, new byte[0]);
clock.update(1);
agent.doWork();
verify(mockLogPublisher).appendConnectedSession(any(ClusterSession.class), anyLong());
final long correlationIdTwo = 2L;
agent.onSessionConnect(correlationIdTwo, 3, RESPONSE_CHANNEL_TWO, new byte[0]);
clock.update(2);
agent.doWork();
verify(mockEgressPublisher).sendEvent(any(ClusterSession.class), eq(EventCode.ERROR), eq(ConsensusModule.Configuration.SESSION_LIMIT_MSG));
}
use of org.agrona.concurrent.CachedEpochClock in project aeron by real-logic.
the class SequencerAgentTest method shouldCloseInactiveSession.
@Test
public void shouldCloseInactiveSession() {
final CachedEpochClock clock = new CachedEpochClock();
final long startMs = 7L;
clock.update(startMs);
ctx.epochClock(clock);
final SequencerAgent agent = newSequencerAgent();
final long correlationId = 1L;
agent.state(ConsensusModule.State.ACTIVE);
agent.role(Cluster.Role.LEADER);
agent.commitPositionCounter(mock(Counter.class));
agent.logRecordingPositionCounter(mock(ReadableCounter.class));
agent.onSessionConnect(correlationId, 2, RESPONSE_CHANNEL_ONE, new byte[0]);
agent.doWork();
verify(mockLogPublisher).appendConnectedSession(any(ClusterSession.class), eq(startMs));
final long timeMs = startMs + TimeUnit.NANOSECONDS.toMillis(ConsensusModule.Configuration.sessionTimeoutNs());
clock.update(timeMs);
agent.doWork();
final long timeoutMs = timeMs + 1L;
clock.update(timeoutMs);
agent.doWork();
verify(mockLogPublisher).appendClosedSession(any(ClusterSession.class), eq(timeoutMs));
verify(mockEgressPublisher).sendEvent(any(ClusterSession.class), eq(EventCode.ERROR), eq(ConsensusModule.Configuration.SESSION_TIMEOUT_MSG));
}
use of org.agrona.concurrent.CachedEpochClock in project Aeron by real-logic.
the class SenderTest method setUp.
@BeforeEach
public void setUp() {
final SendChannelEndpoint mockSendChannelEndpoint = mock(SendChannelEndpoint.class);
when(mockSendChannelEndpoint.udpChannel()).thenReturn(udpChannel);
when(mockSendChannelEndpoint.send(any())).thenAnswer(saveByteBufferAnswer);
when(mockSystemCounters.get(any())).thenReturn(mock(AtomicCounter.class));
final MediaDriver.Context ctx = new MediaDriver.Context().cachedEpochClock(new CachedEpochClock()).cachedNanoClock(nanoClock).senderCachedNanoClock(nanoClock).receiverCachedNanoClock(nanoClock).controlTransportPoller(mockTransportPoller).systemCounters(mockSystemCounters).senderCommandQueue(senderCommandQueue).nanoClock(nanoClock).errorHandler(errorHandler);
sender = new Sender(ctx);
LogBufferDescriptor.initialiseTailWithTermId(rawLog.metaData(), 0, INITIAL_TERM_ID);
termAppenders = new TermAppender[PARTITION_COUNT];
for (int i = 0; i < PARTITION_COUNT; i++) {
termAppenders[i] = new TermAppender(rawLog.termBuffers()[i], rawLog.metaData(), i);
}
final PublicationParams params = new PublicationParams();
params.entityTag = 101;
params.mtuLength = MAX_FRAME_LENGTH;
params.lingerTimeoutNs = Configuration.publicationLingerTimeoutNs();
params.signalEos = true;
publication = new NetworkPublication(1, ctx, params, mockSendChannelEndpoint, rawLog, Configuration.producerWindowLength(TERM_BUFFER_LENGTH, Configuration.publicationTermWindowLength()), new AtomicLongPosition(), new AtomicLongPosition(), new AtomicLongPosition(), new AtomicLongPosition(), mock(AtomicCounter.class), SESSION_ID, STREAM_ID, INITIAL_TERM_ID, flowControl, mockRetransmitHandler, new NetworkPublicationThreadLocals(), false);
senderCommandQueue.offer(() -> sender.onNewNetworkPublication(publication));
}
use of org.agrona.concurrent.CachedEpochClock in project aeron by real-logic.
the class SenderTest method setUp.
@BeforeEach
public void setUp() {
final SendChannelEndpoint mockSendChannelEndpoint = mock(SendChannelEndpoint.class);
when(mockSendChannelEndpoint.udpChannel()).thenReturn(udpChannel);
when(mockSendChannelEndpoint.send(any())).thenAnswer(saveByteBufferAnswer);
when(mockSystemCounters.get(any())).thenReturn(mock(AtomicCounter.class));
final MediaDriver.Context ctx = new MediaDriver.Context().cachedEpochClock(new CachedEpochClock()).cachedNanoClock(nanoClock).senderCachedNanoClock(nanoClock).receiverCachedNanoClock(nanoClock).controlTransportPoller(mockTransportPoller).systemCounters(mockSystemCounters).senderCommandQueue(senderCommandQueue).nanoClock(nanoClock).errorHandler(errorHandler);
sender = new Sender(ctx);
LogBufferDescriptor.initialiseTailWithTermId(rawLog.metaData(), 0, INITIAL_TERM_ID);
termAppenders = new TermAppender[PARTITION_COUNT];
for (int i = 0; i < PARTITION_COUNT; i++) {
termAppenders[i] = new TermAppender(rawLog.termBuffers()[i], rawLog.metaData(), i);
}
final PublicationParams params = new PublicationParams();
params.entityTag = 101;
params.mtuLength = MAX_FRAME_LENGTH;
params.lingerTimeoutNs = Configuration.publicationLingerTimeoutNs();
params.signalEos = true;
publication = new NetworkPublication(1, ctx, params, mockSendChannelEndpoint, rawLog, Configuration.producerWindowLength(TERM_BUFFER_LENGTH, Configuration.publicationTermWindowLength()), new AtomicLongPosition(), new AtomicLongPosition(), new AtomicLongPosition(), new AtomicLongPosition(), mock(AtomicCounter.class), SESSION_ID, STREAM_ID, INITIAL_TERM_ID, flowControl, mockRetransmitHandler, new NetworkPublicationThreadLocals(), false);
senderCommandQueue.offer(() -> sender.onNewNetworkPublication(publication));
}
Aggregations