Search in sources :

Example 66 with FragmentHandler

use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.

the class TimestampingSystemTest method shouldSupportSendReceiveTimestamps.

@Test
@InterruptAfter(10)
void shouldSupportSendReceiveTimestamps() {
    final MutableDirectBuffer buffer = new UnsafeBuffer(new byte[64]);
    try (TestMediaDriver driver = driver();
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(driver.aeronDirectoryName()))) {
        final Subscription sub = aeron.addSubscription(CHANNEL_WITH_CHANNEL_TIMESTAMPS, 1000);
        while (null == sub.resolvedEndpoint()) {
            Tests.yieldingIdle("Failed to resolve endpoint");
        }
        final String uri = new ChannelUriStringBuilder(CHANNEL_WITH_CHANNEL_TIMESTAMPS).endpoint(requireNonNull(sub.resolvedEndpoint())).build();
        final Publication pub = aeron.addPublication(uri, 1000);
        Tests.awaitConnected(pub);
        buffer.putLong(0, SENTINEL_VALUE);
        buffer.putLong(8, SENTINEL_VALUE);
        while (0 > pub.offer(buffer, 0, buffer.capacity())) {
            Tests.yieldingIdle("Failed to offer message");
        }
        final MutableLong receiveTimestamp = new MutableLong(SENTINEL_VALUE);
        final MutableLong sendTimestamp = new MutableLong(SENTINEL_VALUE);
        final FragmentHandler fragmentHandler = (buffer1, offset, length, header) -> {
            receiveTimestamp.set(buffer1.getLong(offset));
            sendTimestamp.set(buffer1.getLong(offset + 8));
        };
        while (1 > sub.poll(fragmentHandler, 1)) {
            Tests.yieldingIdle("Failed to receive message");
        }
        assertNotEquals(SENTINEL_VALUE, receiveTimestamp.longValue());
        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 67 with FragmentHandler

use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.

the class UntetheredSubscriptionTest method shouldRejoinAfterResting.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
public void shouldRejoinAfterResting(final String channel) {
    final AtomicInteger unavailableImageCount = new AtomicInteger();
    final AtomicInteger availableImageCount = new AtomicInteger();
    final UnavailableImageHandler unavailableHandler = (image) -> unavailableImageCount.incrementAndGet();
    final AvailableImageHandler availableHandler = (image) -> availableImageCount.incrementAndGet();
    final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
    };
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocate(MESSAGE_LENGTH));
    final String untetheredChannel = channel + "|tether=false";
    final String publicationChannel = channel.startsWith("aeron-spy") ? channel.substring(10) : channel;
    boolean pollingUntethered = true;
    try (Subscription tetheredSub = aeron.addSubscription(channel, STREAM_ID);
        Subscription untetheredSub = aeron.addSubscription(untetheredChannel, STREAM_ID, availableHandler, unavailableHandler);
        Publication publication = aeron.addPublication(publicationChannel, STREAM_ID)) {
        while (!tetheredSub.isConnected() || !untetheredSub.isConnected()) {
            Tests.yield();
            aeron.conductorAgentInvoker().invoke();
        }
        while (true) {
            if (publication.offer(srcBuffer) < 0) {
                Tests.yield();
                aeron.conductorAgentInvoker().invoke();
            }
            if (pollingUntethered && untetheredSub.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT) > 0) {
                pollingUntethered = false;
            }
            tetheredSub.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT);
            if (unavailableImageCount.get() == 1) {
                while (availableImageCount.get() < 2) {
                    Tests.yield();
                    aeron.conductorAgentInvoker().invoke();
                }
                return;
            }
        }
    }
}
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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteBuffer(java.nio.ByteBuffer) TimeUnit(java.util.concurrent.TimeUnit) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ThreadingMode(io.aeron.driver.ThreadingMode) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) CloseHelper(org.agrona.CloseHelper) MethodSource(org.junit.jupiter.params.provider.MethodSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FragmentHandler(io.aeron.logbuffer.FragmentHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 68 with FragmentHandler

use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.

the class ExclusivePublicationTest method shouldPublishFromIndependentExclusivePublications.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldPublishFromIndependentExclusivePublications(final String channel) {
    try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
        ExclusivePublication publicationOne = aeron.addExclusivePublication(channel, STREAM_ID);
        ExclusivePublication publicationTwo = aeron.addExclusivePublication(channel, STREAM_ID)) {
        final int expectedNumberOfFragments = 778;
        int totalFragmentsRead = 0;
        final MutableInteger messageCount = new MutableInteger();
        final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
            assertEquals(MESSAGE_LENGTH, length);
            messageCount.value++;
        };
        Tests.awaitConnections(subscription, 2);
        for (int i = 0; i < expectedNumberOfFragments; i += 2) {
            while (publicationOne.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L) {
                Tests.yield();
                totalFragmentsRead += pollFragments(subscription, fragmentHandler);
            }
            while (publicationTwo.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L) {
                Tests.yield();
                totalFragmentsRead += pollFragments(subscription, fragmentHandler);
            }
            totalFragmentsRead += pollFragments(subscription, fragmentHandler);
        }
        do {
            totalFragmentsRead += pollFragments(subscription, fragmentHandler);
        } while (totalFragmentsRead < expectedNumberOfFragments);
        assertEquals(expectedNumberOfFragments, messageCount.value);
    }
}
Also used : SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) RawBlockHandler(io.aeron.logbuffer.RawBlockHandler) SIZE_OF_INT(org.agrona.BitUtil.SIZE_OF_INT) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) CLOSED(io.aeron.Publication.CLOSED) Arrays.asList(java.util.Arrays.asList) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) ExecutorService(java.util.concurrent.ExecutorService) MethodSource(org.junit.jupiter.params.provider.MethodSource) FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) MediaDriver(io.aeron.driver.MediaDriver) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) BACK_PRESSURED(io.aeron.Publication.BACK_PRESSURED) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) CountDownLatch(java.util.concurrent.CountDownLatch) YieldingIdleStrategy(org.agrona.concurrent.YieldingIdleStrategy) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) LITTLE_ENDIAN(java.nio.ByteOrder.LITTLE_ENDIAN) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) FragmentHandler(io.aeron.logbuffer.FragmentHandler) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 69 with FragmentHandler

