Search in sources :

Example 86 with MutableInteger

use of org.agrona.collections.MutableInteger 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 87 with MutableInteger

use of org.agrona.collections.MutableInteger 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 88 with MutableInteger

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

the class SpySimulatedConnectionTest method shouldSimulateConnectionWithNoNetworkSubscriptions.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldSimulateConnectionWithNoNetworkSubscriptions(final String channel) {
    final int messagesToSend = NUM_MESSAGES_PER_TERM * 3;
    driverContext.publicationConnectionTimeoutNs(TimeUnit.MILLISECONDS.toNanos(250)).timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(100)).spiesSimulateConnection(true);
    launch();
    spy = client.addSubscription(spyForChannel(channel), STREAM_ID);
    publication = client.addPublication(channel, STREAM_ID);
    while (!spy.isConnected() || !publication.isConnected()) {
        Tests.yield();
    }
    for (int i = 0; i < messagesToSend; i++) {
        while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
            Tests.yield();
        }
        final MutableInteger fragmentsRead = new MutableInteger();
        Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
            final int fragments = spy.poll(fragmentHandlerSpy, 10);
            if (0 == fragments) {
                Thread.yield();
            }
            fragmentsRead.value += fragments;
        }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
    }
    assertEquals(messagesToSend, fragmentCountSpy.value);
}
Also used : MutableInteger(org.agrona.collections.MutableInteger) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 89 with MutableInteger

use of org.agrona.collections.MutableInteger 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 90 with MutableInteger

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

the class PubAndSubTest method shouldContinueAfterRolloverWithMinimalPaddingHeader.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldContinueAfterRolloverWithMinimalPaddingHeader(final String channel) {
    final int termBufferLength = 64 * 1024;
    final int termBufferLengthMinusPaddingHeader = termBufferLength - HEADER_LENGTH;
    final int num1kMessagesInTermBuffer = 63;
    final int lastMessageLength = termBufferLengthMinusPaddingHeader - (num1kMessagesInTermBuffer * 1024) - HEADER_LENGTH;
    final int messageLength = 1024 - HEADER_LENGTH;
    context.publicationTermBufferLength(termBufferLength);
    launch(channel);
    // lock step reception until we get to within 8 messages of the end
    for (int i = 0; i < num1kMessagesInTermBuffer - 7; i++) {
        while (publication.offer(buffer, 0, messageLength) < 0L) {
            Tests.yield();
        }
        pollForFragment();
    }
    for (int i = 7; i > 0; i--) {
        while (publication.offer(buffer, 0, messageLength) < 0L) {
            Tests.yield();
        }
    }
    // small enough to leave room for padding that is just a header
    while (publication.offer(buffer, 0, lastMessageLength) < 0L) {
        Tests.yield();
    }
    // no roll over
    while (publication.offer(buffer, 0, messageLength) < 0L) {
        Tests.yield();
    }
    final MutableInteger fragmentsRead = new MutableInteger();
    Tests.executeUntil(() -> fragmentsRead.value == 9, (j) -> {
        final int fragments = subscription.poll(fragmentHandler, 10);
        if (0 == fragments) {
            Thread.yield();
        }
        fragmentsRead.value += fragments;
    }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
    final InOrder inOrder = inOrder(fragmentHandler);
    inOrder.verify(fragmentHandler, times(num1kMessagesInTermBuffer)).onFragment(any(DirectBuffer.class), anyInt(), eq(messageLength), any(Header.class));
    inOrder.verify(fragmentHandler, times(1)).onFragment(any(DirectBuffer.class), anyInt(), eq(lastMessageLength), any(Header.class));
    inOrder.verify(fragmentHandler, times(1)).onFragment(any(DirectBuffer.class), anyInt(), eq(messageLength), any(Header.class));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) InOrder(org.mockito.InOrder) Header(io.aeron.logbuffer.Header) MutableInteger(org.agrona.collections.MutableInteger) DebugSendChannelEndpoint(io.aeron.driver.ext.DebugSendChannelEndpoint) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

MutableInteger (org.agrona.collections.MutableInteger)151 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)76 DirectBuffer (org.agrona.DirectBuffer)64 Test (org.junit.jupiter.api.Test)61 InterruptAfter (io.aeron.test.InterruptAfter)52 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)42 MethodSource (org.junit.jupiter.params.provider.MethodSource)38 MediaDriver (io.aeron.driver.MediaDriver)34 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)34 Test (org.junit.Test)33 ByteBuffer (java.nio.ByteBuffer)32 CloseHelper (org.agrona.CloseHelper)32 Array32FW (io.aklivity.zilla.specs.binding.kafka.internal.types.Array32FW)28 HEADER (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaConditionType.HEADER)28 HEADERS (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaConditionType.HEADERS)28 KEY (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaConditionType.KEY)28 NOT (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaConditionType.NOT)28 KafkaDeltaType (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaDeltaType)28 KafkaOffsetFW (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaOffsetFW)28 KafkaSkip (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaSkip)28