Search in sources :

Example 71 with MutableInteger

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

the class OneToOneRingBufferTest method shouldLimitReadOfMessages.

@Test
public void shouldLimitReadOfMessages() {
    final int msgLength = 16;
    final int recordLength = HEADER_LENGTH + msgLength;
    final int alignedRecordLength = align(recordLength, ALIGNMENT);
    final long head = 0L;
    final int headIndex = (int) head;
    when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head);
    when(buffer.getInt(typeOffset(headIndex))).thenReturn(MSG_TYPE_ID);
    when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(recordLength);
    final MutableInteger times = new MutableInteger();
    final MessageHandler handler = (msgTypeId, buffer, index, length) -> times.increment();
    final int limit = 1;
    final int messagesRead = ringBuffer.read(handler, limit);
    assertThat(messagesRead, is(1));
    assertThat(times.get(), is(1));
    final InOrder inOrder = inOrder(buffer);
    inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, head + alignedRecordLength);
    inOrder.verify(buffer, times(0)).setMemory(anyInt(), anyInt(), anyByte());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) RingBufferDescriptor(org.agrona.concurrent.ringbuffer.RingBufferDescriptor) IntConsumer(java.util.function.IntConsumer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) RecordDescriptor(org.agrona.concurrent.ringbuffer.RecordDescriptor) MessageHandler(org.agrona.concurrent.MessageHandler) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) SIZE_OF_LONG(org.agrona.BitUtil.SIZE_OF_LONG) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PADDING_MSG_TYPE_ID(org.agrona.concurrent.ringbuffer.RingBuffer.PADDING_MSG_TYPE_ID) MutableInteger(org.agrona.collections.MutableInteger) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) InOrder(org.mockito.InOrder) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Arguments(org.junit.jupiter.params.provider.Arguments) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) INSUFFICIENT_CAPACITY(org.agrona.concurrent.ringbuffer.RingBuffer.INSUFFICIENT_CAPACITY) MIN_CAPACITY(org.agrona.concurrent.ringbuffer.OneToOneRingBuffer.MIN_CAPACITY) Assertions(org.junit.jupiter.api.Assertions) Matchers.is(org.hamcrest.Matchers.is) BitUtil.align(org.agrona.BitUtil.align) InOrder(org.mockito.InOrder) MessageHandler(org.agrona.concurrent.MessageHandler) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) MutableInteger(org.agrona.collections.MutableInteger) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 72 with MutableInteger

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

the class BasicArchiveTest method consume.

private 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)) {
            SystemTest.checkInterruptedStatus();
            Thread.yield();
        }
    }
    assertThat(received.get(), is(count));
}
Also used : FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger)

Example 73 with MutableInteger

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

the class MultiDestinationCastTest method shouldSendToTwoPortsWithDynamicSingleDriver.

@Test(timeout = 10_000)
public void shouldSendToTwoPortsWithDynamicSingleDriver() {
    final int numMessagesToSend = NUM_MESSAGES_PER_TERM * 3;
    launch();
    publication = clientA.addPublication(PUB_MDC_DYNAMIC_URI, STREAM_ID);
    subscriptionA = clientA.addSubscription(SUB1_MDC_DYNAMIC_URI, STREAM_ID);
    subscriptionB = clientA.addSubscription(SUB2_MDC_DYNAMIC_URI, STREAM_ID);
    subscriptionC = clientA.addSubscription(SUB3_MDC_DYNAMIC_URI, STREAM_ID);
    while (!subscriptionA.isConnected() || !subscriptionB.isConnected() || !subscriptionC.isConnected()) {
        SystemTest.checkInterruptedStatus();
        Thread.yield();
    }
    for (int i = 0; i < numMessagesToSend; i++) {
        while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
            SystemTest.checkInterruptedStatus();
            Thread.yield();
        }
        final MutableInteger fragmentsRead = new MutableInteger();
        pollForFragment(subscriptionA, fragmentHandlerA, fragmentsRead);
        fragmentsRead.set(0);
        pollForFragment(subscriptionB, fragmentHandlerB, fragmentsRead);
        fragmentsRead.set(0);
        pollForFragment(subscriptionC, fragmentHandlerC, fragmentsRead);
    }
    verifyFragments(fragmentHandlerA, numMessagesToSend);
    verifyFragments(fragmentHandlerB, numMessagesToSend);
    verifyFragments(fragmentHandlerC, numMessagesToSend);
}
Also used : MutableInteger(org.agrona.collections.MutableInteger) Test(org.junit.Test)

Example 74 with MutableInteger

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

the class FlowControlStrategiesTest method shouldTimeoutImageWhenBehindForTooLongWithMaxMulticastFlowControlStrategy.

@Test(timeout = 10_000)
public void shouldTimeoutImageWhenBehindForTooLongWithMaxMulticastFlowControlStrategy() throws Exception {
    final int numMessagesToSend = NUM_MESSAGES_PER_TERM * 3;
    final CountDownLatch unavailableCountDownLatch = new CountDownLatch(1);
    final CountDownLatch availableCountDownLatch = new CountDownLatch(2);
    driverBContext.imageLivenessTimeoutNs(TimeUnit.MILLISECONDS.toNanos(500));
    driverAContext.multicastFlowControlSupplier((udpChannel, streamId, registrationId) -> new MaxMulticastFlowControl());
    launch();
    publication = clientA.addPublication(MULTICAST_URI, STREAM_ID);
    subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
    subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID, (image) -> availableCountDownLatch.countDown(), (image) -> unavailableCountDownLatch.countDown());
    while (!subscriptionA.isConnected() || !subscriptionB.isConnected()) {
        SystemTest.checkInterruptedStatus();
        Thread.yield();
    }
    for (int i = 0; i < numMessagesToSend; i++) {
        while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
            Thread.yield();
        }
        // A keeps up
        final MutableInteger fragmentsRead = new MutableInteger();
        SystemTest.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) {
            SystemTest.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
                fragmentsRead.value += subscriptionB.poll(fragmentHandlerB, 1);
                Thread.yield();
            }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
        }
    }
    unavailableCountDownLatch.await();
    availableCountDownLatch.await();
    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 : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) io.aeron.driver(io.aeron.driver) Test(org.junit.Test) UUID(java.util.UUID) IoUtil(org.agrona.IoUtil) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) Header(io.aeron.logbuffer.Header) After(org.junit.After) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) DirectBuffer(org.agrona.DirectBuffer) DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) MutableInteger(org.agrona.collections.MutableInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 75 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 String prefix) {
    final MutableInteger received = new MutableInteger(startIndex);
    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 < (startIndex + count)) {
        if (0 == subscription.poll(fragmentHandler, FRAGMENT_LIMIT)) {
            SystemTest.checkInterruptedStatus();
            Thread.yield();
        }
    }
    assertThat(received.get(), is(startIndex + count));
}
Also used : FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger)

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