use of org.agrona.collections.MutableLong in project aeron by real-logic.
the class MultiDestinationSubscriptionTest method shouldMergeStreamsFromMultiplePublicationsWithSameParams.
@Test
@InterruptAfter(10)
void shouldMergeStreamsFromMultiplePublicationsWithSameParams() {
final int numMessagesToSend = 30;
final int numMessagesToSendForA = numMessagesToSend / 2;
final int numMessagesToSendForB = numMessagesToSend / 2;
launch(Tests::onError);
launchSecond();
final String publicationChannelA = new ChannelUriStringBuilder().media(CommonContext.UDP_MEDIA).endpoint(UNICAST_ENDPOINT_A).build();
final String destinationB = new ChannelUriStringBuilder().media(CommonContext.UDP_MEDIA).endpoint(UNICAST_ENDPOINT_B).build();
subscription = clientA.addSubscription(SUB_URI, STREAM_ID);
subscription.addDestination(publicationChannelA);
subscription.addDestination(destinationB);
publicationA = clientA.addExclusivePublication(publicationChannelA, STREAM_ID);
final String publicationChannelB = new ChannelUriStringBuilder().media(CommonContext.UDP_MEDIA).initialPosition(0L, publicationA.initialTermId(), publicationA.termBufferLength()).sessionId(publicationA.sessionId()).endpoint(UNICAST_ENDPOINT_B).build();
publicationB = clientB.addExclusivePublication(publicationChannelB, STREAM_ID);
final MutableLong position = new MutableLong(Long.MIN_VALUE);
final Supplier<String> offerFailure = () -> "Failed to offer: " + position;
for (int i = 0; i < numMessagesToSendForA; i++) {
while (publicationA.offer(buffer, 0, buffer.capacity()) < 0L) {
Tests.yield();
}
pollForFragment(subscription, fragmentHandler);
while ((position.value = publicationB.offer(buffer, 0, buffer.capacity())) < 0L) {
Tests.yieldingIdle(offerFailure);
}
assertEquals(0, subscription.poll(fragmentHandler, 10));
}
for (int i = 0; i < numMessagesToSendForB; i++) {
while (publicationB.offer(buffer, 0, buffer.capacity()) < 0L) {
Tests.yield();
}
pollForFragment(subscription, fragmentHandler);
while (publicationA.offer(buffer, 0, buffer.capacity()) < 0L) {
Tests.yield();
}
assertEquals(0, subscription.poll(fragmentHandler, 10));
}
assertEquals(1, subscription.imageCount());
assertEquals(2, subscription.imageAtIndex(0).activeTransportCount());
verifyFragments(fragmentHandler, numMessagesToSend);
}
use of org.agrona.collections.MutableLong in project aeron by real-logic.
the class MultiDestinationCastTest method shouldManuallyAddPortDuringActiveStream.
@Test
@InterruptAfter(10)
void shouldManuallyAddPortDuringActiveStream() throws InterruptedException {
final int numMessagesToSend = MESSAGES_PER_TERM * 3;
final int numMessageForSub2 = 10;
final CountingFragmentHandler fragmentHandlerA = new CountingFragmentHandler("fragmentHandlerA");
final CountingFragmentHandler fragmentHandlerB = new CountingFragmentHandler("fragmentHandlerB");
final Supplier<String> messageSupplierA = fragmentHandlerA::toString;
final Supplier<String> messageSupplierB = fragmentHandlerB::toString;
final CountDownLatch availableImage = new CountDownLatch(1);
final MutableLong position = new MutableLong(0);
final MutableInteger messagesSent = new MutableInteger(0);
final Supplier<String> positionSupplier = () -> "Failed to publish, position: " + position + ", sent: " + messagesSent;
launch(Tests::onError);
subscriptionA = clientA.addSubscription(SUB1_MDC_MANUAL_URI, STREAM_ID);
subscriptionB = clientB.addSubscription(SUB2_MDC_MANUAL_URI, STREAM_ID, (image) -> availableImage.countDown(), null);
publication = clientA.addPublication(PUB_MDC_MANUAL_URI, STREAM_ID);
publication.addDestination(SUB1_MDC_MANUAL_URI);
Tests.awaitConnected(subscriptionA);
while (messagesSent.value < numMessagesToSend) {
position.value = publication.offer(buffer, 0, MESSAGE_LENGTH);
if (0 <= position.value) {
messagesSent.increment();
} else {
Tests.yieldingIdle(positionSupplier);
}
subscriptionA.poll(fragmentHandlerA, FRAGMENT_LIMIT);
if (messagesSent.value > (numMessagesToSend - numMessageForSub2)) {
subscriptionB.poll(fragmentHandlerB, FRAGMENT_LIMIT);
}
if (messagesSent.value == (numMessagesToSend - numMessageForSub2)) {
final int published = messagesSent.value;
// then B will receive more than the expected `numMessageForSub2`.
while (fragmentHandlerA.notDone(published)) {
if (subscriptionA.poll(fragmentHandlerA, FRAGMENT_LIMIT) <= 0) {
Tests.yieldingIdle(messageSupplierA);
}
}
publication.addDestination(SUB2_MDC_MANUAL_URI);
availableImage.await();
}
}
while (fragmentHandlerA.notDone(numMessagesToSend) || fragmentHandlerB.notDone(numMessageForSub2)) {
if (fragmentHandlerA.notDone(numMessagesToSend) && subscriptionA.poll(fragmentHandlerA, FRAGMENT_LIMIT) <= 0) {
Tests.yieldingIdle(messageSupplierA);
}
if (fragmentHandlerB.notDone(numMessageForSub2) && subscriptionB.poll(fragmentHandlerB, FRAGMENT_LIMIT) <= 0) {
Tests.yieldingIdle(messageSupplierB);
}
}
}
Aggregations