use of org.agrona.collections.MutableInteger in project agrona by real-logic.
the class ManyToOneRingBufferTest method shouldLimitReadOfMessages.
@Test
public void shouldLimitReadOfMessages() {
final int msgLength = 16;
final int recordLength = HEADER_LENGTH + msgLength;
final int alignedRecordLength = align(recordLength, ALIGNMENT);
final long head = 0L;
final int headIndex = (int) head;
when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head);
when(buffer.getInt(typeOffset(headIndex))).thenReturn(MSG_TYPE_ID);
when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(recordLength);
final MutableInteger times = new MutableInteger();
final MessageHandler handler = (msgTypeId, buffer, index, length) -> times.increment();
final int limit = 1;
final int messagesRead = ringBuffer.read(handler, limit);
assertThat(messagesRead, is(1));
assertThat(times.get(), is(1));
final InOrder inOrder = inOrder(buffer);
inOrder.verify(buffer, times(1)).setMemory(headIndex, alignedRecordLength, (byte) 0);
inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, head + alignedRecordLength);
}
use of org.agrona.collections.MutableInteger in project agrona by real-logic.
the class ManyToOneRingBufferTest method shouldReadTwoMessages.
@Test
public void shouldReadTwoMessages() {
final int msgLength = 16;
final int recordLength = HEADER_LENGTH + msgLength;
final int alignedRecordLength = align(recordLength, ALIGNMENT);
final long tail = alignedRecordLength * 2L;
final long head = 0L;
final int headIndex = (int) head;
when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head);
when(buffer.getInt(typeOffset(headIndex))).thenReturn(MSG_TYPE_ID);
when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(recordLength);
when(buffer.getInt(typeOffset(headIndex + alignedRecordLength))).thenReturn(MSG_TYPE_ID);
when(buffer.getIntVolatile(lengthOffset(headIndex + alignedRecordLength))).thenReturn(recordLength);
final MutableInteger times = new MutableInteger();
final MessageHandler handler = (msgTypeId, buffer, index, length) -> times.increment();
final int messagesRead = ringBuffer.read(handler);
assertThat(messagesRead, is(2));
assertThat(times.get(), is(2));
final InOrder inOrder = inOrder(buffer);
inOrder.verify(buffer, times(1)).setMemory(headIndex, alignedRecordLength * 2, (byte) 0);
inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, tail);
}
use of org.agrona.collections.MutableInteger in project agrona by real-logic.
the class OneToOneRingBufferTest method shouldCopeWithExceptionFromHandler.
@Test
public void shouldCopeWithExceptionFromHandler() {
final int msgLength = 16;
final int recordLength = HEADER_LENGTH + msgLength;
final int alignedRecordLength = align(recordLength, ALIGNMENT);
final long tail = alignedRecordLength * 2L;
final long head = 0L;
final int headIndex = (int) head;
when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head);
when(buffer.getInt(typeOffset(headIndex))).thenReturn(MSG_TYPE_ID);
when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(recordLength);
when(buffer.getInt(typeOffset(headIndex + alignedRecordLength))).thenReturn(MSG_TYPE_ID);
when(buffer.getIntVolatile(lengthOffset(headIndex + alignedRecordLength))).thenReturn(recordLength);
final MutableInteger times = new MutableInteger();
final MessageHandler handler = (msgTypeId, buffer, index, length) -> {
if (times.incrementAndGet() == 2) {
throw new RuntimeException();
}
};
try {
ringBuffer.read(handler);
} catch (final RuntimeException ignore) {
assertThat(times.get(), is(2));
final InOrder inOrder = inOrder(buffer);
inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, tail);
inOrder.verify(buffer, times(0)).setMemory(anyInt(), anyInt(), anyByte());
return;
}
fail("Should have thrown exception");
}
use of org.agrona.collections.MutableInteger in project agrona by real-logic.
the class OneToOneRingBufferTest method shouldReadTwoMessages.
@Test
public void shouldReadTwoMessages() {
final int msgLength = 16;
final int recordLength = HEADER_LENGTH + msgLength;
final int alignedRecordLength = align(recordLength, ALIGNMENT);
final long tail = alignedRecordLength * 2L;
final long head = 0L;
final int headIndex = (int) head;
when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head);
when(buffer.getInt(typeOffset(headIndex))).thenReturn(MSG_TYPE_ID);
when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(recordLength);
when(buffer.getInt(typeOffset(headIndex + alignedRecordLength))).thenReturn(MSG_TYPE_ID);
when(buffer.getIntVolatile(lengthOffset(headIndex + alignedRecordLength))).thenReturn(recordLength);
final MutableInteger times = new MutableInteger();
final MessageHandler handler = (msgTypeId, buffer, index, length) -> times.increment();
final int messagesRead = ringBuffer.read(handler);
assertThat(messagesRead, is(2));
assertThat(times.get(), is(2));
final InOrder inOrder = inOrder(buffer);
inOrder.verify(buffer, times(1)).putLongOrdered(HEAD_COUNTER_INDEX, tail);
inOrder.verify(buffer, times(0)).setMemory(anyInt(), anyInt(), anyByte());
}
use of org.agrona.collections.MutableInteger 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());
}
Aggregations