use of io.aeron.test.InterruptAfter in project aeron by real-logic.
the class PubAndSubTest method shouldReceivePublishedMessageViaPollFile.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldReceivePublishedMessageViaPollFile(final String channel) {
launch(channel);
publishMessage();
while (true) {
final long bytes = subscription.rawPoll(rawBlockHandler, Integer.MAX_VALUE);
if (bytes > 0) {
break;
}
Tests.yield();
}
final long expectedOffset = 0L;
final int expectedLength = BitUtil.align(HEADER_LENGTH + SIZE_OF_INT, FRAME_ALIGNMENT);
final ArgumentCaptor<FileChannel> channelArgumentCaptor = ArgumentCaptor.forClass(FileChannel.class);
verify(rawBlockHandler).onBlock(channelArgumentCaptor.capture(), eq(expectedOffset), any(UnsafeBuffer.class), eq((int) expectedOffset), eq(expectedLength), anyInt(), anyInt());
assertTrue(channelArgumentCaptor.getValue().isOpen(), "File Channel is closed");
}
use of io.aeron.test.InterruptAfter in project aeron by real-logic.
the class PubAndSubTest method shouldReceivePublishedMessageBatchedWithDataLoss.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldReceivePublishedMessageBatchedWithDataLoss(final String channel) throws IOException {
assumeFalse(IPC_URI.equals(channel));
final int termBufferLength = 64 * 1024;
final int numMessagesInTermBuffer = 64;
final int messageLength = (termBufferLength / numMessagesInTermBuffer) - HEADER_LENGTH;
final int numMessagesToSend = 2 * numMessagesInTermBuffer;
final int numBatches = 4;
final int numMessagesPerBatch = numMessagesToSend / numBatches;
final LossGenerator noLossGenerator = DebugChannelEndpointConfiguration.lossGeneratorSupplier(0, 0);
context.publicationTermBufferLength(termBufferLength);
context.sendChannelEndpointSupplier((udpChannel, statusIndicator, context) -> new DebugSendChannelEndpoint(udpChannel, statusIndicator, context, noLossGenerator, noLossGenerator));
TestMediaDriver.enableLossGenerationOnReceive(context, 0.1, 0xcafebabeL, true, false);
launch(channel);
for (int i = 0; i < numBatches; i++) {
for (int j = 0; j < numMessagesPerBatch; j++) {
while (publication.offer(buffer, 0, messageLength) < 0L) {
Tests.yield();
}
}
pollForBatch(numMessagesPerBatch);
}
verify(fragmentHandler, times(numMessagesToSend)).onFragment(any(DirectBuffer.class), anyInt(), eq(messageLength), any(Header.class));
verifyLossOccurredForStream(context.aeronDirectoryName(), STREAM_ID);
}
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 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 offerBlockAcceptsUpToAnEntireTermOfData.
@Test
@InterruptAfter(10)
void offerBlockAcceptsUpToAnEntireTermOfData() {
final String channel = "aeron:ipc?term-length=64k";
try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
ExclusivePublication publication = aeron.addExclusivePublication(channel, STREAM_ID)) {
final int sessionId = publication.sessionId();
final int streamId = publication.streamId();
final int currentTermId = publication.termId();
final int termBufferLength = publication.termBufferLength();
final int offset = 1024;
final RawBlockHandler rawBlockHandler = (fileChannel, fileOffset, termBuffer, termOffset, length, pollSessionId, termId) -> {
assertEquals(HDR_TYPE_DATA, frameType(termBuffer, termOffset));
assertEquals(termBufferLength, frameLength(termBuffer, termOffset));
assertEquals(sessionId, frameSessionId(termBuffer, termOffset));
assertEquals(streamId, termBuffer.getInt(termOffset + STREAM_ID_FIELD_OFFSET, LITTLE_ENDIAN));
};
frameType(srcBuffer, offset, HDR_TYPE_DATA);
frameLengthOrdered(srcBuffer, offset, termBufferLength);
frameSessionId(srcBuffer, offset, sessionId);
srcBuffer.putInt(offset + STREAM_ID_FIELD_OFFSET, streamId, LITTLE_ENDIAN);
srcBuffer.putInt(offset + TERM_ID_FIELD_OFFSET, currentTermId, LITTLE_ENDIAN);
srcBuffer.setMemory(offset + DATA_OFFSET, termBufferLength - HEADER_LENGTH, (byte) 13);
Tests.awaitConnections(subscription, 1);
while (publication.availableWindow() <= 0) {
Tests.yield();
}
final long position = publication.position();
final long result = publication.offerBlock(srcBuffer, offset, termBufferLength);
assertEquals(position + termBufferLength, result);
final long pollBytes = subscription.rawPoll(rawBlockHandler, termBufferLength);
assertEquals(termBufferLength, pollBytes);
assertEquals(publication.termBufferLength(), publication.termOffset());
assertEquals(currentTermId, publication.termId());
}
}
Aggregations