use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class PubAndSubTest method shouldContinueAfterBufferRolloverBatched.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldContinueAfterBufferRolloverBatched(final String channel) {
final int termBufferLength = 64 * 1024;
final int numBatchesPerTerm = 4;
final int numMessagesPerBatch = 16;
final int numMessagesInTermBuffer = numMessagesPerBatch * numBatchesPerTerm;
final int messageLength = (termBufferLength / numMessagesInTermBuffer) - HEADER_LENGTH;
final int numMessagesToSend = numMessagesInTermBuffer + 1;
context.publicationTermBufferLength(termBufferLength);
launch(channel);
for (int i = 0; i < numBatchesPerTerm; i++) {
for (int j = 0; j < numMessagesPerBatch; j++) {
while (publication.offer(buffer, 0, messageLength) < 0L) {
Tests.yield();
}
}
pollForBatch(numMessagesPerBatch);
}
while (publication.offer(buffer, 0, messageLength) < 0L) {
Tests.yield();
}
final MutableInteger fragmentsRead = new MutableInteger();
Tests.executeUntil(() -> fragmentsRead.value > 0, (j) -> {
final int fragments = subscription.poll(fragmentHandler, 10);
if (0 == fragments) {
Thread.yield();
}
fragmentsRead.value += fragments;
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(900));
verify(fragmentHandler, times(numMessagesToSend)).onFragment(any(DirectBuffer.class), anyInt(), eq(messageLength), any(Header.class));
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class PubAndSubTest method shouldFragmentExactMessageLengthsCorrectly.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldFragmentExactMessageLengthsCorrectly(final String channel) {
final int termBufferLength = 64 * 1024;
final int numFragmentsPerMessage = 2;
final int mtuLength = context.mtuLength();
final int frameLength = mtuLength - HEADER_LENGTH;
final int messageLength = frameLength * numFragmentsPerMessage;
final int numMessagesToSend = 2;
final int numFramesToExpect = numMessagesToSend * numFragmentsPerMessage;
context.publicationTermBufferLength(termBufferLength);
launch(channel);
for (int i = 0; i < numMessagesToSend; i++) {
while (publication.offer(buffer, 0, messageLength) < 0L) {
Tests.yield();
}
}
final MutableInteger fragmentsRead = new MutableInteger();
Tests.executeUntil(() -> fragmentsRead.value > numFramesToExpect, (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(numFramesToExpect)).onFragment(any(DirectBuffer.class), anyInt(), eq(frameLength), any(Header.class));
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class RemoteEchoTest method sendAndReceiveRandomData.
private void sendAndReceiveRandomData(final List<Publication> pubs, final List<Subscription> subs) {
assertEquals(pubs.size(), subs.size());
final List<ExpandableArrayBuffer> receivedDataBuffers = new ArrayList<>();
final List<MutableInteger> sentDataCounts = new ArrayList<>();
final List<MutableInteger> receivedDataCounts = new ArrayList<>();
final List<FragmentHandler> handlers = new ArrayList<>();
for (int i = 0; i < pubs.size(); i++) {
final ExpandableArrayBuffer receivedData = new ExpandableArrayBuffer(SOURCE_DATA_LENGTH);
final MutableInteger sentBytes = new MutableInteger(0);
final MutableInteger recvBytes = new MutableInteger(0);
final FragmentHandler handler = (buffer, offset, length, header) -> {
receivedData.putBytes(recvBytes.get(), buffer, offset, length);
recvBytes.addAndGet(length);
};
receivedDataBuffers.add(receivedData);
sentDataCounts.add(sentBytes);
receivedDataCounts.add(recvBytes);
handlers.add(handler);
}
while (dataIsPending(receivedDataCounts, SOURCE_DATA_LENGTH)) {
for (int i = 0; i < pubs.size(); i++) {
final Publication pub = pubs.get(i);
final Subscription sub = subs.get(i);
final MutableInteger sentBytes = sentDataCounts.get(i);
final FragmentHandler handler = handlers.get(i);
if (sentBytes.get() < SOURCE_DATA_LENGTH) {
final int randomLength = randomWatcher.random().nextInt(pub.maxMessageLength());
final int toSend = min(SOURCE_DATA_LENGTH - sentBytes.get(), randomLength);
if (pub.offer(sourceData, sentBytes.get(), toSend) > 0) {
sentBytes.addAndGet(toSend);
}
Tests.yield();
}
if (sub.poll(handler, 10) <= 0) {
Tests.yield();
}
}
}
for (final ExpandableArrayBuffer receivedData : receivedDataBuffers) {
assertEquals(0, sourceData.compareTo(receivedData));
}
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class RemoteEchoTest method sendAndReceiveRandomData.
private void sendAndReceiveRandomData(final Publication pub, final Subscription sub) {
final ExpandableArrayBuffer receivedData = new ExpandableArrayBuffer(SOURCE_DATA_LENGTH);
final MutableInteger sentBytes = new MutableInteger(0);
final MutableInteger recvBytes = new MutableInteger(0);
final FragmentHandler handler = (buffer, offset, length, header) -> {
receivedData.putBytes(recvBytes.get(), buffer, offset, length);
recvBytes.addAndGet(length);
};
while (recvBytes.get() < SOURCE_DATA_LENGTH) {
if (sentBytes.get() < SOURCE_DATA_LENGTH) {
final int randomLength = randomWatcher.random().nextInt(pub.maxMessageLength());
final int toSend = min(SOURCE_DATA_LENGTH - sentBytes.get(), randomLength);
if (pub.offer(sourceData, sentBytes.get(), toSend) > 0) {
sentBytes.addAndGet(toSend);
}
Tests.yield();
}
if (sub.poll(handler, 10) <= 0) {
Tests.yield();
}
}
assertEquals(0, sourceData.compareTo(receivedData));
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class ExclusivePublicationTest method shouldOfferTwoBuffersFromConcurrentExclusivePublications.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldOfferTwoBuffersFromConcurrentExclusivePublications(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 + SIZE_OF_INT, length);
final int publisherId = buffer.getInt(offset);
if (1 == publisherId) {
assertEquals(Byte.MIN_VALUE, buffer.getByte(offset + SIZE_OF_INT));
} else if (2 == publisherId) {
assertEquals(Byte.MAX_VALUE, buffer.getByte(offset + SIZE_OF_INT));
} else {
fail("unknown publisherId=" + publisherId);
}
messageCount.value++;
};
Tests.awaitConnections(subscription, 2);
final UnsafeBuffer pubOneHeader = new UnsafeBuffer(new byte[SIZE_OF_INT]);
pubOneHeader.putInt(0, 1);
final UnsafeBuffer pubOnePayload = new UnsafeBuffer(new byte[MESSAGE_LENGTH]);
pubOnePayload.setMemory(0, MESSAGE_LENGTH, Byte.MIN_VALUE);
final UnsafeBuffer pubTwoHeader = new UnsafeBuffer(new byte[SIZE_OF_INT]);
pubTwoHeader.putInt(0, 2);
final UnsafeBuffer pubTwoPayload = new UnsafeBuffer(new byte[MESSAGE_LENGTH]);
pubTwoPayload.setMemory(0, MESSAGE_LENGTH, Byte.MAX_VALUE);
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(pubOneHeader, 0, SIZE_OF_INT, pubOnePayload, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
}
}
return null;
});
threadPool.submit(() -> {
latch.countDown();
latch.await();
for (int count = 0; count < fragmentsPerThread; count++) {
while (publicationTwo.offer(pubTwoHeader, 0, SIZE_OF_INT, pubTwoPayload, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
}
}
return null;
});
threadPool.shutdown();
int totalFragmentsRead = 0;
do {
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
} while (totalFragmentsRead < expectedNumberOfFragments);
} finally {
threadPool.shutdownNow();
}
assertEquals(expectedNumberOfFragments, messageCount.value);
}
}
Aggregations