Search in sources :

Example 26 with MutableLong

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

Example 28 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 29 with MutableLong

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

the class TimestampingSystemTest method shouldSupportReceiveTimestampsOnMergedMds.

@Test
@InterruptAfter(10)
void shouldSupportReceiveTimestampsOnMergedMds() {
    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.addExclusivePublication("aeron:udp?endpoint=localhost:23424", 1000);
        final String pub2Uri = new ChannelUriStringBuilder("aeron:udp?endpoint=localhost:23425").initialPosition(0L, pub1.initialTermId(), pub1.termBufferLength()).sessionId(pub1.sessionId()).build();
        final Publication pub2 = aeron.addExclusivePublication(pub2Uri, 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");
        }
        final MutableLong sendTimestamp = new MutableLong(SENTINEL_VALUE);
        buffer.putLong(0, SENTINEL_VALUE);
        while (0 > pub1.offer(buffer, 0, buffer.capacity())) {
            Tests.yieldingIdle("Failed to offer message");
        }
        buffer.putLong(0, SENTINEL_VALUE);
        while (0 > pub2.offer(buffer, 0, buffer.capacity())) {
            Tests.yieldingIdle("Failed to offer message");
        }
        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());
        buffer.putLong(0, SENTINEL_VALUE);
        while (0 > pub2.offer(buffer, 0, buffer.capacity())) {
            Tests.yieldingIdle("Failed to offer message");
        }
        buffer.putLong(0, SENTINEL_VALUE);
        while (0 > pub1.offer(buffer, 0, buffer.capacity())) {
            Tests.yieldingIdle("Failed to offer message");
        }
        sendTimestamp.set(SENTINEL_VALUE);
        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 30 with MutableLong

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

the class TimestampingSystemTest method shouldSupportChannelSendTimestampsOnMdc.

@Test
@InterruptAfter(10)
void shouldSupportChannelSendTimestampsOnMdc() {
    final MutableDirectBuffer buffer = new UnsafeBuffer(new byte[64]);
    try (TestMediaDriver driver = driver();
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(driver.aeronDirectoryName()))) {
        final Publication mdcPub = aeron.addPublication("aeron:udp?control-mode=manual|channel-snd-ts-offset=0", 1000);
        final Subscription sub1 = aeron.addSubscription("aeron:udp?endpoint=localhost:23424", 1000);
        final Subscription sub2 = aeron.addSubscription("aeron:udp?endpoint=localhost:23425", 1000);
        mdcPub.addDestination("aeron:udp?endpoint=localhost:23424");
        mdcPub.addDestination("aeron:udp?endpoint=localhost:23425");
        while (!sub1.isConnected() || !sub2.isConnected()) {
            Tests.yieldingIdle("Failed to connect");
        }
        buffer.putLong(0, SENTINEL_VALUE);
        while (0 > mdcPub.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 > sub1.poll(fragmentHandler, 1)) {
            Tests.yieldingIdle("Failed to receive message");
        }
        assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
        while (1 > sub2.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)

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