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()));
}
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()));
}
Aggregations