use of io.aeron.logbuffer.FragmentHandler 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 io.aeron.logbuffer.FragmentHandler in project Aeron by real-logic.
the class TwoBufferOfferMessageTest method shouldTransferFragmentedTwoPartMessage.
@Test
@InterruptAfter(10)
void shouldTransferFragmentedTwoPartMessage() {
final UnsafeBuffer expectedBuffer = new UnsafeBuffer(new byte[32 + driver.context().mtuLength()]);
final UnsafeBuffer bufferOne = new UnsafeBuffer(expectedBuffer, 0, 32);
final UnsafeBuffer bufferTwo = new UnsafeBuffer(expectedBuffer, 32, expectedBuffer.capacity() - 32);
bufferOne.setMemory(0, bufferOne.capacity(), (byte) 'a');
bufferTwo.setMemory(0, bufferTwo.capacity(), (byte) 'b');
final String expectedMessage = expectedBuffer.getStringWithoutLengthAscii(0, expectedBuffer.capacity());
final MutableReference<String> receivedMessage = new MutableReference<>();
final FragmentHandler fragmentHandler = new FragmentAssembler((buffer, offset, length, header) -> receivedMessage.set(buffer.getStringWithoutLengthAscii(offset, length)));
try (Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
try (Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
publishMessage(bufferOne, bufferTwo, publication);
pollForMessage(subscription, receivedMessage, fragmentHandler);
assertEquals(expectedMessage, receivedMessage.get());
}
try (Publication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID)) {
publishMessage(bufferOne, bufferTwo, publication);
pollForMessage(subscription, receivedMessage, fragmentHandler);
assertEquals(expectedMessage, receivedMessage.get());
}
}
}
use of io.aeron.logbuffer.FragmentHandler 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 io.aeron.logbuffer.FragmentHandler in project Aeron by real-logic.
the class TimestampingSystemTest method shouldSupportMediaReceiveTimestampsInCDriver.
@Test
@InterruptAfter(10)
@EnabledOnOs(OS.LINUX)
void shouldSupportMediaReceiveTimestampsInCDriver() {
assumeTrue(TestMediaDriver.shouldRunCMediaDriver());
final DirectBuffer buffer = new UnsafeBuffer(new byte[64]);
try (TestMediaDriver driver = driver();
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(driver.aeronDirectoryName()))) {
final Subscription sub = aeron.addSubscription(CHANNEL_WITH_MEDIA_TIMESTAMP, 1000);
while (null == sub.resolvedEndpoint()) {
Tests.yieldingIdle("Failed to resolve endpoint");
}
final String uri = "aeron:udp?endpoint=" + sub.resolvedEndpoint();
final Publication pub = aeron.addPublication(uri, 1000);
Tests.awaitConnected(pub);
while (0 > pub.offer(buffer, 0, buffer.capacity(), (termBuffer, termOffset, frameLength) -> SENTINEL_VALUE)) {
Tests.yieldingIdle("Failed to offer message");
}
final FragmentHandler fragmentHandler = (buffer1, offset, length, header) -> assertNotEquals(SENTINEL_VALUE, header.reservedValue());
while (1 > sub.poll(fragmentHandler, 1)) {
Tests.yieldingIdle("Failed to receive message");
}
}
}
use of io.aeron.logbuffer.FragmentHandler in project Aeron by real-logic.
the class TimestampingSystemTest method shouldSupportReceiveTimestampsOnMds.
@Test
@InterruptAfter(10)
void shouldSupportReceiveTimestampsOnMds() {
final MutableDirectBuffer buffer = new UnsafeBuffer(new byte[64]);
try (TestMediaDriver driver = driver();
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(driver.aeronDirectoryName()))) {
final Subscription mdsSub = aeron.addSubscription("aeron:udp?control-mode=manual|channel-rcv-ts-offset=0", 1000);
final Publication pub1 = aeron.addPublication("aeron:udp?endpoint=localhost:23424", 1000);
final Publication pub2 = aeron.addPublication("aeron:udp?endpoint=localhost:23425", 1000);
mdsSub.addDestination("aeron:udp?endpoint=localhost:23424");
mdsSub.addDestination("aeron:udp?endpoint=localhost:23425");
while (!pub1.isConnected() || !pub2.isConnected()) {
Tests.yieldingIdle("Failed to connect");
}
buffer.putLong(0, SENTINEL_VALUE);
while (0 > pub1.offer(buffer, 0, buffer.capacity())) {
Tests.yieldingIdle("Failed to offer message");
}
while (0 > pub2.offer(buffer, 0, buffer.capacity())) {
Tests.yieldingIdle("Failed to offer message");
}
final MutableLong sendTimestamp = new MutableLong(SENTINEL_VALUE);
final FragmentHandler fragmentHandler = (buffer1, offset, length, header) -> sendTimestamp.set(buffer1.getLong(offset));
while (1 > mdsSub.poll(fragmentHandler, 1)) {
Tests.yieldingIdle("Failed to receive message");
}
assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
while (1 > mdsSub.poll(fragmentHandler, 1)) {
Tests.yieldingIdle("Failed to receive message");
}
assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
}
}
Aggregations