use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.

the class ExclusivePublicationTest method shouldPublishFromConcurrentExclusivePublications.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldPublishFromConcurrentExclusivePublications(final String channel) {
    try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
        ExclusivePublication publicationOne = aeron.addExclusivePublication(channel, STREAM_ID);
        ExclusivePublication publicationTwo = aeron.addExclusivePublication(channel, STREAM_ID)) {
        final int expectedNumberOfFragments = 20_000;
        final int fragmentsPerThread = expectedNumberOfFragments / 2;
        final MutableInteger messageCount = new MutableInteger();
        final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
            assertEquals(MESSAGE_LENGTH, length);
            messageCount.value++;
        };
        Tests.awaitConnections(subscription, 2);
        final ExecutorService threadPool = Executors.newFixedThreadPool(2);
        try {
            final CountDownLatch latch = new CountDownLatch(2);
            threadPool.submit(() -> {
                latch.countDown();
                latch.await();
                for (int count = 0; count < fragmentsPerThread; count++) {
                    while (publicationOne.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L) {
                        Tests.yield();
                    }
                }
                return null;
            });
            threadPool.submit(() -> {
                latch.countDown();
                latch.await();
                for (int count = 0; count < fragmentsPerThread; count++) {
                    while (publicationTwo.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L) {
                        Tests.yield();
                    }
                }
                return null;
            });
            int totalFragmentsRead = 0;
            do {
                totalFragmentsRead += pollFragments(subscription, fragmentHandler);
            } while (totalFragmentsRead < expectedNumberOfFragments);
        } finally {
            threadPool.shutdownNow();
        }
        assertEquals(expectedNumberOfFragments, messageCount.value);
    }
}
Also used : SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) RawBlockHandler(io.aeron.logbuffer.RawBlockHandler) SIZE_OF_INT(org.agrona.BitUtil.SIZE_OF_INT) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) CLOSED(io.aeron.Publication.CLOSED) Arrays.asList(java.util.Arrays.asList) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) ExecutorService(java.util.concurrent.ExecutorService) MethodSource(org.junit.jupiter.params.provider.MethodSource) FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) MediaDriver(io.aeron.driver.MediaDriver) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) BACK_PRESSURED(io.aeron.Publication.BACK_PRESSURED) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) CountDownLatch(java.util.concurrent.CountDownLatch) YieldingIdleStrategy(org.agrona.concurrent.YieldingIdleStrategy) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) LITTLE_ENDIAN(java.nio.ByteOrder.LITTLE_ENDIAN) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) FragmentHandler(io.aeron.logbuffer.FragmentHandler) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 70 with FragmentHandler

