use of org.agrona.collections.MutableInteger in project agrona by real-logic.
the class OneToOneRingBufferTest 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)).putLongOrdered(HEAD_COUNTER_INDEX, head + alignedRecordLength);
inOrder.verify(buffer, times(0)).setMemory(anyInt(), anyInt(), anyByte());
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class BasicArchiveTest method consume.
private static void consume(final Subscription subscription, final int count, final String prefix) {
final MutableInteger received = new MutableInteger(0);
final FragmentHandler fragmentHandler = new FragmentAssembler((buffer, offset, length, header) -> {
final String expected = prefix + received.value;
final String actual = buffer.getStringWithoutLengthAscii(offset, length);
assertEquals(expected, actual);
received.value++;
});
while (received.value < count) {
if (0 == subscription.poll(fragmentHandler, FRAGMENT_LIMIT)) {
SystemTest.checkInterruptedStatus();
Thread.yield();
}
}
assertThat(received.get(), is(count));
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class MultiDestinationCastTest method shouldSendToTwoPortsWithDynamicSingleDriver.
@Test(timeout = 10_000)
public void shouldSendToTwoPortsWithDynamicSingleDriver() {
final int numMessagesToSend = NUM_MESSAGES_PER_TERM * 3;
launch();
publication = clientA.addPublication(PUB_MDC_DYNAMIC_URI, STREAM_ID);
subscriptionA = clientA.addSubscription(SUB1_MDC_DYNAMIC_URI, STREAM_ID);
subscriptionB = clientA.addSubscription(SUB2_MDC_DYNAMIC_URI, STREAM_ID);
subscriptionC = clientA.addSubscription(SUB3_MDC_DYNAMIC_URI, STREAM_ID);
while (!subscriptionA.isConnected() || !subscriptionB.isConnected() || !subscriptionC.isConnected()) {
SystemTest.checkInterruptedStatus();
Thread.yield();
}
for (int i = 0; i < numMessagesToSend; i++) {
while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
SystemTest.checkInterruptedStatus();
Thread.yield();
}
final MutableInteger fragmentsRead = new MutableInteger();
pollForFragment(subscriptionA, fragmentHandlerA, fragmentsRead);
fragmentsRead.set(0);
pollForFragment(subscriptionB, fragmentHandlerB, fragmentsRead);
fragmentsRead.set(0);
pollForFragment(subscriptionC, fragmentHandlerC, fragmentsRead);
}
verifyFragments(fragmentHandlerA, numMessagesToSend);
verifyFragments(fragmentHandlerB, numMessagesToSend);
verifyFragments(fragmentHandlerC, numMessagesToSend);
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class FlowControlStrategiesTest method shouldTimeoutImageWhenBehindForTooLongWithMaxMulticastFlowControlStrategy.
@Test(timeout = 10_000)
public void shouldTimeoutImageWhenBehindForTooLongWithMaxMulticastFlowControlStrategy() throws Exception {
final int numMessagesToSend = NUM_MESSAGES_PER_TERM * 3;
final CountDownLatch unavailableCountDownLatch = new CountDownLatch(1);
final CountDownLatch availableCountDownLatch = new CountDownLatch(2);
driverBContext.imageLivenessTimeoutNs(TimeUnit.MILLISECONDS.toNanos(500));
driverAContext.multicastFlowControlSupplier((udpChannel, streamId, registrationId) -> new MaxMulticastFlowControl());
launch();
publication = clientA.addPublication(MULTICAST_URI, STREAM_ID);
subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID, (image) -> availableCountDownLatch.countDown(), (image) -> unavailableCountDownLatch.countDown());
while (!subscriptionA.isConnected() || !subscriptionB.isConnected()) {
SystemTest.checkInterruptedStatus();
Thread.yield();
}
for (int i = 0; i < numMessagesToSend; i++) {
while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
Thread.yield();
}
// A keeps up
final MutableInteger fragmentsRead = new MutableInteger();
SystemTest.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
fragmentsRead.value += subscriptionA.poll(fragmentHandlerA, 10);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
fragmentsRead.set(0);
// B receives slowly and eventually can't keep up
if (i % 10 == 0) {
SystemTest.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
fragmentsRead.value += subscriptionB.poll(fragmentHandlerB, 1);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
}
}
unavailableCountDownLatch.await();
availableCountDownLatch.await();
verify(fragmentHandlerA, times(numMessagesToSend)).onFragment(any(DirectBuffer.class), anyInt(), eq(MESSAGE_LENGTH), any(Header.class));
verify(fragmentHandlerB, atMost(numMessagesToSend - 1)).onFragment(any(DirectBuffer.class), anyInt(), eq(MESSAGE_LENGTH), any(Header.class));
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class ExtendRecordingTest method consume.
private static void consume(final Subscription subscription, final int startIndex, final int count, final String prefix) {
final MutableInteger received = new MutableInteger(startIndex);
final FragmentHandler fragmentHandler = new FragmentAssembler((buffer, offset, length, header) -> {
final String expected = prefix + received.value;
final String actual = buffer.getStringWithoutLengthAscii(offset, length);
assertEquals(expected, actual);
received.value++;
});
while (received.value < (startIndex + count)) {
if (0 == subscription.poll(fragmentHandler, FRAGMENT_LIMIT)) {
SystemTest.checkInterruptedStatus();
Thread.yield();
}
}
assertThat(received.get(), is(startIndex + count));
}
Aggregations