use of org.agrona.collections.MutableInteger 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);
}
}
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class MultiDriverTest method shouldJoinExistingStreamWithLockStepSendingReceiving.
@Test
@InterruptAfter(10)
void shouldJoinExistingStreamWithLockStepSendingReceiving() throws InterruptedException {
final int numMessagesToSendPreJoin = NUM_MESSAGES_PER_TERM / 2;
final int numMessagesToSendPostJoin = NUM_MESSAGES_PER_TERM;
launch();
subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
publication = clientA.addPublication(MULTICAST_URI, STREAM_ID);
for (int i = 0; i < numMessagesToSendPreJoin; i++) {
while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
Tests.yield();
}
final MutableInteger fragmentsRead = new MutableInteger();
Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
fragmentsRead.value += subscriptionA.poll(fragmentHandlerA, 10);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
}
final CountDownLatch newImageLatch = new CountDownLatch(1);
subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID, (image) -> newImageLatch.countDown(), null);
newImageLatch.await();
for (int i = 0; i < numMessagesToSendPostJoin; i++) {
while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
Tests.yield();
}
final MutableInteger fragmentsRead = new MutableInteger();
Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
fragmentsRead.value += subscriptionA.poll(fragmentHandlerA, 10);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
fragmentsRead.set(0);
Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
fragmentsRead.value += subscriptionB.poll(fragmentHandlerB, 10);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
}
assertEquals(numMessagesToSendPreJoin + numMessagesToSendPostJoin, fragmentCountA.value);
assertEquals(numMessagesToSendPostJoin, fragmentCountB.value);
}
use of org.agrona.collections.MutableInteger in project aeron by real-logic.
the class PongTest method playPingPong.
@Test
void playPingPong() {
buffer.putInt(0, 1);
while (pingPublication.offer(buffer, 0, BitUtil.SIZE_OF_INT) < 0L) {
Tests.yield();
}
final MutableInteger fragmentsRead = new MutableInteger();
Tests.executeUntil(() -> fragmentsRead.get() > 0, (i) -> {
fragmentsRead.value += pingSubscription.poll(this::echoPingHandler, 10);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(5900));
fragmentsRead.set(0);
Tests.executeUntil(() -> fragmentsRead.get() > 0, (i) -> {
fragmentsRead.value += pongSubscription.poll(pongHandler, 10);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(5900));
verify(pongHandler).onFragment(any(DirectBuffer.class), eq(DataHeaderFlyweight.HEADER_LENGTH), eq(BitUtil.SIZE_OF_INT), any(Header.class));
}
use of org.agrona.collections.MutableInteger in project zeebe by camunda-cloud.
the class DbVariableState method getVariablesLocalAsDocument.
@Override
public DirectBuffer getVariablesLocalAsDocument(final long scopeKey) {
writer.wrap(documentResultBuffer, 0);
writer.reserveMapHeader();
final MutableInteger variableCount = new MutableInteger();
visitVariablesLocal(scopeKey, name -> true, (name, value) -> {
writer.writeString(name.getBuffer());
writer.writeRaw(value.getValue());
variableCount.addAndGet(1);
}, () -> false);
writer.writeReservedMapHeader(0, variableCount.get());
resultView.wrap(documentResultBuffer, 0, writer.getOffset());
return resultView;
}
use of org.agrona.collections.MutableInteger in project agrona by real-logic.
the class ManyToOneRingBufferTest method shouldNotReadSingleMessagePartWayThroughWriting.
@Test
public void shouldNotReadSingleMessagePartWayThroughWriting() {
final long head = 0L;
final int headIndex = (int) head;
when(buffer.getLong(HEAD_COUNTER_INDEX)).thenReturn(head);
when(buffer.getIntVolatile(lengthOffset(headIndex))).thenReturn(0);
final MutableInteger times = new MutableInteger();
final MessageHandler handler = (msgTypeId, buffer, index, length) -> times.increment();
final int messagesRead = ringBuffer.read(handler);
assertThat(messagesRead, is(0));
assertThat(times.get(), is(0));
final InOrder inOrder = inOrder(buffer);
inOrder.verify(buffer, times(1)).getIntVolatile(lengthOffset(headIndex));
inOrder.verify(buffer, times(0)).setMemory(headIndex, 0, (byte) 0);
inOrder.verify(buffer, times(0)).putLongOrdered(HEAD_COUNTER_INDEX, headIndex);
}
Aggregations