Search in sources :

Example 1 with MutableReference

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

the class AuthenticationTest method shouldRejectOnConnectRequest.

@Test
@InterruptAfter(10)
public void shouldRejectOnConnectRequest() {
    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() {

        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.reject();
        }

        public void onChallengedSession(final SessionProxy sessionProxy, final long nowMs) {
            fail();
        }
    });
    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 2 with MutableReference

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

the class AuthenticationTest method shouldAuthenticateOnConnectRequestWithCredentials.

@Test
@InterruptAfter(10)
public void shouldAuthenticateOnConnectRequestWithCredentials() {
    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 encodedCredentials;
        }

        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(CREDENTIALS_STRING, new String(encodedCredentials));
        }

        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(PRINCIPAL_STRING.getBytes());
        }

        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(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 3 with MutableReference

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

Example 4 with MutableReference

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

the class ReplicateRecordingTest method shouldReplicateMoreThanOnce.

@Test
@InterruptAfter(10)
public void shouldReplicateMoreThanOnce() {
    final String messagePrefix = "Message-Prefix-";
    final int messageCount = 10;
    final long srcRecordingId;
    final long subscriptionId = srcAeronArchive.startRecording(LIVE_CHANNEL, LIVE_STREAM_ID, LOCAL);
    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 recordingIdRef = new MutableLong();
        final MutableReference<RecordingSignal> signalRef = new MutableReference<>();
        final RecordingSignalAdapter adapter = newRecordingSignalAdapter(signalRef, recordingIdRef);
        long replicationId = dstAeronArchive.replicate(srcRecordingId, NULL_VALUE, SRC_CONTROL_STREAM_ID, SRC_CONTROL_REQUEST_CHANNEL, null);
        assertEquals(RecordingSignal.REPLICATE, awaitSignal(signalRef, adapter));
        assertEquals(RecordingSignal.EXTEND, awaitSignal(signalRef, adapter));
        final CountersReader dstCounters = dstAeron.countersReader();
        final long dstRecordingId = recordingIdRef.get();
        int dstCounterId = RecordingPos.findCounterIdByRecording(dstCounters, dstRecordingId);
        awaitPosition(dstCounters, dstCounterId, publication.position());
        dstAeronArchive.stopReplication(replicationId);
        assertEquals(RecordingSignal.STOP, awaitSignal(signalRef, adapter));
        replicationId = dstAeronArchive.replicate(srcRecordingId, dstRecordingId, SRC_CONTROL_STREAM_ID, SRC_CONTROL_REQUEST_CHANNEL, null);
        assertEquals(RecordingSignal.EXTEND, awaitSignal(signalRef, adapter));
        dstCounterId = RecordingPos.findCounterIdByRecording(dstCounters, dstRecordingId);
        offer(publication, messageCount, messagePrefix);
        awaitPosition(dstCounters, dstCounterId, publication.position());
        dstAeronArchive.stopReplication(replicationId);
    }
    srcAeronArchive.stopRecording(subscriptionId);
}
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 5 with MutableReference

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

the class ReplicateRecordingTest method shouldReplicateLiveRecordingAndStopAtSpecifiedPosition.

@Test
@InterruptAfter(10)
public void shouldReplicateLiveRecordingAndStopAtSpecifiedPosition() {
    final String messagePrefix = "Message-Prefix-";
    final int messageCount = 10;
    final long srcRecordingId;
    final long subscriptionId = srcAeronArchive.startRecording(LIVE_CHANNEL, LIVE_STREAM_ID, LOCAL);
    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);
        final long firstPosition = publication.position();
        awaitPosition(srcCounters, counterId, firstPosition);
        offer(publication, messageCount, messagePrefix);
        awaitPosition(srcCounters, counterId, publication.position());
        final MutableLong dstRecordingId = new MutableLong();
        final MutableReference<RecordingSignal> signalRef = new MutableReference<>();
        final RecordingSignalAdapter adapter = newRecordingSignalAdapter(signalRef, dstRecordingId);
        dstAeronArchive.replicate(srcRecordingId, NULL_VALUE, firstPosition, SRC_CONTROL_STREAM_ID, SRC_CONTROL_REQUEST_CHANNEL, null, null);
        assertEquals(RecordingSignal.REPLICATE, awaitSignal(signalRef, adapter));
        assertEquals(RecordingSignal.EXTEND, awaitSignal(signalRef, adapter));
        assertEquals(RecordingSignal.STOP, awaitSignal(signalRef, adapter));
        offer(publication, messageCount, messagePrefix);
        final int srcCounterId = RecordingPos.findCounterIdByRecording(srcCounters, srcRecordingId);
        awaitPosition(srcCounters, srcCounterId, publication.position());
        assertTrue(firstPosition < publication.position());
        long dstStopPosition;
        while (NULL_POSITION == (dstStopPosition = dstAeronArchive.getStopPosition(dstRecordingId.get()))) {
            Tests.yield();
        }
        assertEquals(firstPosition, dstStopPosition);
    }
    srcAeronArchive.stopRecording(subscriptionId);
}
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)

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