Search in sources :

Example 81 with MutableLong

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

the class MultiDestinationSubscriptionTest method shouldMergeStreamsFromMultiplePublicationsWithSameParams.

@Test
@InterruptAfter(10)
void shouldMergeStreamsFromMultiplePublicationsWithSameParams() {
    final int numMessagesToSend = 30;
    final int numMessagesToSendForA = numMessagesToSend / 2;
    final int numMessagesToSendForB = numMessagesToSend / 2;
    launch(Tests::onError);
    launchSecond();
    final String publicationChannelA = new ChannelUriStringBuilder().media(CommonContext.UDP_MEDIA).endpoint(UNICAST_ENDPOINT_A).build();
    final String destinationB = new ChannelUriStringBuilder().media(CommonContext.UDP_MEDIA).endpoint(UNICAST_ENDPOINT_B).build();
    subscription = clientA.addSubscription(SUB_URI, STREAM_ID);
    subscription.addDestination(publicationChannelA);
    subscription.addDestination(destinationB);
    publicationA = clientA.addExclusivePublication(publicationChannelA, STREAM_ID);
    final String publicationChannelB = new ChannelUriStringBuilder().media(CommonContext.UDP_MEDIA).initialPosition(0L, publicationA.initialTermId(), publicationA.termBufferLength()).sessionId(publicationA.sessionId()).endpoint(UNICAST_ENDPOINT_B).build();
    publicationB = clientB.addExclusivePublication(publicationChannelB, STREAM_ID);
    final MutableLong position = new MutableLong(Long.MIN_VALUE);
    final Supplier<String> offerFailure = () -> "Failed to offer: " + position;
    for (int i = 0; i < numMessagesToSendForA; i++) {
        while (publicationA.offer(buffer, 0, buffer.capacity()) < 0L) {
            Tests.yield();
        }
        pollForFragment(subscription, fragmentHandler);
        while ((position.value = publicationB.offer(buffer, 0, buffer.capacity())) < 0L) {
            Tests.yieldingIdle(offerFailure);
        }
        assertEquals(0, subscription.poll(fragmentHandler, 10));
    }
    for (int i = 0; i < numMessagesToSendForB; i++) {
        while (publicationB.offer(buffer, 0, buffer.capacity()) < 0L) {
            Tests.yield();
        }
        pollForFragment(subscription, fragmentHandler);
        while (publicationA.offer(buffer, 0, buffer.capacity()) < 0L) {
            Tests.yield();
        }
        assertEquals(0, subscription.poll(fragmentHandler, 10));
    }
    assertEquals(1, subscription.imageCount());
    assertEquals(2, subscription.imageAtIndex(0).activeTransportCount());
    verifyFragments(fragmentHandler, numMessagesToSend);
}
Also used : MutableLong(org.agrona.collections.MutableLong) Test(org.junit.jupiter.api.Test)

Example 82 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)

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