Search in sources :

Example 96 with MutableInteger

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

the class MultiDestinationCastTest method shouldManuallyAddPortDuringActiveStream.

@Test
@InterruptAfter(10)
void shouldManuallyAddPortDuringActiveStream() throws InterruptedException {
    final int numMessagesToSend = MESSAGES_PER_TERM * 3;
    final int numMessageForSub2 = 10;
    final CountingFragmentHandler fragmentHandlerA = new CountingFragmentHandler("fragmentHandlerA");
    final CountingFragmentHandler fragmentHandlerB = new CountingFragmentHandler("fragmentHandlerB");
    final Supplier<String> messageSupplierA = fragmentHandlerA::toString;
    final Supplier<String> messageSupplierB = fragmentHandlerB::toString;
    final CountDownLatch availableImage = new CountDownLatch(1);
    final MutableLong position = new MutableLong(0);
    final MutableInteger messagesSent = new MutableInteger(0);
    final Supplier<String> positionSupplier = () -> "Failed to publish, position: " + position + ", sent: " + messagesSent;
    launch(Tests::onError);
    subscriptionA = clientA.addSubscription(SUB1_MDC_MANUAL_URI, STREAM_ID);
    subscriptionB = clientB.addSubscription(SUB2_MDC_MANUAL_URI, STREAM_ID, (image) -> availableImage.countDown(), null);
    publication = clientA.addPublication(PUB_MDC_MANUAL_URI, STREAM_ID);
    publication.addDestination(SUB1_MDC_MANUAL_URI);
    Tests.awaitConnected(subscriptionA);
    while (messagesSent.value < numMessagesToSend) {
        position.value = publication.offer(buffer, 0, MESSAGE_LENGTH);
        if (0 <= position.value) {
            messagesSent.increment();
        } else {
            Tests.yieldingIdle(positionSupplier);
        }
        subscriptionA.poll(fragmentHandlerA, FRAGMENT_LIMIT);
        if (messagesSent.value > (numMessagesToSend - numMessageForSub2)) {
            subscriptionB.poll(fragmentHandlerB, FRAGMENT_LIMIT);
        }
        if (messagesSent.value == (numMessagesToSend - numMessageForSub2)) {
            final int published = messagesSent.value;
            // then B will receive more than the expected `numMessageForSub2`.
            while (fragmentHandlerA.notDone(published)) {
                if (subscriptionA.poll(fragmentHandlerA, FRAGMENT_LIMIT) <= 0) {
                    Tests.yieldingIdle(messageSupplierA);
                }
            }
            publication.addDestination(SUB2_MDC_MANUAL_URI);
            availableImage.await();
        }
    }
    while (fragmentHandlerA.notDone(numMessagesToSend) || fragmentHandlerB.notDone(numMessageForSub2)) {
        if (fragmentHandlerA.notDone(numMessagesToSend) && subscriptionA.poll(fragmentHandlerA, FRAGMENT_LIMIT) <= 0) {
            Tests.yieldingIdle(messageSupplierA);
        }
        if (fragmentHandlerB.notDone(numMessageForSub2) && subscriptionB.poll(fragmentHandlerB, FRAGMENT_LIMIT) <= 0) {
            Tests.yieldingIdle(messageSupplierB);
        }
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ErrorHandler(org.agrona.ErrorHandler) RegistrationException(io.aeron.exceptions.RegistrationException) io.aeron.test(io.aeron.test) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Supplier(java.util.function.Supplier) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MediaDriver(io.aeron.driver.MediaDriver) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) File(java.io.File) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) Header(io.aeron.logbuffer.Header) MutableLong(org.agrona.collections.MutableLong) ThreadingMode(io.aeron.driver.ThreadingMode) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) FragmentHandler(io.aeron.logbuffer.FragmentHandler) DirectBuffer(org.agrona.DirectBuffer) MutableLong(org.agrona.collections.MutableLong) MutableInteger(org.agrona.collections.MutableInteger) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 97 with MutableInteger

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

the class MultiDriverTest method shouldJoinExistingStreamWithLockStepSendingReceiving.

