use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class TimestampingSystemTest method shouldSupportSendReceiveTimestamps.
@Test
@InterruptAfter(10)
void shouldSupportSendReceiveTimestamps() {
final MutableDirectBuffer 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_CHANNEL_TIMESTAMPS, 1000);
while (null == sub.resolvedEndpoint()) {
Tests.yieldingIdle("Failed to resolve endpoint");
}
final String uri = new ChannelUriStringBuilder(CHANNEL_WITH_CHANNEL_TIMESTAMPS).endpoint(requireNonNull(sub.resolvedEndpoint())).build();
final Publication pub = aeron.addPublication(uri, 1000);
Tests.awaitConnected(pub);
buffer.putLong(0, SENTINEL_VALUE);
buffer.putLong(8, SENTINEL_VALUE);
while (0 > pub.offer(buffer, 0, buffer.capacity())) {
Tests.yieldingIdle("Failed to offer message");
}
final MutableLong receiveTimestamp = new MutableLong(SENTINEL_VALUE);
final MutableLong sendTimestamp = new MutableLong(SENTINEL_VALUE);
final FragmentHandler fragmentHandler = (buffer1, offset, length, header) -> {
receiveTimestamp.set(buffer1.getLong(offset));
sendTimestamp.set(buffer1.getLong(offset + 8));
};
while (1 > sub.poll(fragmentHandler, 1)) {
Tests.yieldingIdle("Failed to receive message");
}
assertNotEquals(SENTINEL_VALUE, receiveTimestamp.longValue());
assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
}
}
use of io.aeron.logbuffer.FragmentHandler in project aeron by real-logic.
the class UntetheredSubscriptionTest method shouldRejoinAfterResting.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
public void shouldRejoinAfterResting(final String channel) {
final AtomicInteger unavailableImageCount = new AtomicInteger();
final AtomicInteger availableImageCount = new AtomicInteger();
final UnavailableImageHandler unavailableHandler = (image) -> unavailableImageCount.incrementAndGet();
final AvailableImageHandler availableHandler = (image) -> availableImageCount.incrementAndGet();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
};
final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocate(MESSAGE_LENGTH));
final String untetheredChannel = channel + "|tether=false";
final String publicationChannel = channel.startsWith("aeron-spy") ? channel.substring(10) : channel;
boolean pollingUntethered = true;
try (Subscription tetheredSub = aeron.addSubscription(channel, STREAM_ID);
Subscription untetheredSub = aeron.addSubscription(untetheredChannel, STREAM_ID, availableHandler, unavailableHandler);
Publication publication = aeron.addPublication(publicationChannel, STREAM_ID)) {
while (!tetheredSub.isConnected() || !untetheredSub.isConnected()) {
Tests.yield();
aeron.conductorAgentInvoker().invoke();
}
while (true) {
if (publication.offer(srcBuffer) < 0) {
Tests.yield();
aeron.conductorAgentInvoker().invoke();
}
if (pollingUntethered && untetheredSub.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT) > 0) {
pollingUntethered = false;
}
tetheredSub.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT);
if (unavailableImageCount.get() == 1) {
while (availableImageCount.get() < 2) {
Tests.yield();
aeron.conductorAgentInvoker().invoke();
}
return;
}
}
}
}
use of io.aeron.logbuffer.FragmentHandler 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);
}
}
use of io.aeron.logbuffer.FragmentHandler 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 io.aeron.logbuffer.FragmentHandler 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);
}
}
Aggregations