Search in sources :

Example 66 with MutableInteger

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

the class ManyToOneRingBufferTest 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)).setMemory(headIndex, alignedRecordLength, (byte) 0);
    inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, head + alignedRecordLength);
}
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)

Example 67 with MutableInteger

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

the class ManyToOneRingBufferTest method shouldReadTwoMessages.

@Test
public void shouldReadTwoMessages() {
    final int msgLength = 16;
    final int recordLength = HEADER_LENGTH + msgLength;
    final int alignedRecordLength = align(recordLength, ALIGNMENT);
    final long tail = alignedRecordLength * 2L;
    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);
    when(buffer.getInt(typeOffset(headIndex + alignedRecordLength))).thenReturn(MSG_TYPE_ID);
    when(buffer.getIntVolatile(lengthOffset(headIndex + alignedRecordLength))).thenReturn(recordLength);
    final MutableInteger times = new MutableInteger();
    final MessageHandler handler = (msgTypeId, buffer, index, length) -> times.increment();
    final int messagesRead = ringBuffer.read(handler);
    assertThat(messagesRead, is(2));
    assertThat(times.get(), is(2));
    final InOrder inOrder = inOrder(buffer);
    inOrder.verify(buffer, times(1)).setMemory(headIndex, alignedRecordLength * 2, (byte) 0);
    inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, tail);
}
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)

Example 68 with MutableInteger

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

the class OneToOneRingBufferTest method shouldCopeWithExceptionFromHandler.

@Test
public void shouldCopeWithExceptionFromHandler() {
    final int msgLength = 16;
    final int recordLength = HEADER_LENGTH + msgLength;
    final int alignedRecordLength = align(recordLength, ALIGNMENT);
    final long tail = alignedRecordLength * 2L;
    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);
    when(buffer.getInt(typeOffset(headIndex + alignedRecordLength))).thenReturn(MSG_TYPE_ID);
    when(buffer.getIntVolatile(lengthOffset(headIndex + alignedRecordLength))).thenReturn(recordLength);
    final MutableInteger times = new MutableInteger();
    final MessageHandler handler = (msgTypeId, buffer, index, length) -> {
        if (times.incrementAndGet() == 2) {
            throw new RuntimeException();
        }
    };
    try {
        ringBuffer.read(handler);
    } catch (final RuntimeException ignore) {
        assertThat(times.get(), is(2));
        final InOrder inOrder = inOrder(buffer);
        inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, tail);
        inOrder.verify(buffer, times(0)).setMemory(anyInt(), anyInt(), anyByte());
        return;
    }
    fail("Should have thrown exception");
}
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 69 with MutableInteger

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

the class OneToOneRingBufferTest method shouldReadTwoMessages.

@Test
public void shouldReadTwoMessages() {
    final int msgLength = 16;
    final int recordLength = HEADER_LENGTH + msgLength;
    final int alignedRecordLength = align(recordLength, ALIGNMENT);
    final long tail = alignedRecordLength * 2L;
    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);
    when(buffer.getInt(typeOffset(headIndex + alignedRecordLength))).thenReturn(MSG_TYPE_ID);
    when(buffer.getIntVolatile(lengthOffset(headIndex + alignedRecordLength))).thenReturn(recordLength);
    final MutableInteger times = new MutableInteger();
    final MessageHandler handler = (msgTypeId, buffer, index, length) -> times.increment();
    final int messagesRead = ringBuffer.read(handler);
    assertThat(messagesRead, is(2));
    assertThat(times.get(), is(2));
    final InOrder inOrder = inOrder(buffer);
    inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, tail);
    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 70 with MutableInteger

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

the class OneToOneRingBufferTest method shouldCommitOnEachMessage.

@Test
public void shouldCommitOnEachMessage() {
    final String msg = "Hello World";
    final ExpandableArrayBuffer srcBuffer = new ExpandableArrayBuffer();
    final int srcLength = srcBuffer.putStringAscii(0, msg);
    final RingBuffer ringBuffer = new OneToOneRingBuffer(new UnsafeBuffer(new byte[1024]));
    final MutableInteger counter = new MutableInteger();
    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
    final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
        if (0 == counter.getAndIncrement()) {
            assertEquals(0L, ringBuffer.consumerPosition());
        } else {
            assertEquals(ringBuffer.producerPosition() / 2, ringBuffer.consumerPosition());
        }
        return ControlledMessageHandler.Action.COMMIT;
    };
    final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler);
    assertEquals(2, counter.get());
    assertEquals(2, messagesRead);
    assertEquals(ringBuffer.producerPosition(), ringBuffer.consumerPosition());
}
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) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) MutableInteger(org.agrona.collections.MutableInteger) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) 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