Search in sources :

Example 56 with MutableInteger

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

the class ExclusivePublicationTest method shouldOfferTwoBuffersFromIndependentExclusivePublications.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldOfferTwoBuffersFromIndependentExclusivePublications(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 + 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);
        for (int i = 0; i < expectedNumberOfFragments; i += 2) {
            while (publicationOne.offer(pubOneHeader, 0, SIZE_OF_INT, pubOnePayload, 0, MESSAGE_LENGTH) < 0L) {
                Tests.yield();
                totalFragmentsRead += pollFragments(subscription, fragmentHandler);
            }
            while (publicationTwo.offer(pubTwoHeader, 0, SIZE_OF_INT, pubTwoPayload, 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) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 57 with MutableInteger

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

the class MaxFlowControlStrategySystemTest method shouldTimeoutImageWhenBehindForTooLongWithMaxMulticastFlowControlStrategy.

@Test
@InterruptAfter(10)
void shouldTimeoutImageWhenBehindForTooLongWithMaxMulticastFlowControlStrategy() {
    final int numMessagesToSend = NUM_MESSAGES_PER_TERM * 3;
    driverBContext.imageLivenessTimeoutNs(TimeUnit.MILLISECONDS.toNanos(500));
    driverAContext.multicastFlowControlSupplier(new MaxMulticastFlowControlSupplier());
    launch();
    subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
    subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID);
    publication = clientA.addPublication(MULTICAST_URI, STREAM_ID);
    while (!subscriptionA.isConnected() || !subscriptionB.isConnected() || !publication.isConnected()) {
        Tests.yield();
    }
    final MutableInteger fragmentsRead = new MutableInteger();
    for (int i = 0; i < numMessagesToSend; i++) {
        while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
            Tests.yield();
        }
        fragmentsRead.set(0);
        // A keeps up
        Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
            fragmentsRead.value += subscriptionA.poll(fragmentHandlerA, 10);
            Thread.yield();
        }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
        fragmentsRead.set(0);
        // B receives slowly and eventually can't keep up
        if (i % 10 == 0) {
            Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
                fragmentsRead.value += subscriptionB.poll(fragmentHandlerB, 1);
                Thread.yield();
            }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
        }
    }
    verify(fragmentHandlerA, times(numMessagesToSend)).onFragment(any(DirectBuffer.class), anyInt(), eq(MESSAGE_LENGTH), any(Header.class));
    verify(fragmentHandlerB, atMost(numMessagesToSend - 1)).onFragment(any(DirectBuffer.class), anyInt(), eq(MESSAGE_LENGTH), any(Header.class));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) MutableInteger(org.agrona.collections.MutableInteger) MaxMulticastFlowControlSupplier(io.aeron.driver.MaxMulticastFlowControlSupplier) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 58 with MutableInteger

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

the class ArchiveTool method verify.

static boolean verify(final PrintStream out, final File archiveDir, final Set<VerifyOption> options, final Checksum checksum, final EpochClock epochClock, final ActionConfirmation<File> truncateOnPageStraddle) {
    try (Catalog catalog = openCatalogReadWrite(archiveDir, epochClock, MIN_CAPACITY, checksum, null)) {
        final MutableInteger errorCount = new MutableInteger();
        catalog.forEach(createVerifyEntryProcessor(out, archiveDir, options, catalog, checksum, epochClock, errorCount, truncateOnPageStraddle));
        return errorCount.get() == 0;
    }
}
Also used : MutableInteger(org.agrona.collections.MutableInteger) Catalog(io.aeron.archive.Catalog)

Example 59 with MutableInteger

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

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

the class SelectorAndTransportTest method shouldSendMultipleDataFramesPerDatagramUnicastFromSourceToReceiver.

@Test
@InterruptAfter(10)
public void shouldSendMultipleDataFramesPerDatagramUnicastFromSourceToReceiver() {
    final MutableInteger dataHeadersReceived = new MutableInteger(0);
    doAnswer((invocation) -> {
        dataHeadersReceived.value++;
        return null;
    }).when(mockDispatcher).onDataPacket(any(ReceiveChannelEndpoint.class), any(DataHeaderFlyweight.class), any(UnsafeBuffer.class), anyInt(), any(InetSocketAddress.class), anyInt());
    receiveChannelEndpoint = new ReceiveChannelEndpoint(RCV_DST, mockDispatcher, mockReceiveStatusIndicator, context);
    sendChannelEndpoint = new SendChannelEndpoint(SRC_DST, mockSendStatusIndicator, context);
    receiveChannelEndpoint.openDatagramChannel(mockReceiveStatusIndicator);
    receiveChannelEndpoint.registerForRead(dataTransportPoller);
    sendChannelEndpoint.openDatagramChannel(mockSendStatusIndicator);
    sendChannelEndpoint.registerForRead(controlTransportPoller);
    encodeDataHeader.wrap(buffer);
    encodeDataHeader.version(HeaderFlyweight.CURRENT_VERSION).flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS).headerType(HeaderFlyweight.HDR_TYPE_DATA).frameLength(FRAME_LENGTH);
    encodeDataHeader.sessionId(SESSION_ID).streamId(STREAM_ID).termId(TERM_ID);
    final int alignedFrameLength = BitUtil.align(FRAME_LENGTH, FrameDescriptor.FRAME_ALIGNMENT);
    encodeDataHeader.wrap(buffer, alignedFrameLength, buffer.capacity() - alignedFrameLength);
    encodeDataHeader.version(HeaderFlyweight.CURRENT_VERSION).flags(DataHeaderFlyweight.BEGIN_AND_END_FLAGS).headerType(HeaderFlyweight.HDR_TYPE_DATA).frameLength(24);
    encodeDataHeader.sessionId(SESSION_ID).streamId(STREAM_ID).termId(TERM_ID);
    byteBuffer.position(0).limit(2 * alignedFrameLength);
    processLoop(dataTransportPoller, 5);
    sendChannelEndpoint.send(byteBuffer);
    while (dataHeadersReceived.get() < 1) {
        processLoop(dataTransportPoller, 1);
    }
    assertEquals(1, dataHeadersReceived.get());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) MutableInteger(org.agrona.collections.MutableInteger) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

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