Search in sources :

Example 51 with MutableInteger

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

the class PubAndSubTest method shouldContinueAfterBufferRolloverBatched.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldContinueAfterBufferRolloverBatched(final String channel) {
    final int termBufferLength = 64 * 1024;
    final int numBatchesPerTerm = 4;
    final int numMessagesPerBatch = 16;
    final int numMessagesInTermBuffer = numMessagesPerBatch * numBatchesPerTerm;
    final int messageLength = (termBufferLength / numMessagesInTermBuffer) - HEADER_LENGTH;
    final int numMessagesToSend = numMessagesInTermBuffer + 1;
    context.publicationTermBufferLength(termBufferLength);
    launch(channel);
    for (int i = 0; i < numBatchesPerTerm; i++) {
        for (int j = 0; j < numMessagesPerBatch; j++) {
            while (publication.offer(buffer, 0, messageLength) < 0L) {
                Tests.yield();
            }
        }
        pollForBatch(numMessagesPerBatch);
    }
    while (publication.offer(buffer, 0, messageLength) < 0L) {
        Tests.yield();
    }
    final MutableInteger fragmentsRead = new MutableInteger();
    Tests.executeUntil(() -> fragmentsRead.value > 0, (j) -> {
        final int fragments = subscription.poll(fragmentHandler, 10);
        if (0 == fragments) {
            Thread.yield();
        }
        fragmentsRead.value += fragments;
    }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(900));
    verify(fragmentHandler, times(numMessagesToSend)).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 52 with MutableInteger

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

the class PubAndSubTest method shouldFragmentExactMessageLengthsCorrectly.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldFragmentExactMessageLengthsCorrectly(final String channel) {
    final int termBufferLength = 64 * 1024;
    final int numFragmentsPerMessage = 2;
    final int mtuLength = context.mtuLength();
    final int frameLength = mtuLength - HEADER_LENGTH;
    final int messageLength = frameLength * numFragmentsPerMessage;
    final int numMessagesToSend = 2;
    final int numFramesToExpect = numMessagesToSend * numFragmentsPerMessage;
    context.publicationTermBufferLength(termBufferLength);
    launch(channel);
    for (int i = 0; i < numMessagesToSend; i++) {
        while (publication.offer(buffer, 0, messageLength) < 0L) {
            Tests.yield();
        }
    }
    final MutableInteger fragmentsRead = new MutableInteger();
    Tests.executeUntil(() -> fragmentsRead.value > numFramesToExpect, (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(numFramesToExpect)).onFragment(any(DirectBuffer.class), anyInt(), eq(frameLength), 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 53 with MutableInteger

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

the class RemoteEchoTest method sendAndReceiveRandomData.

private void sendAndReceiveRandomData(final List<Publication> pubs, final List<Subscription> subs) {
    assertEquals(pubs.size(), subs.size());
    final List<ExpandableArrayBuffer> receivedDataBuffers = new ArrayList<>();
    final List<MutableInteger> sentDataCounts = new ArrayList<>();
    final List<MutableInteger> receivedDataCounts = new ArrayList<>();
    final List<FragmentHandler> handlers = new ArrayList<>();
    for (int i = 0; i < pubs.size(); i++) {
        final ExpandableArrayBuffer receivedData = new ExpandableArrayBuffer(SOURCE_DATA_LENGTH);
        final MutableInteger sentBytes = new MutableInteger(0);
        final MutableInteger recvBytes = new MutableInteger(0);
        final FragmentHandler handler = (buffer, offset, length, header) -> {
            receivedData.putBytes(recvBytes.get(), buffer, offset, length);
            recvBytes.addAndGet(length);
        };
        receivedDataBuffers.add(receivedData);
        sentDataCounts.add(sentBytes);
        receivedDataCounts.add(recvBytes);
        handlers.add(handler);
    }
    while (dataIsPending(receivedDataCounts, SOURCE_DATA_LENGTH)) {
        for (int i = 0; i < pubs.size(); i++) {
            final Publication pub = pubs.get(i);
            final Subscription sub = subs.get(i);
            final MutableInteger sentBytes = sentDataCounts.get(i);
            final FragmentHandler handler = handlers.get(i);
            if (sentBytes.get() < SOURCE_DATA_LENGTH) {
                final int randomLength = randomWatcher.random().nextInt(pub.maxMessageLength());
                final int toSend = min(SOURCE_DATA_LENGTH - sentBytes.get(), randomLength);
                if (pub.offer(sourceData, sentBytes.get(), toSend) > 0) {
                    sentBytes.addAndGet(toSend);
                }
                Tests.yield();
            }
            if (sub.poll(handler, 10) <= 0) {
                Tests.yield();
            }
        }
    }
    for (final ExpandableArrayBuffer receivedData : receivedDataBuffers) {
        assertEquals(0, sourceData.compareTo(receivedData));
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ProvisioningConstants(io.aeron.samples.echo.api.ProvisioningConstants) io.aeron.test(io.aeron.test) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) TimeoutException(java.util.concurrent.TimeoutException) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) InetAddress(java.net.InetAddress) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) JMXConnectorFactory(javax.management.remote.JMXConnectorFactory) NetworkUtil(io.aeron.driver.media.NetworkUtil) ManagementFactory(java.lang.management.ManagementFactory) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MediaDriver(io.aeron.driver.MediaDriver) EchoMonitorMBean(io.aeron.samples.echo.api.EchoMonitorMBean) IO_AERON_TYPE_PROVISIONING_NAME_TESTING(io.aeron.samples.echo.api.ProvisioningConstants.IO_AERON_TYPE_PROVISIONING_NAME_TESTING) javax.management(javax.management) IOException(java.io.IOException) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) JMXServiceURL(javax.management.remote.JMXServiceURL) Math.min(java.lang.Math.min) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ProvisioningServerMain(io.aeron.samples.echo.ProvisioningServerMain) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ProvisioningMBean(io.aeron.samples.echo.api.ProvisioningMBean) FragmentHandler(io.aeron.logbuffer.FragmentHandler) JMXConnector(javax.management.remote.JMXConnector) DirectBuffer(org.agrona.DirectBuffer) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) ArrayList(java.util.ArrayList) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer)

