Search in sources :

Example 1 with MIN_CAPACITY

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

the class ManyToOneRingBufferTest 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 ManyToOneRingBuffer ringBuffer = new ManyToOneRingBuffer(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) 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) 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.ManyToOneRingBuffer.MIN_CAPACITY in project agrona by real-logic.

the class ManyToOneRingBufferTest method shouldWriteMessageAfterInsertedPaddingIsConsumedThusMakeEnoughContiguousSpace.

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

Aggregations

TRUE (java.lang.Boolean.TRUE)2 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.ManyToOneRingBuffer.MIN_CAPACITY)2 PADDING_MSG_TYPE_ID (org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer.PADDING_MSG_TYPE_ID)2 RecordDescriptor (org.agrona.concurrent.ringbuffer.RecordDescriptor)2 INSUFFICIENT_CAPACITY (org.agrona.concurrent.ringbuffer.RingBuffer.INSUFFICIENT_CAPACITY)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