use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class PubAndSubTest method shouldReceiveOnlyAfterSendingUpToFlowControlLimit.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldReceiveOnlyAfterSendingUpToFlowControlLimit(final String channel) {
/*
* The subscriber will flow control before an entire term buffer. So, send until can't send anymore.
* Then start up subscriber to drain.
*/
final int termBufferLength = 64 * 1024;
final int numMessagesPerTerm = 64;
final int messageLength = (termBufferLength / numMessagesPerTerm) - HEADER_LENGTH;
final int maxFails = 10000;
int messagesSent = 0;
context.publicationTermBufferLength(termBufferLength);
launch(channel);
for (int i = 0; i < numMessagesPerTerm; i++) {
int offerFails = 0;
while (publication.offer(buffer, 0, messageLength) < 0L) {
if (++offerFails > maxFails) {
break;
}
Tests.yield();
}
if (offerFails > maxFails) {
break;
}
messagesSent++;
}
final MutableInteger fragmentsRead = new MutableInteger();
final int messagesToReceive = messagesSent;
Tests.executeUntil(() -> fragmentsRead.value >= messagesToReceive, (j) -> {
final int fragments = subscription.poll(fragmentHandler, 10);
if (0 == fragments) {
Thread.yield();
}
fragmentsRead.value += fragments;
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
verify(fragmentHandler, times(messagesToReceive)).onFragment(any(DirectBuffer.class), anyInt(), eq(messageLength), any(Header.class));
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class PublicationUnblockTest method shouldUnblockNonCommittedMessage.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldUnblockNonCommittedMessage(final String channel) {
final MutableInteger fragmentCount = new MutableInteger();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> fragmentCount.value++;
try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
Publication publicationOne = aeron.addPublication(channel, STREAM_ID);
Publication publicationTwo = aeron.addPublication(channel, STREAM_ID)) {
final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[driver.context().mtuLength()]);
final int length = 128;
srcBuffer.setMemory(0, length, (byte) 66);
final BufferClaim bufferClaim = new BufferClaim();
while (publicationOne.tryClaim(length, bufferClaim) < 0L) {
Tests.yield();
}
bufferClaim.buffer().setMemory(bufferClaim.offset(), length, (byte) 65);
bufferClaim.commit();
while (publicationTwo.offer(srcBuffer, 0, length) < 0L) {
Tests.yield();
}
while (publicationOne.tryClaim(length, bufferClaim) < 0L) {
Tests.yield();
}
while (publicationTwo.offer(srcBuffer, 0, length) < 0L) {
Tests.yield();
}
final int expectedFragments = 3;
int numFragments = 0;
do {
final int fragments = subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT);
if (fragments == 0) {
Tests.yield();
}
numFragments += fragments;
} while (numFragments < expectedFragments);
assertEquals(expectedFragments, numFragments);
assertEquals(expectedFragments, fragmentCount.value);
}
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class BufferClaimMessageTest method shouldReceivePublishedMessageWithInterleavedAbort.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldReceivePublishedMessageWithInterleavedAbort(final String channel) {
final MutableInteger fragmentCount = new MutableInteger();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> fragmentCount.value++;
final BufferClaim bufferClaim = new BufferClaim();
final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocate(MESSAGE_LENGTH));
try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
Publication publication = aeron.addPublication(channel, STREAM_ID)) {
publishMessage(srcBuffer, publication);
while (publication.tryClaim(MESSAGE_LENGTH, bufferClaim) < 0L) {
Tests.yield();
}
publishMessage(srcBuffer, publication);
bufferClaim.abort();
final int expectedNumberOfFragments = 2;
int numFragments = 0;
do {
final int fragments = subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT);
if (0 == fragments) {
Tests.yield();
}
numFragments += fragments;
} while (numFragments < expectedNumberOfFragments);
assertEquals(expectedNumberOfFragments, fragmentCount.value);
}
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class ExclusivePublicationTest method shouldPublishFromConcurrentExclusivePublications.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldPublishFromConcurrentExclusivePublications(final String channel) {
try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
ExclusivePublication publicationOne = aeron.addExclusivePublication(channel, STREAM_ID);
ExclusivePublication publicationTwo = aeron.addExclusivePublication(channel, STREAM_ID)) {
final int expectedNumberOfFragments = 20_000;
final int fragmentsPerThread = expectedNumberOfFragments / 2;
final MutableInteger messageCount = new MutableInteger();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
assertEquals(MESSAGE_LENGTH, length);
messageCount.value++;
};
Tests.awaitConnections(subscription, 2);
final ExecutorService threadPool = Executors.newFixedThreadPool(2);
try {
final CountDownLatch latch = new CountDownLatch(2);
threadPool.submit(() -> {
latch.countDown();
latch.await();
for (int count = 0; count < fragmentsPerThread; count++) {
while (publicationOne.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
}
}
return null;
});
threadPool.submit(() -> {
latch.countDown();
latch.await();
for (int count = 0; count < fragmentsPerThread; count++) {
while (publicationTwo.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
}
}
return null;
});
int totalFragmentsRead = 0;
do {
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
} while (totalFragmentsRead < expectedNumberOfFragments);
} finally {
threadPool.shutdownNow();
}
assertEquals(expectedNumberOfFragments, messageCount.value);
}
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class ExclusivePublicationTest method shouldPublishFromIndependentExclusivePublications.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldPublishFromIndependentExclusivePublications(final String channel) {
try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
ExclusivePublication publicationOne = aeron.addExclusivePublication(channel, STREAM_ID);
ExclusivePublication publicationTwo = aeron.addExclusivePublication(channel, STREAM_ID)) {
final int expectedNumberOfFragments = 778;
int totalFragmentsRead = 0;
final MutableInteger messageCount = new MutableInteger();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
assertEquals(MESSAGE_LENGTH, length);
messageCount.value++;
};
Tests.awaitConnections(subscription, 2);
for (int i = 0; i < expectedNumberOfFragments; i += 2) {
while (publicationOne.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
}
while (publicationTwo.offer(srcBuffer, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
}
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
}
do {
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
} while (totalFragmentsRead < expectedNumberOfFragments);
assertEquals(expectedNumberOfFragments, messageCount.value);
}
}
Aggregations