Example 54 with MutableInteger

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

the class RemoteEchoTest method sendAndReceiveRandomData.

private void sendAndReceiveRandomData(final Publication pub, final Subscription sub) {
    final ExpandableArrayBuffer receivedData = new ExpandableArrayBuffer(SOURCE_DATA_LENGTH);
    final MutableInteger sentBytes = new MutableInteger(0);
    final MutableInteger recvBytes = new MutableInteger(0);
    final FragmentHandler handler = (buffer, offset, length, header) -> {
        receivedData.putBytes(recvBytes.get(), buffer, offset, length);
        recvBytes.addAndGet(length);
    };
    while (recvBytes.get() < SOURCE_DATA_LENGTH) {
        if (sentBytes.get() < SOURCE_DATA_LENGTH) {
            final int randomLength = randomWatcher.random().nextInt(pub.maxMessageLength());
            final int toSend = min(SOURCE_DATA_LENGTH - sentBytes.get(), randomLength);
            if (pub.offer(sourceData, sentBytes.get(), toSend) > 0) {
                sentBytes.addAndGet(toSend);
            }
            Tests.yield();
        }
        if (sub.poll(handler, 10) <= 0) {
            Tests.yield();
        }
    }
    assertEquals(0, sourceData.compareTo(receivedData));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ProvisioningConstants(io.aeron.samples.echo.api.ProvisioningConstants) io.aeron.test(io.aeron.test) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) TimeoutException(java.util.concurrent.TimeoutException) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) InetAddress(java.net.InetAddress) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) JMXConnectorFactory(javax.management.remote.JMXConnectorFactory) NetworkUtil(io.aeron.driver.media.NetworkUtil) ManagementFactory(java.lang.management.ManagementFactory) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MediaDriver(io.aeron.driver.MediaDriver) EchoMonitorMBean(io.aeron.samples.echo.api.EchoMonitorMBean) IO_AERON_TYPE_PROVISIONING_NAME_TESTING(io.aeron.samples.echo.api.ProvisioningConstants.IO_AERON_TYPE_PROVISIONING_NAME_TESTING) javax.management(javax.management) IOException(java.io.IOException) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) JMXServiceURL(javax.management.remote.JMXServiceURL) Math.min(java.lang.Math.min) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ProvisioningServerMain(io.aeron.samples.echo.ProvisioningServerMain) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ProvisioningMBean(io.aeron.samples.echo.api.ProvisioningMBean) FragmentHandler(io.aeron.logbuffer.FragmentHandler) JMXConnector(javax.management.remote.JMXConnector) DirectBuffer(org.agrona.DirectBuffer) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer)

Example 55 with MutableInteger

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

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