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