Search in sources :

Example 21 with FragmentHandler

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

the class ExclusivePublicationTest method shouldOfferTwoBuffersFromConcurrentExclusivePublications.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldOfferTwoBuffersFromConcurrentExclusivePublications(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 + SIZE_OF_INT, length);
            final int publisherId = buffer.getInt(offset);
            if (1 == publisherId) {
                assertEquals(Byte.MIN_VALUE, buffer.getByte(offset + SIZE_OF_INT));
            } else if (2 == publisherId) {
                assertEquals(Byte.MAX_VALUE, buffer.getByte(offset + SIZE_OF_INT));
            } else {
                fail("unknown publisherId=" + publisherId);
            }
            messageCount.value++;
        };
        Tests.awaitConnections(subscription, 2);
        final UnsafeBuffer pubOneHeader = new UnsafeBuffer(new byte[SIZE_OF_INT]);
        pubOneHeader.putInt(0, 1);
        final UnsafeBuffer pubOnePayload = new UnsafeBuffer(new byte[MESSAGE_LENGTH]);
        pubOnePayload.setMemory(0, MESSAGE_LENGTH, Byte.MIN_VALUE);
        final UnsafeBuffer pubTwoHeader = new UnsafeBuffer(new byte[SIZE_OF_INT]);
        pubTwoHeader.putInt(0, 2);
        final UnsafeBuffer pubTwoPayload = new UnsafeBuffer(new byte[MESSAGE_LENGTH]);
        pubTwoPayload.setMemory(0, MESSAGE_LENGTH, Byte.MAX_VALUE);
        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(pubOneHeader, 0, SIZE_OF_INT, pubOnePayload, 0, MESSAGE_LENGTH) < 0L) {
                        Tests.yield();
                    }
                }
                return null;
            });
            threadPool.submit(() -> {
                latch.countDown();
                latch.await();
                for (int count = 0; count < fragmentsPerThread; count++) {
                    while (publicationTwo.offer(pubTwoHeader, 0, SIZE_OF_INT, pubTwoPayload, 0, MESSAGE_LENGTH) < 0L) {
                        Tests.yield();
                    }
                }
                return null;
            });
            threadPool.shutdown();
            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) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) CountDownLatch(java.util.concurrent.CountDownLatch) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 22 with FragmentHandler

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

the class ClientErrorHandlerTest method shouldHaveCorrectTermBufferLength.

