Search in sources :

Example 31 with FragmentHandler

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

the class BasicArchiveTest method consume.

private static void consume(final Subscription subscription, final int count, final String prefix) {
    final MutableInteger received = new MutableInteger(0);
    final FragmentHandler fragmentHandler = new FragmentAssembler((buffer, offset, length, header) -> {
        final String expected = prefix + received.value;
        final String actual = buffer.getStringWithoutLengthAscii(offset, length);
        assertEquals(expected, actual);
        received.value++;
    });
    while (received.value < count) {
        if (0 == subscription.poll(fragmentHandler, FRAGMENT_LIMIT)) {
            SystemTest.checkInterruptedStatus();
            Thread.yield();
        }
    }
    assertThat(received.get(), is(count));
}
Also used : FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger)

Example 32 with FragmentHandler

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

the class ExtendRecordingTest method consume.

private static void consume(final Subscription subscription, final int startIndex, final int count, final String prefix) {
    final MutableInteger received = new MutableInteger(startIndex);
    final FragmentHandler fragmentHandler = new FragmentAssembler((buffer, offset, length, header) -> {
        final String expected = prefix + received.value;
        final String actual = buffer.getStringWithoutLengthAscii(offset, length);
        assertEquals(expected, actual);
        received.value++;
    });
    while (received.value < (startIndex + count)) {
        if (0 == subscription.poll(fragmentHandler, FRAGMENT_LIMIT)) {
            SystemTest.checkInterruptedStatus();
            Thread.yield();
        }
    }
    assertThat(received.get(), is(startIndex + count));
}
Also used : FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger)

Example 33 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 34 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)

Example 35 with FragmentHandler

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

the class SubscriptionTest method shouldReadDataFromMultipleSources.

@Test
public void shouldReadDataFromMultipleSources() {
    subscription.addImage(imageOneMock);
    subscription.addImage(imageTwoMock);
    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;
    });
    when(imageTwoMock.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(2, subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT));
}
Also used : 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