Search in sources :

Example 1 with ControlledMessageHandler

use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.

the class ManyToOneRingBufferTest 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 ManyToOneRingBuffer(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) 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) 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)

Example 2 with ControlledMessageHandler

use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.

the class ManyToOneRingBufferTest method shouldAbortOnControlledRead.

@Test
public void shouldAbortOnControlledRead() {
    final String msg = "Hello World";
    final ExpandableArrayBuffer srcBuffer = new ExpandableArrayBuffer();
    final int srcLength = srcBuffer.putStringAscii(0, msg);
    final RingBuffer ringBuffer = new ManyToOneRingBuffer(new UnsafeBuffer(new byte[1024]));
    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
    final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
        assertEquals(MSG_TYPE_ID, msgTypeId);
        assertEquals(HEADER_LENGTH, index);
        assertEquals(srcLength, length);
        assertEquals(msg, buffer.getStringAscii(index));
        return ControlledMessageHandler.Action.ABORT;
    };
    final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler, 1);
    assertEquals(0, messagesRead);
    assertEquals(0, 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) 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) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with ControlledMessageHandler

use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.

the class ManyToOneRingBufferTest method shouldContinueOnControlledRead.

@Test
public void shouldContinueOnControlledRead() {
    final String msg = "Hello World";
    final ExpandableArrayBuffer srcBuffer = new ExpandableArrayBuffer();
    final int srcLength = srcBuffer.putStringAscii(0, msg);
    final RingBuffer ringBuffer = new ManyToOneRingBuffer(new UnsafeBuffer(new byte[1024]));
    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
    final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
        assertEquals(MSG_TYPE_ID, msgTypeId);
        assertEquals(HEADER_LENGTH, index);
        assertEquals(srcLength, length);
        assertEquals(msg, buffer.getStringAscii(index));
        return ControlledMessageHandler.Action.CONTINUE;
    };
    final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler, 1);
    assertEquals(1, 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) 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) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ControlledMessageHandler

use of org.agrona.concurrent.ControlledMessageHandler 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)

Example 5 with ControlledMessageHandler

use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.

the class OneToOneRingBufferTest method shouldContinueOnControlledRead.

@Test
public void shouldContinueOnControlledRead() {
    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]));
    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
    final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
        assertEquals(MSG_TYPE_ID, msgTypeId);
        assertEquals(HEADER_LENGTH, index);
        assertEquals(srcLength, length);
        assertEquals(msg, buffer.getStringAscii(index));
        return ControlledMessageHandler.Action.CONTINUE;
    };
    final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler, 1);
    assertEquals(1, 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) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ControlledMessageHandler (org.agrona.concurrent.ControlledMessageHandler)10 Arrays (java.util.Arrays)8 List (java.util.List)8 IntConsumer (java.util.function.IntConsumer)8 SIZE_OF_LONG (org.agrona.BitUtil.SIZE_OF_LONG)8 BitUtil.align (org.agrona.BitUtil.align)8 ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)8 MutableInteger (org.agrona.collections.MutableInteger)8 MessageHandler (org.agrona.concurrent.MessageHandler)8 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)8 RecordDescriptor (org.agrona.concurrent.ringbuffer.RecordDescriptor)8 INSUFFICIENT_CAPACITY (org.agrona.concurrent.ringbuffer.RingBuffer.INSUFFICIENT_CAPACITY)8 RingBufferDescriptor (org.agrona.concurrent.ringbuffer.RingBufferDescriptor)8 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)8 Matchers.is (org.hamcrest.Matchers.is)8 Assertions (org.junit.jupiter.api.Assertions)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 Test (org.junit.jupiter.api.Test)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 Arguments (org.junit.jupiter.params.provider.Arguments)8