use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.
the class ManyToOneRingBufferTest 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 ManyToOneRingBuffer(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());
}
use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.
the class ManyToOneRingBufferTest method shouldAbortOnControlledRead.
@Test
public void shouldAbortOnControlledRead() {
final String msg = "Hello World";
final ExpandableArrayBuffer srcBuffer = new ExpandableArrayBuffer();
final int srcLength = srcBuffer.putStringAscii(0, msg);
final RingBuffer ringBuffer = new ManyToOneRingBuffer(new UnsafeBuffer(new byte[1024]));
assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
assertEquals(MSG_TYPE_ID, msgTypeId);
assertEquals(HEADER_LENGTH, index);
assertEquals(srcLength, length);
assertEquals(msg, buffer.getStringAscii(index));
return ControlledMessageHandler.Action.ABORT;
};
final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler, 1);
assertEquals(0, messagesRead);
assertEquals(0, ringBuffer.consumerPosition());
}
use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.
the class ManyToOneRingBufferTest method shouldContinueOnControlledRead.
@Test
public void shouldContinueOnControlledRead() {
final String msg = "Hello World";
final ExpandableArrayBuffer srcBuffer = new ExpandableArrayBuffer();
final int srcLength = srcBuffer.putStringAscii(0, msg);
final RingBuffer ringBuffer = new ManyToOneRingBuffer(new UnsafeBuffer(new byte[1024]));
assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
assertEquals(MSG_TYPE_ID, msgTypeId);
assertEquals(HEADER_LENGTH, index);
assertEquals(srcLength, length);
assertEquals(msg, buffer.getStringAscii(index));
return ControlledMessageHandler.Action.CONTINUE;
};
final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler, 1);
assertEquals(1, messagesRead);
assertEquals(ringBuffer.producerPosition(), ringBuffer.consumerPosition());
}
use of org.agrona.concurrent.ControlledMessageHandler 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());
}
use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.
the class OneToOneRingBufferTest method shouldContinueOnControlledRead.
@Test
public void shouldContinueOnControlledRead() {
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]));
assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
assertEquals(MSG_TYPE_ID, msgTypeId);
assertEquals(HEADER_LENGTH, index);
assertEquals(srcLength, length);
assertEquals(msg, buffer.getStringAscii(index));
return ControlledMessageHandler.Action.CONTINUE;
};
final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler, 1);
assertEquals(1, messagesRead);
assertEquals(ringBuffer.producerPosition(), ringBuffer.consumerPosition());
}
Aggregations