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 MultiDestinationCastTest method shouldManuallyAddPortDuringActiveStream.
@Test
@InterruptAfter(10)
void shouldManuallyAddPortDuringActiveStream() throws InterruptedException {
final int numMessagesToSend = MESSAGES_PER_TERM * 3;
final int numMessageForSub2 = 10;
final CountingFragmentHandler fragmentHandlerA = new CountingFragmentHandler("fragmentHandlerA");
final CountingFragmentHandler fragmentHandlerB = new CountingFragmentHandler("fragmentHandlerB");
final Supplier<String> messageSupplierA = fragmentHandlerA::toString;
final Supplier<String> messageSupplierB = fragmentHandlerB::toString;
final CountDownLatch availableImage = new CountDownLatch(1);
final MutableLong position = new MutableLong(0);
final MutableInteger messagesSent = new MutableInteger(0);
final Supplier<String> positionSupplier = () -> "Failed to publish, position: " + position + ", sent: " + messagesSent;
launch(Tests::onError);
subscriptionA = clientA.addSubscription(SUB1_MDC_MANUAL_URI, STREAM_ID);
subscriptionB = clientB.addSubscription(SUB2_MDC_MANUAL_URI, STREAM_ID, (image) -> availableImage.countDown(), null);
publication = clientA.addPublication(PUB_MDC_MANUAL_URI, STREAM_ID);
publication.addDestination(SUB1_MDC_MANUAL_URI);
Tests.awaitConnected(subscriptionA);
while (messagesSent.value < numMessagesToSend) {
position.value = publication.offer(buffer, 0, MESSAGE_LENGTH);
if (0 <= position.value) {
messagesSent.increment();
} else {
Tests.yieldingIdle(positionSupplier);
}
subscriptionA.poll(fragmentHandlerA, FRAGMENT_LIMIT);
if (messagesSent.value > (numMessagesToSend - numMessageForSub2)) {
subscriptionB.poll(fragmentHandlerB, FRAGMENT_LIMIT);
}
if (messagesSent.value == (numMessagesToSend - numMessageForSub2)) {
final int published = messagesSent.value;
// then B will receive more than the expected `numMessageForSub2`.
while (fragmentHandlerA.notDone(published)) {
if (subscriptionA.poll(fragmentHandlerA, FRAGMENT_LIMIT) <= 0) {
Tests.yieldingIdle(messageSupplierA);
}
}
publication.addDestination(SUB2_MDC_MANUAL_URI);
availableImage.await();
}
}
while (fragmentHandlerA.notDone(numMessagesToSend) || fragmentHandlerB.notDone(numMessageForSub2)) {
if (fragmentHandlerA.notDone(numMessagesToSend) && subscriptionA.poll(fragmentHandlerA, FRAGMENT_LIMIT) <= 0) {
Tests.yieldingIdle(messageSupplierA);
}
if (fragmentHandlerB.notDone(numMessageForSub2) && subscriptionB.poll(fragmentHandlerB, FRAGMENT_LIMIT) <= 0) {
Tests.yieldingIdle(messageSupplierB);
}
}
}
use of org.agrona.collections.MutableLong in project Aeron by real-logic.
the class ReplayedBasicSubscriber method findLatestRecording.
private static long findLatestRecording(final AeronArchive archive) {
final MutableLong lastRecordingId = new MutableLong();
final RecordingDescriptorConsumer consumer = (controlSessionId, correlationId, recordingId, startTimestamp, stopTimestamp, startPosition, stopPosition, initialTermId, segmentFileLength, termBufferLength, mtuLength, sessionId, streamId, strippedChannel, originalChannel, sourceIdentity) -> lastRecordingId.set(recordingId);
final long fromRecordingId = 0L;
final int recordCount = 100;
final int foundCount = archive.listRecordingsForUri(fromRecordingId, recordCount, CHANNEL, STREAM_ID, consumer);
if (foundCount == 0) {
throw new IllegalStateException("no recordings found");
}
return lastRecordingId.get();
}
use of org.agrona.collections.MutableLong in project aeron by real-logic.
the class AuthenticationTest method shouldRejectOnChallengeResponse.
@Test
@InterruptAfter(10)
public void shouldRejectOnChallengeResponse() {
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 challengeRespondedTo = 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));
challengeRespondedTo = 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 (challengeRespondedTo) {
assertEquals(sessionProxy.sessionId(), authenticatorSessionId.value);
sessionProxy.reject();
}
}
});
launchClusteredMediaDriver(() -> authenticator);
launchService(serviceSessionId, encodedPrincipal, serviceMsgCounter);
try {
connectClient(credentialsSupplier);
} catch (final AuthenticationException ex) {
assertEquals(-1L, serviceSessionId.value);
ClusterTests.failOnClusterError();
return;
}
fail("should have seen exception");
}
use of org.agrona.collections.MutableLong in project aeron by real-logic.
the class AuthenticationTest method shouldAuthenticateOnConnectRequestWithEmptyCredentials.
@Test
@InterruptAfter(10)
public void shouldAuthenticateOnConnectRequestWithEmptyCredentials() {
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) {
fail();
return null;
}
});
final Authenticator authenticator = spy(new Authenticator() {
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) {
fail();
}
public void onConnectedSession(final SessionProxy sessionProxy, final long nowMs) {
assertEquals(sessionProxy.sessionId(), authenticatorSessionId.value);
sessionProxy.authenticate(null);
}
public void onChallengedSession(final SessionProxy sessionProxy, final long nowMs) {
fail();
}
});
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(0, encodedPrincipal.get().length);
ClusterTests.failOnClusterError();
}
Aggregations