Search in sources :

Example 21 with InterruptAfter

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();
    }
}
Also used : FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) CsvSource(org.junit.jupiter.params.provider.CsvSource) RegistrationException(io.aeron.exceptions.RegistrationException) RandomWatcher(io.aeron.test.RandomWatcher) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Function(java.util.function.Function) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ThreadingMode(io.aeron.driver.ThreadingMode) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) DirectBuffer(org.agrona.DirectBuffer) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) InterruptAfter(io.aeron.test.InterruptAfter) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 22 with InterruptAfter

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);
}
Also used : MutableInteger(org.agrona.collections.MutableInteger) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 23 with InterruptAfter

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());
}
Also used : MutableInteger(org.agrona.collections.MutableInteger) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 24 with InterruptAfter

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);
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) MutableBoolean(org.agrona.collections.MutableBoolean) Arrays(java.util.Arrays) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) ByteBuffer(java.nio.ByteBuffer) InterruptAfter(io.aeron.test.InterruptAfter) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ThreadingMode(io.aeron.driver.ThreadingMode) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) BufferClaim(io.aeron.logbuffer.BufferClaim) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MethodSource(org.junit.jupiter.params.provider.MethodSource) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) BufferClaim(io.aeron.logbuffer.BufferClaim) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 25 with InterruptAfter

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());
}
Also used : ReadableCounter(io.aeron.status.ReadableCounter) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Aggregations

InterruptAfter (io.aeron.test.InterruptAfter)304 Test (org.junit.jupiter.api.Test)240 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)90 MediaDriver (io.aeron.driver.MediaDriver)74 TestNode (io.aeron.test.cluster.TestNode)72 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)68 Tests (io.aeron.test.Tests)66 CountersReader (org.agrona.concurrent.status.CountersReader)64 MethodSource (org.junit.jupiter.params.provider.MethodSource)62 SlowTest (io.aeron.test.SlowTest)60 InterruptingTestCallback (io.aeron.test.InterruptingTestCallback)58 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)58 TestMediaDriver (io.aeron.test.driver.TestMediaDriver)54 TestCluster (io.aeron.test.cluster.TestCluster)52 ThreadingMode (io.aeron.driver.ThreadingMode)50 MutableInteger (org.agrona.collections.MutableInteger)50 DirectBuffer (org.agrona.DirectBuffer)48 SystemTestWatcher (io.aeron.test.SystemTestWatcher)46 MutableLong (org.agrona.collections.MutableLong)46 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)46