Search in sources :

Example 56 with MutableLong

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

the class ArchiveAuthenticationTest method shouldBeAbleToRecordWithAuthenticateOnChallengeResponse.

@Test
@InterruptAfter(10)
public void shouldBeAbleToRecordWithAuthenticateOnChallengeResponse() {
    final MutableLong authenticatorSessionId = new MutableLong(-1L);
    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());
            }
        }
    });
    launchArchivingMediaDriver(() -> authenticator);
    connectClient(credentialsSupplier);
    assertEquals(aeronArchive.controlSessionId(), authenticatorSessionId.value);
    createRecording();
}
Also used : MutableLong(org.agrona.collections.MutableLong) CredentialsSupplier(io.aeron.security.CredentialsSupplier) SessionProxy(io.aeron.security.SessionProxy) Authenticator(io.aeron.security.Authenticator) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 57 with MutableLong

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

the class ReplicateRecordingTest method shouldReplicateStoppedRecording.

@Test
@InterruptAfter(10)
public void shouldReplicateStoppedRecording() {
    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 counters = srcAeron.countersReader();
        final int counterId = awaitRecordingCounterId(counters, publication.sessionId());
        srcRecordingId = RecordingPos.getRecordingId(counters, counterId);
        offer(publication, messageCount, messagePrefix);
        awaitPosition(counters, counterId, publication.position());
    }
    srcAeronArchive.stopRecording(subscriptionId);
    final MutableLong dstRecordingId = new MutableLong();
    final MutableReference<RecordingSignal> signalRef = new MutableReference<>();
    final RecordingSignalAdapter adapter = newRecordingSignalAdapter(signalRef, dstRecordingId);
    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 ObjectHashSet<RecordingSignal> transitionEventsSet = new ObjectHashSet<>();
    transitionEventsSet.add(awaitSignal(signalRef, adapter));
    transitionEventsSet.add(awaitSignal(signalRef, adapter));
    assertTrue(transitionEventsSet.contains(RecordingSignal.STOP));
    assertTrue(transitionEventsSet.contains(RecordingSignal.SYNC));
}
Also used : MutableLong(org.agrona.collections.MutableLong) ObjectHashSet(org.agrona.collections.ObjectHashSet) 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 58 with MutableLong

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

the class ReplicateRecordingTest method shouldThrowExceptionWhenSrcRecordingIdUnknown.

@Test
@InterruptAfter(10)
public void shouldThrowExceptionWhenSrcRecordingIdUnknown() {
    final long unknownId = 7L;
    final ControlEventListener listener = mock(ControlEventListener.class);
    final MutableLong dstRecordingId = new MutableLong();
    final MutableReference<RecordingSignal> signalRef = new MutableReference<>();
    final RecordingSignalAdapter adapter = newRecordingSignalAdapter(listener, signalRef, dstRecordingId);
    final long replicationId = dstAeronArchive.replicate(unknownId, NULL_VALUE, SRC_CONTROL_STREAM_ID, SRC_CONTROL_REQUEST_CHANNEL, null);
    awaitSignalOrResponse(signalRef, adapter);
    verify(listener).onResponse(eq(dstAeronArchive.controlSessionId()), eq(replicationId), eq((long) ArchiveException.UNKNOWN_RECORDING), eq(ControlResponseCode.ERROR), anyString());
}
Also used : MutableLong(org.agrona.collections.MutableLong) RecordingSignal(io.aeron.archive.codecs.RecordingSignal) MutableReference(org.agrona.collections.MutableReference) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 59 with MutableLong

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

the class TimestampingSystemTest method shouldSupportReceiveTimestampsOnMds.

@Test
@InterruptAfter(10)
void shouldSupportReceiveTimestampsOnMds() {
    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.addPublication("aeron:udp?endpoint=localhost:23424", 1000);
        final Publication pub2 = aeron.addPublication("aeron:udp?endpoint=localhost:23425", 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");
        }
        buffer.putLong(0, SENTINEL_VALUE);
        while (0 > pub1.offer(buffer, 0, buffer.capacity())) {
            Tests.yieldingIdle("Failed to offer message");
        }
        while (0 > pub2.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 > mdsSub.poll(fragmentHandler, 1)) {
            Tests.yieldingIdle("Failed to receive message");
        }
        assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
        while (1 > mdsSub.poll(fragmentHandler, 1)) {
            Tests.yieldingIdle("Failed to receive message");
        }
        assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) BeforeEach(org.junit.jupiter.api.BeforeEach) RegistrationException(io.aeron.exceptions.RegistrationException) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) OS(org.junit.jupiter.api.condition.OS) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) MutableLong(org.agrona.collections.MutableLong) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Objects.requireNonNull(java.util.Objects.requireNonNull) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) MutableDirectBuffer(org.agrona.MutableDirectBuffer) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) DirectBuffer(org.agrona.DirectBuffer) MutableLong(org.agrona.collections.MutableLong) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MutableDirectBuffer(org.agrona.MutableDirectBuffer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 60 with MutableLong

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

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