use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class ArchiveSystemTests method consume.
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)) {
Tests.yield();
}
}
assertEquals(count, received.get());
}
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 MutableInteger received = new MutableInteger(startIndex);
final FragmentHandler fragmentHandler = new FragmentAssembler((buffer, offset, length, header) -> {
final String expected = MESSAGE_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, ArchiveSystemTests.FRAGMENT_LIMIT)) {
Tests.yield();
}
}
assertEquals(startIndex + count, received.get());
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class SpySimulatedConnectionTest method shouldSimulateConnectionWithNoNetworkSubscriptions.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldSimulateConnectionWithNoNetworkSubscriptions(final String channel) {
final int messagesToSend = NUM_MESSAGES_PER_TERM * 3;
driverContext.publicationConnectionTimeoutNs(TimeUnit.MILLISECONDS.toNanos(250)).timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(100)).spiesSimulateConnection(true);
launch();
spy = client.addSubscription(spyForChannel(channel), STREAM_ID);
publication = client.addPublication(channel, STREAM_ID);
while (!spy.isConnected() || !publication.isConnected()) {
Tests.yield();
}
for (int i = 0; i < messagesToSend; i++) {
while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
Tests.yield();
}
final MutableInteger fragmentsRead = new MutableInteger();
Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
final int fragments = spy.poll(fragmentHandlerSpy, 10);
if (0 == fragments) {
Thread.yield();
}
fragmentsRead.value += fragments;
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
}
assertEquals(messagesToSend, fragmentCountSpy.value);
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class StopStartSecondSubscriberTest method shouldReceiveMessagesAfterStopStart.
private void shouldReceiveMessagesAfterStopStart(final String channelOne, final int streamOne, final String channelTwo, final int streamTwo) {
final int numMessages = 1;
final MutableInteger subscriber2AfterRestartCount = new MutableInteger();
final AtomicBoolean running = new AtomicBoolean(true);
final CountDownLatch latch = new CountDownLatch(2);
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> subscriber2AfterRestartCount.value++;
launch(channelOne, streamOne, channelTwo, streamTwo);
buffer.putInt(0, 1);
final ExecutorService executor = Executors.newFixedThreadPool(2);
try {
executor.execute(() -> doPublisherWork(publicationOne, running, latch));
executor.execute(() -> doPublisherWork(publicationTwo, running, latch));
final MutableInteger fragmentsReadOne = new MutableInteger();
final MutableInteger fragmentsReadTwo = new MutableInteger();
final BooleanSupplier fragmentsReadCondition = () -> fragmentsReadOne.get() >= numMessages && fragmentsReadTwo.get() >= numMessages;
Tests.executeUntil(fragmentsReadCondition, (i) -> {
fragmentsReadOne.value += subscriptionOne.poll(fragmentHandlerOne, 1);
fragmentsReadTwo.value += subscriptionTwo.poll(fragmentHandlerTwo, 1);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(4900));
assertTrue(subOneCount.get() >= numMessages);
assertTrue(subTwoCount.get() >= numMessages);
subscriptionTwo.close();
fragmentsReadOne.set(0);
fragmentsReadTwo.set(0);
subscriptionTwo = subscriberTwo.addSubscription(channelTwo, streamTwo);
Tests.executeUntil(fragmentsReadCondition, (i) -> {
fragmentsReadOne.value += subscriptionOne.poll(fragmentHandlerOne, 1);
fragmentsReadTwo.value += subscriptionTwo.poll(fragmentHandler, 1);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(4900));
running.set(false);
latch.await();
assertTrue(subOneCount.get() >= numMessages * 2, "Expecting subscriberOne to receive messages the entire time");
assertTrue(subTwoCount.get() >= numMessages, "Expecting subscriberTwo to receive messages before being stopped and started");
assertTrue(subscriber2AfterRestartCount.get() >= numMessages, "Expecting subscriberTwo to receive messages after being stopped and started");
} catch (final InterruptedException ex) {
fail("Interrupted", ex);
} finally {
running.set(false);
executor.shutdownNow();
}
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class PubAndSubTest method shouldContinueAfterRolloverWithMinimalPaddingHeader.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldContinueAfterRolloverWithMinimalPaddingHeader(final String channel) {
final int termBufferLength = 64 * 1024;
final int termBufferLengthMinusPaddingHeader = termBufferLength - HEADER_LENGTH;
final int num1kMessagesInTermBuffer = 63;
final int lastMessageLength = termBufferLengthMinusPaddingHeader - (num1kMessagesInTermBuffer * 1024) - HEADER_LENGTH;
final int messageLength = 1024 - HEADER_LENGTH;
context.publicationTermBufferLength(termBufferLength);
launch(channel);
// lock step reception until we get to within 8 messages of the end
for (int i = 0; i < num1kMessagesInTermBuffer - 7; i++) {
while (publication.offer(buffer, 0, messageLength) < 0L) {
Tests.yield();
}
pollForFragment();
}
for (int i = 7; i > 0; i--) {
while (publication.offer(buffer, 0, messageLength) < 0L) {
Tests.yield();
}
}
// small enough to leave room for padding that is just a header
while (publication.offer(buffer, 0, lastMessageLength) < 0L) {
Tests.yield();
}
// no roll over
while (publication.offer(buffer, 0, messageLength) < 0L) {
Tests.yield();
}
final MutableInteger fragmentsRead = new MutableInteger();
Tests.executeUntil(() -> fragmentsRead.value == 9, (j) -> {
final int fragments = subscription.poll(fragmentHandler, 10);
if (0 == fragments) {
Thread.yield();
}
fragmentsRead.value += fragments;
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
final InOrder inOrder = inOrder(fragmentHandler);
inOrder.verify(fragmentHandler, times(num1kMessagesInTermBuffer)).onFragment(any(DirectBuffer.class), anyInt(), eq(messageLength), any(Header.class));
inOrder.verify(fragmentHandler, times(1)).onFragment(any(DirectBuffer.class), anyInt(), eq(lastMessageLength), any(Header.class));
inOrder.verify(fragmentHandler, times(1)).onFragment(any(DirectBuffer.class), anyInt(), eq(messageLength), any(Header.class));
}
Aggregations