Search in sources :

Example 61 with MutableLong

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));
}
Also used : MutableLong(org.agrona.collections.MutableLong) AtomicCounter(org.agrona.concurrent.status.AtomicCounter) ReadableCounter(io.aeron.status.ReadableCounter) TestClusterClock(io.aeron.test.cluster.TestClusterClock) Test(org.junit.jupiter.api.Test)

Example 62 with MutableLong

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);
        }
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ErrorHandler(org.agrona.ErrorHandler) RegistrationException(io.aeron.exceptions.RegistrationException) io.aeron.test(io.aeron.test) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) IoUtil(org.agrona.IoUtil) Supplier(java.util.function.Supplier) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MediaDriver(io.aeron.driver.MediaDriver) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) File(java.io.File) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) Header(io.aeron.logbuffer.Header) MutableLong(org.agrona.collections.MutableLong) ThreadingMode(io.aeron.driver.ThreadingMode) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) FragmentHandler(io.aeron.logbuffer.FragmentHandler) DirectBuffer(org.agrona.DirectBuffer) MutableLong(org.agrona.collections.MutableLong) MutableInteger(org.agrona.collections.MutableInteger) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 63 with MutableLong

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();
}
Also used : MutableLong(org.agrona.collections.MutableLong) Subscription(io.aeron.Subscription) RecordingDescriptorConsumer(io.aeron.archive.client.RecordingDescriptorConsumer) SampleConfiguration(io.aeron.samples.SampleConfiguration) AeronArchive(io.aeron.archive.client.AeronArchive) ChannelUri(io.aeron.ChannelUri) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FragmentHandler(io.aeron.logbuffer.FragmentHandler) SamplesUtil(io.aeron.samples.SamplesUtil) SigInt(org.agrona.concurrent.SigInt) MutableLong(org.agrona.collections.MutableLong) RecordingDescriptorConsumer(io.aeron.archive.client.RecordingDescriptorConsumer)

Example 64 with MutableLong

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");
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) MutableLong(org.agrona.collections.MutableLong) MutableReference(org.agrona.collections.MutableReference) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 65 with MutableLong

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();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) MutableLong(org.agrona.collections.MutableLong) MutableReference(org.agrona.collections.MutableReference) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Aggregations

MutableLong (org.agrona.collections.MutableLong)82 Test (org.junit.jupiter.api.Test)68 InterruptAfter (io.aeron.test.InterruptAfter)44 MutableReference (org.agrona.collections.MutableReference)28 CountersReader (org.agrona.concurrent.status.CountersReader)19 RecordingSignal (io.aeron.archive.codecs.RecordingSignal)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 MediaDriver (io.aeron.driver.MediaDriver)14 FragmentHandler (io.aeron.logbuffer.FragmentHandler)14 DirectBuffer (org.agrona.DirectBuffer)13 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)11 RegistrationException (io.aeron.exceptions.RegistrationException)10 TestMediaDriver (io.aeron.test.driver.TestMediaDriver)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)10 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)10 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)10 AeronArchive (io.aeron.archive.client.AeronArchive)8 Authenticator (io.aeron.security.Authenticator)8 CredentialsSupplier (io.aeron.security.CredentialsSupplier)8