Search in sources :

Example 91 with MutableInteger

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

the class PubAndSubTest method shouldReceiveOnlyAfterSendingUpToFlowControlLimit.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldReceiveOnlyAfterSendingUpToFlowControlLimit(final String channel) {
    /*
         * The subscriber will flow control before an entire term buffer. So, send until can't send anymore.
         * Then start up subscriber to drain.
         */
    final int termBufferLength = 64 * 1024;
    final int numMessagesPerTerm = 64;
    final int messageLength = (termBufferLength / numMessagesPerTerm) - HEADER_LENGTH;
    final int maxFails = 10000;
    int messagesSent = 0;
    context.publicationTermBufferLength(termBufferLength);
    launch(channel);
    for (int i = 0; i < numMessagesPerTerm; i++) {
        int offerFails = 0;
        while (publication.offer(buffer, 0, messageLength) < 0L) {
            if (++offerFails > maxFails) {
                break;
            }
            Tests.yield();
        }
        if (offerFails > maxFails) {
            break;
        }
        messagesSent++;
    }
    final MutableInteger fragmentsRead = new MutableInteger();
    final int messagesToReceive = messagesSent;
    Tests.executeUntil(() -> fragmentsRead.value >= messagesToReceive, (j) -> {
        final int fragments = subscription.poll(fragmentHandler, 10);
        if (0 == fragments) {
            Thread.yield();
        }
        fragmentsRead.value += fragments;
    }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
    verify(fragmentHandler, times(messagesToReceive)).onFragment(any(DirectBuffer.class), anyInt(), eq(messageLength), any(Header.class));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) 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)

Example 92 with MutableInteger

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

Example 93 with MutableInteger

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

the class BufferClaimMessageTest method shouldReceivePublishedMessageWithInterleavedAbort.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldReceivePublishedMessageWithInterleavedAbort(final String channel) {
    final MutableInteger fragmentCount = new MutableInteger();
    final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> fragmentCount.value++;
    final BufferClaim bufferClaim = new BufferClaim();
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocate(MESSAGE_LENGTH));
    try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
        Publication publication = aeron.addPublication(channel, STREAM_ID)) {
        publishMessage(srcBuffer, publication);
        while (publication.tryClaim(MESSAGE_LENGTH, bufferClaim) < 0L) {
            Tests.yield();
        }
        publishMessage(srcBuffer, publication);
        bufferClaim.abort();
        final int expectedNumberOfFragments = 2;
        int numFragments = 0;
        do {
            final int fragments = subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT);
            if (0 == fragments) {
                Tests.yield();
            }
            numFragments += fragments;
        } while (numFragments < expectedNumberOfFragments);
        assertEquals(expectedNumberOfFragments, fragmentCount.value);
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) MutableBoolean(org.agrona.collections.MutableBoolean) Arrays(java.util.Arrays) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) ByteBuffer(java.nio.ByteBuffer) 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) 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)

Example 94 with MutableInteger

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

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

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