Search in sources :

Example 16 with MutableReference

use of org.agrona.collections.MutableReference in project aeron by real-logic.

the class ReplicateRecordingTest method shouldReplicateLiveRecordingAndMerge.

@Test
@InterruptAfter(10)
public void shouldReplicateLiveRecordingAndMerge() {
    final String messagePrefix = "Message-Prefix-";
    final int messageCount = 10;
    final long srcRecordingId;
    final long subscriptionId = srcAeronArchive.startRecording(LIVE_CHANNEL, LIVE_STREAM_ID, LOCAL);
    final MutableReference<RecordingSignal> signalRef = new MutableReference<>();
    final RecordingSignalAdapter adapter;
    try (Publication publication = srcAeron.addPublication(LIVE_CHANNEL, LIVE_STREAM_ID)) {
        final CountersReader srcCounters = srcAeron.countersReader();
        final int counterId = awaitRecordingCounterId(srcCounters, publication.sessionId());
        srcRecordingId = RecordingPos.getRecordingId(srcCounters, counterId);
        offer(publication, messageCount, messagePrefix);
        awaitPosition(srcCounters, counterId, publication.position());
        final MutableLong dstRecordingId = new MutableLong();
        adapter = newRecordingSignalAdapter(signalRef, dstRecordingId);
        dstAeronArchive.replicate(srcRecordingId, NULL_VALUE, SRC_CONTROL_STREAM_ID, SRC_CONTROL_REQUEST_CHANNEL, LIVE_CHANNEL);
        offer(publication, messageCount, messagePrefix);
        assertEquals(RecordingSignal.REPLICATE, awaitSignal(signalRef, adapter));
        assertEquals(RecordingSignal.EXTEND, awaitSignal(signalRef, adapter));
        assertEquals(RecordingSignal.MERGE, awaitSignal(signalRef, adapter));
        final CountersReader dstCounters = dstAeron.countersReader();
        final int dstCounterId = RecordingPos.findCounterIdByRecording(dstCounters, dstRecordingId.get());
        offer(publication, messageCount, messagePrefix);
        awaitPosition(dstCounters, dstCounterId, publication.position());
    }
    srcAeronArchive.stopRecording(subscriptionId);
    assertEquals(RecordingSignal.STOP, awaitSignal(signalRef, adapter));
}
Also used : MutableLong(org.agrona.collections.MutableLong) RecordingSignal(io.aeron.archive.codecs.RecordingSignal) MutableReference(org.agrona.collections.MutableReference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 17 with MutableReference

use of org.agrona.collections.MutableReference in project aeron by real-logic.

the class TwoBufferOfferMessageTest method shouldTransferUnfragmentedTwoPartMessage.

@Test
@InterruptAfter(10)
void shouldTransferUnfragmentedTwoPartMessage() {
    final UnsafeBuffer expectedBuffer = new UnsafeBuffer(new byte[256]);
    final UnsafeBuffer bufferOne = new UnsafeBuffer(expectedBuffer, 0, 32);
    final UnsafeBuffer bufferTwo = new UnsafeBuffer(expectedBuffer, 32, expectedBuffer.capacity() - 32);
    bufferOne.setMemory(0, bufferOne.capacity(), (byte) 'a');
    bufferTwo.setMemory(0, bufferTwo.capacity(), (byte) 'b');
    final String expectedMessage = expectedBuffer.getStringWithoutLengthAscii(0, expectedBuffer.capacity());
    final MutableReference<String> receivedMessage = new MutableReference<>();
    final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> receivedMessage.set(buffer.getStringWithoutLengthAscii(offset, length));
    try (Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
        try (Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
            publishMessage(bufferOne, bufferTwo, publication);
            pollForMessage(subscription, receivedMessage, fragmentHandler);
            assertEquals(expectedMessage, receivedMessage.get());
        }
        try (Publication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID)) {
            publishMessage(bufferOne, bufferTwo, publication);
            pollForMessage(subscription, receivedMessage, fragmentHandler);
            assertEquals(expectedMessage, receivedMessage.get());
        }
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) MutableReference(org.agrona.collections.MutableReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ThreadingMode(io.aeron.driver.ThreadingMode) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) CloseHelper(org.agrona.CloseHelper) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableReference(org.agrona.collections.MutableReference) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 18 with MutableReference

use of org.agrona.collections.MutableReference in project aeron by real-logic.

the class MultiModuleSharedDriverTest method shouldSupportTwoSingleNodeClusters.

@Test
@InterruptAfter(20)
@SuppressWarnings({ "try", "methodlength" })
public void shouldSupportTwoSingleNodeClusters() {
    final MediaDriver.Context driverCtx = new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).nameResolver(new RedirectingNameResolver(TestCluster.DEFAULT_NODE_MAPPINGS)).dirDeleteOnShutdown(false).dirDeleteOnStart(true);
    final Archive.Context archiveCtx = new Archive.Context().threadingMode(ArchiveThreadingMode.SHARED).archiveDir(new File(SystemUtil.tmpDirName(), "archive")).recordingEventsEnabled(false).deleteArchiveOnStart(true);
    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx, archiveCtx)) {
        final ConsensusModule.Context moduleCtx0 = new ConsensusModule.Context().clusterId(0).deleteDirOnStart(true).clusterDir(new File(SystemUtil.tmpDirName(), "cluster-0-0")).logChannel("aeron:ipc?term-length=64k").logStreamId(100).serviceStreamId(104).consensusModuleStreamId(105).ingressChannel("aeron:udp?endpoint=localhost:9020").replicationChannel("aeron:udp?endpoint=localhost:0");
        final ClusteredServiceContainer.Context containerCtx0 = new ClusteredServiceContainer.Context().clusterId(moduleCtx0.clusterId()).clusteredService(new EchoService()).clusterDir(moduleCtx0.clusterDir()).serviceStreamId(moduleCtx0.serviceStreamId()).consensusModuleStreamId(moduleCtx0.consensusModuleStreamId());
        final ConsensusModule.Context moduleCtx1 = new ConsensusModule.Context().clusterId(1).deleteDirOnStart(true).clusterDir(new File(SystemUtil.tmpDirName(), "cluster-0-1")).logChannel("aeron:ipc?term-length=64k").logStreamId(200).serviceStreamId(204).consensusModuleStreamId(205).ingressChannel("aeron:udp?endpoint=localhost:9021").replicationChannel("aeron:udp?endpoint=localhost:0");
        final ClusteredServiceContainer.Context containerCtx1 = new ClusteredServiceContainer.Context().clusteredService(new EchoService()).clusterDir(moduleCtx1.clusterDir()).serviceStreamId(moduleCtx1.serviceStreamId()).consensusModuleStreamId(moduleCtx1.consensusModuleStreamId()).clusterId(moduleCtx1.clusterId());
        ConsensusModule consensusModule0 = null;
        ClusteredServiceContainer container0 = null;
        ConsensusModule consensusModule1 = null;
        ClusteredServiceContainer container1 = null;
        AeronCluster client0 = null;
        AeronCluster client1 = null;
        try {
            consensusModule0 = ConsensusModule.launch(moduleCtx0);
            consensusModule1 = ConsensusModule.launch(moduleCtx1);
            container0 = ClusteredServiceContainer.launch(containerCtx0);
            container1 = ClusteredServiceContainer.launch(containerCtx1);
            final MutableReference<String> egress = new MutableReference<>();
            final EgressListener egressListener = (clusterSessionId, timestamp, buffer, offset, length, header) -> egress.set(buffer.getStringWithoutLengthAscii(offset, length));
            client0 = AeronCluster.connect(new AeronCluster.Context().egressListener(egressListener).ingressChannel(moduleCtx0.ingressChannel()).egressChannel("aeron:udp?endpoint=localhost:0"));
            client1 = AeronCluster.connect(new AeronCluster.Context().egressListener(egressListener).ingressChannel(moduleCtx1.ingressChannel()).egressChannel("aeron:udp?endpoint=localhost:0"));
            echoMessage(client0, "Message 0", egress);
            echoMessage(client1, "Message 1", egress);
        } finally {
            systemTestWatcher.dataCollector().add(moduleCtx0.clusterDir());
            systemTestWatcher.dataCollector().add(moduleCtx1.clusterDir());
            CloseHelper.closeAll(client0, client1, consensusModule0, consensusModule1, container0, container1);
        }
    } finally {
        systemTestWatcher.dataCollector().add(driverCtx.aeronDirectory());
        systemTestWatcher.dataCollector().add(archiveCtx.archiveDir());
    }
}
Also used : CommonContext(io.aeron.CommonContext) AeronCluster(io.aeron.cluster.client.AeronCluster) StubClusteredService(io.aeron.test.cluster.StubClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) io.aeron.test(io.aeron.test) SystemUtil(org.agrona.SystemUtil) RedirectingNameResolver(io.aeron.test.driver.RedirectingNameResolver) MutableReference(org.agrona.collections.MutableReference) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArchiveThreadingMode(io.aeron.archive.ArchiveThreadingMode) CloseHelper(org.agrona.CloseHelper) MediaDriver(io.aeron.driver.MediaDriver) Archive(io.aeron.archive.Archive) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) CommonContext(io.aeron.CommonContext) File(java.io.File) Test(org.junit.jupiter.api.Test) EgressListener(io.aeron.cluster.client.EgressListener) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) ThreadingMode(io.aeron.driver.ThreadingMode) TestCluster(io.aeron.test.cluster.TestCluster) DirectBuffer(org.agrona.DirectBuffer) Archive(io.aeron.archive.Archive) AeronCluster(io.aeron.cluster.client.AeronCluster) MutableReference(org.agrona.collections.MutableReference) EgressListener(io.aeron.cluster.client.EgressListener) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) MediaDriver(io.aeron.driver.MediaDriver) RedirectingNameResolver(io.aeron.test.driver.RedirectingNameResolver) File(java.io.File) ArchivingMediaDriver(io.aeron.archive.ArchivingMediaDriver) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer) Test(org.junit.jupiter.api.Test)

Example 19 with MutableReference

use of org.agrona.collections.MutableReference 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();
}
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 20 with MutableReference

use of org.agrona.collections.MutableReference 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)

Aggregations

MutableReference (org.agrona.collections.MutableReference)42 Test (org.junit.jupiter.api.Test)42 InterruptAfter (io.aeron.test.InterruptAfter)34 MutableLong (org.agrona.collections.MutableLong)28 RecordingSignal (io.aeron.archive.codecs.RecordingSignal)20 CountersReader (org.agrona.concurrent.status.CountersReader)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 MediaDriver (io.aeron.driver.MediaDriver)10 ThreadingMode (io.aeron.driver.ThreadingMode)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)10 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)10 File (java.io.File)8 CloseHelper (org.agrona.CloseHelper)8 CommonContext (io.aeron.CommonContext)6 AeronCluster (io.aeron.cluster.client.AeronCluster)6 EgressListener (io.aeron.cluster.client.EgressListener)6 io.aeron.test (io.aeron.test)6 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)6 Archive (io.aeron.archive.Archive)4