@Test
@InterruptAfter(10)
void shouldJoinExistingStreamWithLockStepSendingReceiving() throws InterruptedException {
    final int numMessagesToSendPreJoin = NUM_MESSAGES_PER_TERM / 2;
    final int numMessagesToSendPostJoin = NUM_MESSAGES_PER_TERM;
    launch();
    subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
    publication = clientA.addPublication(MULTICAST_URI, STREAM_ID);
    for (int i = 0; i < numMessagesToSendPreJoin; i++) {
        while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
            Tests.yield();
        }
        final MutableInteger fragmentsRead = new MutableInteger();
        Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
            fragmentsRead.value += subscriptionA.poll(fragmentHandlerA, 10);
            Thread.yield();
        }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
    }
    final CountDownLatch newImageLatch = new CountDownLatch(1);
    subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID, (image) -> newImageLatch.countDown(), null);
    newImageLatch.await();
    for (int i = 0; i < numMessagesToSendPostJoin; i++) {
        while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
            Tests.yield();
        }
        final MutableInteger fragmentsRead = new MutableInteger();
        Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
            fragmentsRead.value += subscriptionA.poll(fragmentHandlerA, 10);
            Thread.yield();
        }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
        fragmentsRead.set(0);
        Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
            fragmentsRead.value += subscriptionB.poll(fragmentHandlerB, 10);
            Thread.yield();
        }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
    }
    assertEquals(numMessagesToSendPreJoin + numMessagesToSendPostJoin, fragmentCountA.value);
    assertEquals(numMessagesToSendPostJoin, fragmentCountB.value);
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) Tests(io.aeron.test.Tests) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) IoUtil(org.agrona.IoUtil) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) File(java.io.File) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) InterruptAfter(io.aeron.test.InterruptAfter) CountDownLatch(java.util.concurrent.CountDownLatch) SystemUtil(org.agrona.SystemUtil) AfterEach(org.junit.jupiter.api.AfterEach) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ThreadingMode(io.aeron.driver.ThreadingMode) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MutableInteger(org.agrona.collections.MutableInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 98 with MutableInteger

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

the class PongTest method playPingPong.

@Test
void playPingPong() {
    buffer.putInt(0, 1);
    while (pingPublication.offer(buffer, 0, BitUtil.SIZE_OF_INT) < 0L) {
        Tests.yield();
    }
    final MutableInteger fragmentsRead = new MutableInteger();
    Tests.executeUntil(() -> fragmentsRead.get() > 0, (i) -> {
        fragmentsRead.value += pingSubscription.poll(this::echoPingHandler, 10);
        Thread.yield();
    }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(5900));
    fragmentsRead.set(0);
    Tests.executeUntil(() -> fragmentsRead.get() > 0, (i) -> {
        fragmentsRead.value += pongSubscription.poll(pongHandler, 10);
        Thread.yield();
    }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(5900));
    verify(pongHandler).onFragment(any(DirectBuffer.class), eq(DataHeaderFlyweight.HEADER_LENGTH), eq(BitUtil.SIZE_OF_INT), any(Header.class));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) MutableInteger(org.agrona.collections.MutableInteger) SlowTest(io.aeron.test.SlowTest) Test(org.junit.jupiter.api.Test)

Example 99 with MutableInteger

use of org.agrona.collections.MutableInteger in project zeebe by camunda-cloud.

the class DbVariableState method getVariablesLocalAsDocument.

@Override
public DirectBuffer getVariablesLocalAsDocument(final long scopeKey) {
    writer.wrap(documentResultBuffer, 0);
    writer.reserveMapHeader();
    final MutableInteger variableCount = new MutableInteger();
    visitVariablesLocal(scopeKey, name -> true, (name, value) -> {
        writer.writeString(name.getBuffer());
        writer.writeRaw(value.getValue());
        variableCount.addAndGet(1);
    }, () -> false);
    writer.writeReservedMapHeader(0, variableCount.get());
    resultView.wrap(documentResultBuffer, 0, writer.getOffset());
    return resultView;
}
Also used : MutableInteger(org.agrona.collections.MutableInteger)

Example 100 with MutableInteger

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

the class ManyToOneRingBufferTest method shouldNotReadSingleMessagePartWayThroughWriting.

@Test
public void shouldNotReadSingleMessagePartWayThroughWriting() {
    final long head = 0L;
    final int headIndex = (int) head;
    when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head);
    when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(0);
    final MutableInteger times = new MutableInteger();
    final MessageHandler handler = (msgTypeId, buffer, index, length) -> times.increment();
    final int messagesRead = ringBuffer.read(handler);
    assertThat(messagesRead, is(0));
    assertThat(times.get(), is(0));
    final InOrder inOrder = inOrder(buffer);
    inOrder.verify(buffer, times(1)).getIntVolatile(lengthOffset(headIndex));
    inOrder.verify(buffer, times(0)).setMemory(headIndex, 0, (byte) 0);
    inOrder.verify(buffer, times(0)).putLongOrdered(HEAD_COUNTER_INDEX, headIndex);
}
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) MIN_CAPACITY(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer.MIN_CAPACITY) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) SIZE_OF_LONG(org.agrona.BitUtil.SIZE_OF_LONG) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MutableInteger(org.agrona.collections.MutableInteger) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) InOrder(org.mockito.InOrder) PADDING_MSG_TYPE_ID(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer.PADDING_MSG_TYPE_ID) 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) Assertions(org.junit.jupiter.api.Assertions) Matchers.is(org.hamcrest.Matchers.is) TRUE(java.lang.Boolean.TRUE) 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)

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