Search in sources :

Example 61 with FragmentHandler

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

the class ArchiveSystemTests method consume.

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)) {
            Tests.yield();
        }
    }
    assertEquals(count, received.get());
}
Also used : FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) FragmentAssembler(io.aeron.FragmentAssembler)

Example 62 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 MutableInteger received = new MutableInteger(startIndex);
    final FragmentHandler fragmentHandler = new FragmentAssembler((buffer, offset, length, header) -> {
        final String expected = MESSAGE_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, ArchiveSystemTests.FRAGMENT_LIMIT)) {
            Tests.yield();
        }
    }
    assertEquals(startIndex + count, received.get());
}
Also used : FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger)

Example 63 with FragmentHandler

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

the class TwoBufferOfferMessageTest method shouldTransferFragmentedTwoPartMessage.

@Test
@InterruptAfter(10)
void shouldTransferFragmentedTwoPartMessage() {
    final UnsafeBuffer expectedBuffer = new UnsafeBuffer(new byte[32 + driver.context().mtuLength()]);
    final UnsafeBuffer bufferOne = new UnsafeBuffer(expectedBuffer, 0, 32);
    final UnsafeBuffer bufferTwo = new UnsafeBuffer(expectedBuffer, 32, expectedBuffer.capacity() - 32);
    bufferOne.setMemory(0, bufferOne.capacity(), (byte) 'a');
    bufferTwo.setMemory(0, bufferTwo.capacity(), (byte) 'b');
    final String expectedMessage = expectedBuffer.getStringWithoutLengthAscii(0, expectedBuffer.capacity());
    final MutableReference<String> receivedMessage = new MutableReference<>();
    final FragmentHandler fragmentHandler = new FragmentAssembler((buffer, offset, length, header) -> receivedMessage.set(buffer.getStringWithoutLengthAscii(offset, length)));
    try (Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
        try (Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
            publishMessage(bufferOne, bufferTwo, publication);
            pollForMessage(subscription, receivedMessage, fragmentHandler);
            assertEquals(expectedMessage, receivedMessage.get());
        }
        try (Publication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID)) {
            publishMessage(bufferOne, bufferTwo, publication);
            pollForMessage(subscription, receivedMessage, fragmentHandler);
            assertEquals(expectedMessage, receivedMessage.get());
        }
    }
}
Also used : FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableReference(org.agrona.collections.MutableReference) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 64 with FragmentHandler

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

the class StopStartSecondSubscriberTest method shouldReceiveMessagesAfterStopStart.

private void shouldReceiveMessagesAfterStopStart(final String channelOne, final int streamOne, final String channelTwo, final int streamTwo) {
    final int numMessages = 1;
    final MutableInteger subscriber2AfterRestartCount = new MutableInteger();
    final AtomicBoolean running = new AtomicBoolean(true);
    final CountDownLatch latch = new CountDownLatch(2);
    final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> subscriber2AfterRestartCount.value++;
    launch(channelOne, streamOne, channelTwo, streamTwo);
    buffer.putInt(0, 1);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    try {
        executor.execute(() -> doPublisherWork(publicationOne, running, latch));
        executor.execute(() -> doPublisherWork(publicationTwo, running, latch));
        final MutableInteger fragmentsReadOne = new MutableInteger();
        final MutableInteger fragmentsReadTwo = new MutableInteger();
        final BooleanSupplier fragmentsReadCondition = () -> fragmentsReadOne.get() >= numMessages && fragmentsReadTwo.get() >= numMessages;
        Tests.executeUntil(fragmentsReadCondition, (i) -> {
            fragmentsReadOne.value += subscriptionOne.poll(fragmentHandlerOne, 1);
            fragmentsReadTwo.value += subscriptionTwo.poll(fragmentHandlerTwo, 1);
            Thread.yield();
        }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(4900));
        assertTrue(subOneCount.get() >= numMessages);
        assertTrue(subTwoCount.get() >= numMessages);
        subscriptionTwo.close();
        fragmentsReadOne.set(0);
        fragmentsReadTwo.set(0);
        subscriptionTwo = subscriberTwo.addSubscription(channelTwo, streamTwo);
        Tests.executeUntil(fragmentsReadCondition, (i) -> {
            fragmentsReadOne.value += subscriptionOne.poll(fragmentHandlerOne, 1);
            fragmentsReadTwo.value += subscriptionTwo.poll(fragmentHandler, 1);
            Thread.yield();
        }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(4900));
        running.set(false);
        latch.await();
        assertTrue(subOneCount.get() >= numMessages * 2, "Expecting subscriberOne to receive messages the entire time");
        assertTrue(subTwoCount.get() >= numMessages, "Expecting subscriberTwo to receive messages before being stopped and started");
        assertTrue(subscriber2AfterRestartCount.get() >= numMessages, "Expecting subscriberTwo to receive messages after being stopped and started");
    } catch (final InterruptedException ex) {
        fail("Interrupted", ex);
    } finally {
        running.set(false);
        executor.shutdownNow();
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) Tests(io.aeron.test.Tests) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) BitUtil(org.agrona.BitUtil) Executors(java.util.concurrent.Executors) BooleanSupplier(java.util.function.BooleanSupplier) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) InterruptAfter(io.aeron.test.InterruptAfter) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions(org.junit.jupiter.api.Assertions) MutableDirectBuffer(org.agrona.MutableDirectBuffer) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) ExecutorService(java.util.concurrent.ExecutorService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) BooleanSupplier(java.util.function.BooleanSupplier)

Example 65 with FragmentHandler

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

the class TimestampingSystemTest method shouldSupportReceiveTimestampsOnMds.

@Test
@InterruptAfter(10)
void shouldSupportReceiveTimestampsOnMds() {
    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.addPublication("aeron:udp?endpoint=localhost:23424", 1000);
        final Publication pub2 = aeron.addPublication("aeron:udp?endpoint=localhost:23425", 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");
        }
        buffer.putLong(0, SENTINEL_VALUE);
        while (0 > pub1.offer(buffer, 0, buffer.capacity())) {
            Tests.yieldingIdle("Failed to offer message");
        }
        while (0 > pub2.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 > mdsSub.poll(fragmentHandler, 1)) {
            Tests.yieldingIdle("Failed to receive message");
        }
        assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
        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)

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