use of org.agrona.collections.MutableLong in project aeron by real-logic.
the class AuthenticationTest method shouldAuthenticateOnChallengeResponse.
@Test
@InterruptAfter(10)
public void shouldAuthenticateOnChallengeResponse() {
final AtomicLong serviceMsgCounter = new AtomicLong(0L);
final MutableLong serviceSessionId = new MutableLong(-1L);
final MutableLong authenticatorSessionId = new MutableLong(-1L);
final MutableReference<byte[]> encodedPrincipal = new MutableReference<>();
final CredentialsSupplier credentialsSupplier = spy(new CredentialsSupplier() {
public byte[] encodedCredentials() {
return NULL_CREDENTIAL;
}
public byte[] onChallenge(final byte[] encodedChallenge) {
assertEquals(CHALLENGE_STRING, new String(encodedChallenge));
return encodedCredentials;
}
});
final Authenticator authenticator = spy(new Authenticator() {
boolean challengeSuccessful = false;
public void onConnectRequest(final long sessionId, final byte[] encodedCredentials, final long nowMs) {
authenticatorSessionId.value = sessionId;
assertEquals(0, encodedCredentials.length);
}
public void onChallengeResponse(final long sessionId, final byte[] encodedCredentials, final long nowMs) {
assertEquals(sessionId, authenticatorSessionId.value);
assertEquals(CREDENTIALS_STRING, new String(encodedCredentials));
challengeSuccessful = true;
}
public void onConnectedSession(final SessionProxy sessionProxy, final long nowMs) {
assertEquals(sessionProxy.sessionId(), authenticatorSessionId.value);
sessionProxy.challenge(encodedChallenge);
}
public void onChallengedSession(final SessionProxy sessionProxy, final long nowMs) {
if (challengeSuccessful) {
assertEquals(sessionProxy.sessionId(), authenticatorSessionId.value);
sessionProxy.authenticate(PRINCIPAL_STRING.getBytes());
}
}
});
launchClusteredMediaDriver(() -> authenticator);
launchService(serviceSessionId, encodedPrincipal, serviceMsgCounter);
connectClient(credentialsSupplier);
sendCountedMessageIntoCluster(0);
Tests.awaitValue(serviceMsgCounter, 1);
assertEquals(aeronCluster.clusterSessionId(), authenticatorSessionId.value);
assertEquals(aeronCluster.clusterSessionId(), serviceSessionId.value);
assertEquals(PRINCIPAL_STRING, new String(encodedPrincipal.get()));
ClusterTests.failOnClusterError();
}
use of org.agrona.collections.MutableLong 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 org.agrona.collections.MutableLong 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 org.agrona.collections.MutableLong in project aeron by real-logic.
the class TimestampingSystemTest method shouldSupportReceiveTimestampsOnMergedMds.
@Test
@InterruptAfter(10)
void shouldSupportReceiveTimestampsOnMergedMds() {
final MutableDirectBuffer buffer = new UnsafeBuffer(new byte[64]);
try (TestMediaDriver driver = driver();
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(driver.aeronDirectoryName()))) {
final Subscription mdsSub = aeron.addSubscription("aeron:udp?control-mode=manual|channel-rcv-ts-offset=0", 1000);
final Publication pub1 = aeron.addExclusivePublication("aeron:udp?endpoint=localhost:23424", 1000);
final String pub2Uri = new ChannelUriStringBuilder("aeron:udp?endpoint=localhost:23425").initialPosition(0L, pub1.initialTermId(), pub1.termBufferLength()).sessionId(pub1.sessionId()).build();
final Publication pub2 = aeron.addExclusivePublication(pub2Uri, 1000);
mdsSub.addDestination("aeron:udp?endpoint=localhost:23424");
mdsSub.addDestination("aeron:udp?endpoint=localhost:23425");
while (!pub1.isConnected() || !pub2.isConnected()) {
Tests.yieldingIdle("Failed to connect");
}
final MutableLong sendTimestamp = new MutableLong(SENTINEL_VALUE);
buffer.putLong(0, SENTINEL_VALUE);
while (0 > pub1.offer(buffer, 0, buffer.capacity())) {
Tests.yieldingIdle("Failed to offer message");
}
buffer.putLong(0, SENTINEL_VALUE);
while (0 > pub2.offer(buffer, 0, buffer.capacity())) {
Tests.yieldingIdle("Failed to offer message");
}
final FragmentHandler fragmentHandler = (buffer1, offset, length, header) -> sendTimestamp.set(buffer1.getLong(offset));
while (1 > mdsSub.poll(fragmentHandler, 1)) {
Tests.yieldingIdle("Failed to receive message");
}
assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
buffer.putLong(0, SENTINEL_VALUE);
while (0 > pub2.offer(buffer, 0, buffer.capacity())) {
Tests.yieldingIdle("Failed to offer message");
}
buffer.putLong(0, SENTINEL_VALUE);
while (0 > pub1.offer(buffer, 0, buffer.capacity())) {
Tests.yieldingIdle("Failed to offer message");
}
sendTimestamp.set(SENTINEL_VALUE);
while (1 > mdsSub.poll(fragmentHandler, 1)) {
Tests.yieldingIdle("Failed to receive message");
}
assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
}
}
use of org.agrona.collections.MutableLong in project aeron by real-logic.
the class TimestampingSystemTest method shouldSupportChannelSendTimestampsOnMdc.
@Test
@InterruptAfter(10)
void shouldSupportChannelSendTimestampsOnMdc() {
final MutableDirectBuffer buffer = new UnsafeBuffer(new byte[64]);
try (TestMediaDriver driver = driver();
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(driver.aeronDirectoryName()))) {
final Publication mdcPub = aeron.addPublication("aeron:udp?control-mode=manual|channel-snd-ts-offset=0", 1000);
final Subscription sub1 = aeron.addSubscription("aeron:udp?endpoint=localhost:23424", 1000);
final Subscription sub2 = aeron.addSubscription("aeron:udp?endpoint=localhost:23425", 1000);
mdcPub.addDestination("aeron:udp?endpoint=localhost:23424");
mdcPub.addDestination("aeron:udp?endpoint=localhost:23425");
while (!sub1.isConnected() || !sub2.isConnected()) {
Tests.yieldingIdle("Failed to connect");
}
buffer.putLong(0, SENTINEL_VALUE);
while (0 > mdcPub.offer(buffer, 0, buffer.capacity())) {
Tests.yieldingIdle("Failed to offer message");
}
final MutableLong sendTimestamp = new MutableLong(SENTINEL_VALUE);
final FragmentHandler fragmentHandler = (buffer1, offset, length, header) -> sendTimestamp.set(buffer1.getLong(offset));
while (1 > sub1.poll(fragmentHandler, 1)) {
Tests.yieldingIdle("Failed to receive message");
}
assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
while (1 > sub2.poll(fragmentHandler, 1)) {
Tests.yieldingIdle("Failed to receive message");
}
assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
}
}
Aggregations