use of io.aeron.test.InterruptAfter 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);
}
}
use of io.aeron.test.InterruptAfter in project Aeron by real-logic.
the class ExclusivePublicationTest method shouldOfferTwoBuffersFromIndependentExclusivePublications.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldOfferTwoBuffersFromIndependentExclusivePublications(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 + 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);
for (int i = 0; i < expectedNumberOfFragments; i += 2) {
while (publicationOne.offer(pubOneHeader, 0, SIZE_OF_INT, pubOnePayload, 0, MESSAGE_LENGTH) < 0L) {
Tests.yield();
totalFragmentsRead += pollFragments(subscription, fragmentHandler);
}
while (publicationTwo.offer(pubTwoHeader, 0, SIZE_OF_INT, pubTwoPayload, 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.test.InterruptAfter in project Aeron by real-logic.
the class ImageAvailabilityTest method shouldCallImageHandlers.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
@SuppressWarnings("try")
void shouldCallImageHandlers(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 String spyChannel = channel.contains("ipc") ? channel : CommonContext.SPY_PREFIX + channel;
try (Subscription subOne = aeron.addSubscription(channel, STREAM_ID, availableHandler, unavailableHandler);
Subscription subTwo = aeron.addSubscription(spyChannel, STREAM_ID, availableHandler, unavailableHandler);
Publication publication = aeron.addPublication(channel, STREAM_ID)) {
while (!subOne.isConnected() || !subTwo.isConnected() || !publication.isConnected()) {
Tests.yield();
aeron.conductorAgentInvoker().invoke();
}
final Image image = subOne.imageAtIndex(0);
final Image spyImage = subTwo.imageAtIndex(0);
assertFalse(image.isClosed());
assertFalse(image.isEndOfStream());
assertFalse(spyImage.isClosed());
assertFalse(spyImage.isEndOfStream());
assertEquals(2, availableImageCount.get());
assertEquals(0, unavailableImageCount.get());
publication.close();
while (subOne.isConnected() || subTwo.isConnected()) {
Tests.yield();
aeron.conductorAgentInvoker().invoke();
}
assertTrue(image.isClosed());
assertTrue(image.isEndOfStream());
assertTrue(spyImage.isClosed());
assertTrue(spyImage.isEndOfStream());
assertEquals(2, availableImageCount.get());
assertEquals(2, unavailableImageCount.get());
}
}
use of io.aeron.test.InterruptAfter in project Aeron by real-logic.
the class ImageAvailabilityTest method shouldCallImageHandlersWithPublisherOnDifferentClient.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
@SuppressWarnings("try")
void shouldCallImageHandlersWithPublisherOnDifferentClient(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 String spyChannel = channel.contains("ipc") ? channel : CommonContext.SPY_PREFIX + channel;
final Aeron.Context ctx = new Aeron.Context().useConductorAgentInvoker(true);
try (Aeron aeronTwo = Aeron.connect(ctx);
Subscription subOne = aeron.addSubscription(channel, STREAM_ID, availableHandler, unavailableHandler);
Subscription subTwo = aeron.addSubscription(spyChannel, STREAM_ID, availableHandler, unavailableHandler);
Publication publication = aeronTwo.addPublication(channel, STREAM_ID)) {
while (!subOne.isConnected() || !subTwo.isConnected() || !publication.isConnected()) {
Tests.yield();
aeron.conductorAgentInvoker().invoke();
}
final Image image = subOne.imageAtIndex(0);
final Image spyImage = subTwo.imageAtIndex(0);
assertFalse(image.isClosed());
assertFalse(image.isEndOfStream());
assertFalse(spyImage.isClosed());
assertFalse(spyImage.isEndOfStream());
assertEquals(2, availableImageCount.get());
assertEquals(0, unavailableImageCount.get());
aeronTwo.close();
while (subOne.isConnected() || subTwo.isConnected()) {
Tests.yield();
aeron.conductorAgentInvoker().invoke();
}
assertTrue(image.isClosed());
assertTrue(image.isEndOfStream());
assertTrue(spyImage.isClosed());
assertTrue(spyImage.isEndOfStream());
assertEquals(2, availableImageCount.get());
assertEquals(2, unavailableImageCount.get());
}
}
use of io.aeron.test.InterruptAfter in project Aeron by real-logic.
the class GapFillLossTest method shouldGapFillWhenLossOccurs.
@Test
@InterruptAfter(10)
void shouldGapFillWhenLossOccurs() throws Exception {
final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(MSG_LENGTH));
srcBuffer.setMemory(0, MSG_LENGTH, (byte) 7);
final MediaDriver.Context ctx = new MediaDriver.Context().errorHandler(Tests::onError).threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).publicationTermBufferLength(LogBufferDescriptor.TERM_MIN_LENGTH);
final LossGenerator noLossGenerator = DebugChannelEndpointConfiguration.lossGeneratorSupplier(0, 0);
ctx.sendChannelEndpointSupplier((udpChannel, statusIndicator, context) -> new DebugSendChannelEndpoint(udpChannel, statusIndicator, context, noLossGenerator, noLossGenerator));
TestMediaDriver.enableLossGenerationOnReceive(ctx, 0.20, 0xcafebabeL, true, false);
try (TestMediaDriver mediaDriver = TestMediaDriver.launch(ctx, watcher);
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(UNRELIABLE_CHANNEL, STREAM_ID);
Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
final Subscriber subscriber = new Subscriber(subscription);
final Thread subscriberThread = new Thread(subscriber);
subscriberThread.setDaemon(true);
subscriberThread.start();
long position = 0;
for (int i = 0; i < NUM_MESSAGES; i++) {
srcBuffer.putLong(0, i);
while ((position = publication.offer(srcBuffer)) < 0L) {
Tests.yield();
}
}
FINAL_POSITION.set(position);
subscriberThread.join();
verifyLossOccurredForStream(ctx.aeronDirectoryName(), STREAM_ID);
assertThat(subscriber.messageCount, lessThan(NUM_MESSAGES));
} finally {
ctx.deleteDirectory();
}
}
Aggregations