use of io.aeron.test.InterruptAfter in project Aeron by real-logic.
the class SpecifiedPositionPublicationTest method shouldValidateSpecifiedPositionForConcurrentPublications.
@InterruptAfter(5)
@ParameterizedTest
@CsvSource({ CommonContext.IPC_CHANNEL, "aeron:udp?endpoint=localhost:24325" })
void shouldValidateSpecifiedPositionForConcurrentPublications(final String initialUri) {
final MediaDriver.Context context = new MediaDriver.Context().dirDeleteOnStart(true).ipcPublicationTermWindowLength(LogBufferDescriptor.TERM_MIN_LENGTH).threadingMode(ThreadingMode.SHARED);
final DirectBuffer msg = new UnsafeBuffer(new byte[64]);
final int termLength = 1 << 16;
final int initialTermId = randomWatcher.random().nextInt();
final int activeTermId = initialTermId + randomWatcher.random().nextInt(Integer.MAX_VALUE);
final int positionBitsToShift = LogBufferDescriptor.positionBitsToShift(termLength);
final int termOffset = randomWatcher.random().nextInt(termLength) & -FrameDescriptor.FRAME_ALIGNMENT;
final long startPosition = LogBufferDescriptor.computePosition(activeTermId, termOffset, positionBitsToShift, initialTermId);
final int totalMessageLength = DataHeaderFlyweight.HEADER_LENGTH + msg.capacity();
final PositionCalculator positionCalculator = new PositionCalculator(startPosition, termLength, termOffset);
final long positionMsg1 = positionCalculator.addMessage(totalMessageLength);
final long positionMsg2 = positionCalculator.addMessage(totalMessageLength);
final long positionMsg3 = positionCalculator.addMessage(totalMessageLength);
final String channel = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId, termLength).build();
final String invalidPositionUri = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition + FrameDescriptor.FRAME_ALIGNMENT, initialTermId, termLength).build();
final String invalidInitialTermIdUri = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId + 1, termLength).build();
final String invalidTermLengthUri = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId, termLength << 1).build();
final int streamId = 1001;
try (TestMediaDriver mediaDriver = TestMediaDriver.launch(context, testWatcher);
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(initialUri, streamId);
Publication publication = aeron.addPublication(channel, streamId)) {
Tests.awaitConnected(subscription);
Tests.awaitConnected(publication);
assertEquals(startPosition, publication.position());
Tests.await(() -> publication.offer(msg) > 0);
assertEquals(positionMsg1, publication.position());
Tests.await(() -> subscription.poll((buffer, offset, length, header) -> {
}, 1) == 1);
try (Publication publication2 = aeron.addPublication(channel, streamId)) {
assertEquals(positionMsg1, publication2.position());
Tests.await(() -> publication.offer(msg) > 0);
assertEquals(positionMsg2, publication2.position());
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> assertEquals(positionMsg2, header.position());
Tests.await(() -> subscription.poll(fragmentHandler, 1) == 1);
}
try (Publication publication3 = aeron.addPublication(initialUri, streamId)) {
assertEquals(positionMsg2, publication3.position());
Tests.await(() -> publication.offer(msg) > 0);
assertEquals(positionMsg3, publication3.position());
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> assertEquals(positionMsg3, header.position());
Tests.await(() -> subscription.poll(fragmentHandler, 1) == 1);
}
assertThrows(RegistrationException.class, () -> aeron.addPublication(invalidPositionUri, streamId));
assertThrows(RegistrationException.class, () -> aeron.addPublication(invalidInitialTermIdUri, streamId));
assertThrows(RegistrationException.class, () -> aeron.addPublication(invalidTermLengthUri, streamId));
} finally {
context.deleteDirectory();
}
}
use of io.aeron.test.InterruptAfter in project Aeron by real-logic.
the class SpySimulatedConnectionTest method shouldSimulateConnectionWithNoNetworkSubscriptions.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldSimulateConnectionWithNoNetworkSubscriptions(final String channel) {
final int messagesToSend = NUM_MESSAGES_PER_TERM * 3;
driverContext.publicationConnectionTimeoutNs(TimeUnit.MILLISECONDS.toNanos(250)).timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(100)).spiesSimulateConnection(true);
launch();
spy = client.addSubscription(spyForChannel(channel), STREAM_ID);
publication = client.addPublication(channel, STREAM_ID);
while (!spy.isConnected() || !publication.isConnected()) {
Tests.yield();
}
for (int i = 0; i < messagesToSend; i++) {
while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
Tests.yield();
}
final MutableInteger fragmentsRead = new MutableInteger();
Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
final int fragments = spy.poll(fragmentHandlerSpy, 10);
if (0 == fragments) {
Thread.yield();
}
fragmentsRead.value += fragments;
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
}
assertEquals(messagesToSend, fragmentCountSpy.value);
}
use of io.aeron.test.InterruptAfter in project Aeron by real-logic.
the class StopStartSecondSubscriberTest method shouldReceivePublishedMessage.
@Test
@InterruptAfter(10)
void shouldReceivePublishedMessage() {
launch(CHANNEL1, STREAM_ID1, CHANNEL2, STREAM_ID2);
buffer.putInt(0, 1);
final int messagesPerPublication = 1;
while (publicationOne.offer(buffer, 0, BitUtil.SIZE_OF_INT) < 0L) {
Tests.yield();
}
while (publicationTwo.offer(buffer, 0, BitUtil.SIZE_OF_INT) < 0L) {
Tests.yield();
}
final MutableInteger fragmentsRead1 = new MutableInteger();
final MutableInteger fragmentsRead2 = new MutableInteger();
Tests.executeUntil(() -> fragmentsRead1.get() >= messagesPerPublication && fragmentsRead2.get() >= messagesPerPublication, (i) -> {
fragmentsRead1.value += subscriptionOne.poll(fragmentHandlerOne, 10);
fragmentsRead2.value += subscriptionTwo.poll(fragmentHandlerTwo, 10);
Thread.yield();
}, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(9900));
assertEquals(messagesPerPublication, subOneCount.get());
assertEquals(messagesPerPublication, subTwoCount.get());
}
use of io.aeron.test.InterruptAfter in project Aeron by real-logic.
the class BufferClaimMessageTest method shouldReceivePublishedMessageWithInterleavedAbort.
@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldReceivePublishedMessageWithInterleavedAbort(final String channel) {
final MutableInteger fragmentCount = new MutableInteger();
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> fragmentCount.value++;
final BufferClaim bufferClaim = new BufferClaim();
final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocate(MESSAGE_LENGTH));
try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
Publication publication = aeron.addPublication(channel, STREAM_ID)) {
publishMessage(srcBuffer, publication);
while (publication.tryClaim(MESSAGE_LENGTH, bufferClaim) < 0L) {
Tests.yield();
}
publishMessage(srcBuffer, publication);
bufferClaim.abort();
final int expectedNumberOfFragments = 2;
int numFragments = 0;
do {
final int fragments = subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT);
if (0 == fragments) {
Tests.yield();
}
numFragments += fragments;
} while (numFragments < expectedNumberOfFragments);
assertEquals(expectedNumberOfFragments, fragmentCount.value);
}
}
use of io.aeron.test.InterruptAfter in project Aeron by real-logic.
the class CounterTest method shouldBeAbleToAddReadableCounterWithinHandler.
@Test
@InterruptAfter(10)
void shouldBeAbleToAddReadableCounterWithinHandler() {
clientB.addAvailableCounterHandler(this::createReadableCounter);
final Counter counter = clientA.addCounter(COUNTER_TYPE_ID, keyBuffer, 0, keyBuffer.capacity(), labelBuffer, 0, COUNTER_LABEL.length());
while (null == readableCounter) {
Tests.sleep(1);
}
assertEquals(CountersReader.RECORD_ALLOCATED, readableCounter.state());
assertEquals(counter.id(), readableCounter.counterId());
assertEquals(counter.registrationId(), readableCounter.registrationId());
}
Aggregations