use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.

the class PublicationUnblockTest method shouldUnblockNonCommittedMessage.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldUnblockNonCommittedMessage(final String channel) {
    final MutableInteger fragmentCount = new MutableInteger();
    final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> fragmentCount.value++;
    try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
        Publication publicationOne = aeron.addPublication(channel, STREAM_ID);
        Publication publicationTwo = aeron.addPublication(channel, STREAM_ID)) {
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[driver.context().mtuLength()]);
        final int length = 128;
        srcBuffer.setMemory(0, length, (byte) 66);
        final BufferClaim bufferClaim = new BufferClaim();
        while (publicationOne.tryClaim(length, bufferClaim) < 0L) {
            Tests.yield();
        }
        bufferClaim.buffer().setMemory(bufferClaim.offset(), length, (byte) 65);
        bufferClaim.commit();
        while (publicationTwo.offer(srcBuffer, 0, length) < 0L) {
            Tests.yield();
        }
        while (publicationOne.tryClaim(length, bufferClaim) < 0L) {
            Tests.yield();
        }
        while (publicationTwo.offer(srcBuffer, 0, length) < 0L) {
            Tests.yield();
        }
        final int expectedFragments = 3;
        int numFragments = 0;
        do {
            final int fragments = subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT);
            if (fragments == 0) {
                Tests.yield();
            }
            numFragments += fragments;
        } while (numFragments < expectedFragments);
        assertEquals(expectedFragments, numFragments);
        assertEquals(expectedFragments, fragmentCount.value);
    }
}
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) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) TimeUnit(java.util.concurrent.TimeUnit) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ThreadingMode(io.aeron.driver.ThreadingMode) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Arrays.asList(java.util.Arrays.asList) BufferClaim(io.aeron.logbuffer.BufferClaim) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MethodSource(org.junit.jupiter.params.provider.MethodSource) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) BufferClaim(io.aeron.logbuffer.BufferClaim) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

FragmentHandler (io.aeron.logbuffer.FragmentHandler)70 MediaDriver (io.aeron.driver.MediaDriver)50 InterruptAfter (io.aeron.test.InterruptAfter)44 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)42 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)42 Tests (io.aeron.test.Tests)40 Test (org.junit.jupiter.api.Test)40 InterruptingTestCallback (io.aeron.test.InterruptingTestCallback)36 TestMediaDriver (io.aeron.test.driver.TestMediaDriver)36 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)36 SystemTestWatcher (io.aeron.test.SystemTestWatcher)34 ThreadingMode (io.aeron.driver.ThreadingMode)30 AfterEach (org.junit.jupiter.api.AfterEach)30 CloseHelper (org.agrona.CloseHelper)28 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 MutableInteger (org.agrona.collections.MutableInteger)22 DirectBuffer (org.agrona.DirectBuffer)20 Assertions (org.junit.jupiter.api.Assertions)20 TimeUnit (java.util.concurrent.TimeUnit)18 MethodSource (org.junit.jupiter.params.provider.MethodSource)18