Search in sources :

Example 1 with MIN_CAPACITY

use of org.agrona.concurrent.ringbuffer.OneToOneRingBuffer.MIN_CAPACITY in project agrona by real-logic.

the class OneToOneRingBufferTest method shouldWriteMessageAfterInsertedPaddingIsConsumedThusMakeEnoughContiguousSpace.

@Test
void shouldWriteMessageAfterInsertedPaddingIsConsumedThusMakeEnoughContiguousSpace() {
    final int msgType = 42;
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[MIN_CAPACITY]);
    final OneToOneRingBuffer ringBuffer = new OneToOneRingBuffer(new UnsafeBuffer(new byte[MIN_CAPACITY * 2 + TRAILER_LENGTH]));
    srcBuffer.putLong(0, Long.MAX_VALUE);
    assertTrue(ringBuffer.write(msgType, srcBuffer, 0, SIZE_OF_LONG));
    // an empty message
    assertTrue(ringBuffer.write(msgType, srcBuffer, 0, 0));
    srcBuffer.putLong(0, Long.MIN_VALUE);
    // not enough space in the buffer
    assertFalse(ringBuffer.write(msgType, srcBuffer, 0, SIZE_OF_LONG));
    // consume one message and move head
    assertEquals(1, ringBuffer.read((msgTypeId, buffer, index, length) -> assertEquals(Long.MAX_VALUE, buffer.getLong(index)), 1));
    // not enough contiguous space --> insert padding
    assertFalse(ringBuffer.write(msgType, srcBuffer, 0, SIZE_OF_LONG));
    // consume the empty message and move head
    assertEquals(1, ringBuffer.read((msgTypeId, buffer, index, length) -> assertEquals(0, length)));
    // message fits
    assertTrue(ringBuffer.write(msgType, srcBuffer, 0, SIZE_OF_LONG));
    assertEquals(1, ringBuffer.read((msgTypeId, buffer, index, length) -> assertEquals(Long.MIN_VALUE, buffer.getLong(index))));
    assertEquals(0, ringBuffer.read((msgTypeId, buffer, index, length) -> fail()));
}
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) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with MIN_CAPACITY

use of org.agrona.concurrent.ringbuffer.OneToOneRingBuffer.MIN_CAPACITY in project agrona by real-logic.

the class OneToOneRingBufferTest method shouldAllowWritingEmptyMessagesWhenCapacityIsMinimal.

@Test
void shouldAllowWritingEmptyMessagesWhenCapacityIsMinimal() {
    final int msgType = 13;
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[MIN_CAPACITY]);
    srcBuffer.putLong(0, Long.MAX_VALUE);
    final OneToOneRingBuffer ringBuffer = new OneToOneRingBuffer(new UnsafeBuffer(new byte[MIN_CAPACITY + TRAILER_LENGTH]));
    assertTrue(ringBuffer.write(msgType, srcBuffer, 0, 0));
    // ring buffer is full so the second write should fail
    assertFalse(ringBuffer.write(msgType, srcBuffer, 0, 0));
    // write will succeed after the read
    assertEquals(1, ringBuffer.read((msgTypeId, buffer, index, length) -> assertEquals(0, length)));
    assertTrue(ringBuffer.write(msgType, srcBuffer, 0, 0));
    assertEquals(1, ringBuffer.read((msgTypeId, buffer, index, length) -> assertEquals(0, length)));
    assertEquals(0, ringBuffer.read((msgTypeId, buffer, index, length) -> fail()));
}
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) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Arrays (java.util.Arrays)2 List (java.util.List)2 IntConsumer (java.util.function.IntConsumer)2 SIZE_OF_LONG (org.agrona.BitUtil.SIZE_OF_LONG)2 BitUtil.align (org.agrona.BitUtil.align)2 ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)2 MutableInteger (org.agrona.collections.MutableInteger)2 ControlledMessageHandler (org.agrona.concurrent.ControlledMessageHandler)2 MessageHandler (org.agrona.concurrent.MessageHandler)2 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)2 MIN_CAPACITY (org.agrona.concurrent.ringbuffer.OneToOneRingBuffer.MIN_CAPACITY)2 RecordDescriptor (org.agrona.concurrent.ringbuffer.RecordDescriptor)2 INSUFFICIENT_CAPACITY (org.agrona.concurrent.ringbuffer.RingBuffer.INSUFFICIENT_CAPACITY)2 PADDING_MSG_TYPE_ID (org.agrona.concurrent.ringbuffer.RingBuffer.PADDING_MSG_TYPE_ID)2 RingBufferDescriptor (org.agrona.concurrent.ringbuffer.RingBufferDescriptor)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Matchers.is (org.hamcrest.Matchers.is)2 Assertions (org.junit.jupiter.api.Assertions)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2