@Test
@InterruptAfter(10)
@SuppressWarnings("try")
void shouldHaveCorrectTermBufferLength() {
    final MediaDriver.Context ctx = new MediaDriver.Context().errorHandler(Tests::onError).dirDeleteOnStart(true);
    final ErrorHandler mockErrorHandlerOne = mock(ErrorHandler.class);
    final Aeron.Context clientCtxOne = new Aeron.Context().errorHandler(mockErrorHandlerOne);
    final ErrorHandler mockErrorHandlerTwo = mock(ErrorHandler.class);
    final Aeron.Context clientCtxTwo = new Aeron.Context().errorHandler(mockErrorHandlerTwo).subscriberErrorHandler(RethrowingErrorHandler.INSTANCE);
    try (TestMediaDriver ignore = TestMediaDriver.launch(ctx, testWatcher);
        Aeron aeronOne = Aeron.connect(clientCtxOne);
        Aeron aeronTwo = Aeron.connect(clientCtxTwo);
        Publication publication = aeronOne.addPublication(CHANNEL, STREAM_ID);
        Subscription subscriptionOne = aeronOne.addSubscription(CHANNEL, STREAM_ID);
        Subscription subscriptionTwo = aeronTwo.addSubscription(CHANNEL, STREAM_ID)) {
        awaitConnected(subscriptionOne);
        awaitConnected(subscriptionTwo);
        assertEquals(clientCtxOne.errorHandler(), clientCtxOne.subscriberErrorHandler());
        assertNotEquals(clientCtxTwo.errorHandler(), clientCtxTwo.subscriberErrorHandler());
        final UnsafeBuffer buffer = new UnsafeBuffer(new byte[100]);
        while (publication.offer(buffer) < 0) {
            Tests.yield();
        }
        final RuntimeException expectedException = new RuntimeException("Expected");
        final FragmentHandler handler = (buffer1, offset, length, header) -> {
            throw expectedException;
        };
        while (0 == subscriptionOne.poll(handler, 1)) {
            Tests.yield();
        }
        verify(mockErrorHandlerOne).onError(expectedException);
        try {
            while (0 == subscriptionTwo.poll(handler, 1)) {
                Tests.yield();
            }
            fail("Expected exception");
        } catch (final Exception ex) {
            assertEquals(expectedException, ex);
        }
        verify(mockErrorHandlerTwo, never()).onError(any());
    } finally {
        ctx.deleteDirectory();
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) ErrorHandler(org.agrona.ErrorHandler) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) Mockito(org.mockito.Mockito) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Assertions(org.junit.jupiter.api.Assertions) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Tests.awaitConnected(io.aeron.test.Tests.awaitConnected) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) ErrorHandler(org.agrona.ErrorHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MediaDriver(io.aeron.driver.MediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 23 with FragmentHandler

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

the class MultiSubscriberTest method shouldReceiveMessageOnSeparateSubscriptions.

@Test
@InterruptAfter(10)
void shouldReceiveMessageOnSeparateSubscriptions() {
    final FragmentHandler mockFragmentHandlerOne = mock(FragmentHandler.class);
    final FragmentHandler mockFragmentHandlerTwo = mock(FragmentHandler.class);
    final FragmentAssembler adapterOne = new FragmentAssembler(mockFragmentHandlerOne);
    final FragmentAssembler adapterTwo = new FragmentAssembler(mockFragmentHandlerTwo);
    try (Subscription subscriptionOne = aeron.addSubscription(CHANNEL_1, STREAM_ID);
        Subscription subscriptionTwo = aeron.addSubscription(CHANNEL_2, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL_1, STREAM_ID)) {
        final byte[] expectedBytes = "Hello, World! here is a small message".getBytes();
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(expectedBytes);
        assertEquals(0, subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT));
        assertEquals(0, subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT));
        while (!subscriptionOne.isConnected() || !subscriptionTwo.isConnected()) {
            Tests.yield();
        }
        while (publication.offer(srcBuffer) < 0L) {
            Tests.yield();
        }
        while (subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT) == 0) {
            Tests.yield();
        }
        while (subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT) == 0) {
            Tests.yield();
        }
        verifyData(srcBuffer, mockFragmentHandlerOne);
        verifyData(srcBuffer, mockFragmentHandlerTwo);
    }
}
Also used : FragmentHandler(io.aeron.logbuffer.FragmentHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 24 with FragmentHandler

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

the class DriverLoggingAgentTest method testLogMediaDriverEvents.

private void testLogMediaDriverEvents(final String channel, final String enabledEvents, final EnumSet<DriverEventCode> expectedEvents) {
    before(enabledEvents, expectedEvents);
    final MediaDriver.Context driverCtx = new MediaDriver.Context().errorHandler(Tests::onError).publicationLingerTimeoutNs(0).timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(1));
    try (MediaDriver mediaDriver = MediaDriver.launch(driverCtx)) {
        try (Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
            Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
            Publication publication = aeron.addPublication(channel, STREAM_ID)) {
            final UnsafeBuffer offerBuffer = new UnsafeBuffer(new byte[32]);
            while (publication.offer(offerBuffer) < 0) {
                Tests.yield();
            }
            final MutableInteger counter = new MutableInteger();
            final FragmentHandler handler = (buffer, offset, length, header) -> counter.value++;
            while (0 == subscription.poll(handler, 1)) {
                Tests.yield();
            }
            assertEquals(counter.get(), 1);
        }
        final Supplier<String> errorMessage = () -> "Pending events: " + WAIT_LIST;
        while (!WAIT_LIST.isEmpty()) {
            Tests.yieldingIdle(errorMessage);
        }
    }
}
Also used : Tests(io.aeron.test.Tests) DriverEventCode(io.aeron.agent.DriverEventCode) Subscription(io.aeron.Subscription) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) EnumSource(org.junit.jupiter.params.provider.EnumSource) Supplier(java.util.function.Supplier) Collections.synchronizedSet(java.util.Collections.synchronizedSet) MessageHandler(org.agrona.concurrent.MessageHandler) EVENT_READER_FRAME_LIMIT(io.aeron.agent.EventConfiguration.EVENT_READER_FRAME_LIMIT) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) EVENT_RING_BUFFER(io.aeron.agent.EventConfiguration.EVENT_RING_BUFFER) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Publication(io.aeron.Publication) Agent(org.agrona.concurrent.Agent) MutableInteger(org.agrona.collections.MutableInteger) EnumSet(java.util.EnumSet) MediaDriver(io.aeron.driver.MediaDriver) Aeron(io.aeron.Aeron) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) EnumMap(java.util.EnumMap) Set(java.util.Set) IPC_CHANNEL(io.aeron.CommonContext.IPC_CHANNEL) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) INCLUDE(org.junit.jupiter.params.provider.EnumSource.Mode.INCLUDE) MutableDirectBuffer(org.agrona.MutableDirectBuffer) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) Publication(io.aeron.Publication) Tests(io.aeron.test.Tests) Aeron(io.aeron.Aeron) MediaDriver(io.aeron.driver.MediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Subscription(io.aeron.Subscription) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 25 with FragmentHandler

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

the class SubscriptionTest method shouldReadData.

@Test
public void shouldReadData() {
    subscription.addImage(imageOneMock);
    when(imageOneMock.poll(any(FragmentHandler.class), anyInt())).then((invocation) -> {
        final FragmentHandler handler = (FragmentHandler) invocation.getArguments()[0];
        handler.onFragment(atomicReadBuffer, HEADER_LENGTH, READ_BUFFER_CAPACITY - HEADER_LENGTH, header);
        return 1;
    });
    assertEquals(1, subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT));
    verify(fragmentHandler).onFragment(eq(atomicReadBuffer), eq(HEADER_LENGTH), eq(READ_BUFFER_CAPACITY - HEADER_LENGTH), any(Header.class));
}
Also used : Header(io.aeron.logbuffer.Header) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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