Search in sources :

Example 81 with InterruptAfter

use of io.aeron.test.InterruptAfter in project aeron by real-logic.

the class MaxFlowControlStrategySystemTest method shouldTimeoutImageWhenBehindForTooLongWithMaxMulticastFlowControlStrategy.

@Test
@InterruptAfter(10)
void shouldTimeoutImageWhenBehindForTooLongWithMaxMulticastFlowControlStrategy() {
    final int numMessagesToSend = NUM_MESSAGES_PER_TERM * 3;
    driverBContext.imageLivenessTimeoutNs(TimeUnit.MILLISECONDS.toNanos(500));
    driverAContext.multicastFlowControlSupplier(new MaxMulticastFlowControlSupplier());
    launch();
    subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
    subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID);
    publication = clientA.addPublication(MULTICAST_URI, STREAM_ID);
    while (!subscriptionA.isConnected() || !subscriptionB.isConnected() || !publication.isConnected()) {
        Tests.yield();
    }
    final MutableInteger fragmentsRead = new MutableInteger();
    for (int i = 0; i < numMessagesToSend; i++) {
        while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
            Tests.yield();
        }
        fragmentsRead.set(0);
        // A keeps up
        Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
            fragmentsRead.value += subscriptionA.poll(fragmentHandlerA, 10);
            Thread.yield();
        }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
        fragmentsRead.set(0);
        // B receives slowly and eventually can't keep up
        if (i % 10 == 0) {
            Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
                fragmentsRead.value += subscriptionB.poll(fragmentHandlerB, 1);
                Thread.yield();
            }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
        }
    }
    verify(fragmentHandlerA, times(numMessagesToSend)).onFragment(any(DirectBuffer.class), anyInt(), eq(MESSAGE_LENGTH), any(Header.class));
    verify(fragmentHandlerB, atMost(numMessagesToSend - 1)).onFragment(any(DirectBuffer.class), anyInt(), eq(MESSAGE_LENGTH), any(Header.class));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) MutableInteger(org.agrona.collections.MutableInteger) MaxMulticastFlowControlSupplier(io.aeron.driver.MaxMulticastFlowControlSupplier) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 82 with InterruptAfter

use of io.aeron.test.InterruptAfter in project aeron by real-logic.

the class MemoryOrderingTest method shouldReceiveMessagesInOrderWithFirstLongWordIntact.

@Test
@InterruptAfter(10)
void shouldReceiveMessagesInOrderWithFirstLongWordIntact() throws Exception {
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocate(MESSAGE_LENGTH));
    srcBuffer.setMemory(0, MESSAGE_LENGTH, (byte) 7);
    try (Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
        final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
        final Thread subscriberThread = new Thread(new Subscriber(subscription));
        subscriberThread.setDaemon(true);
        subscriberThread.start();
        for (int i = 0; i < NUM_MESSAGES; i++) {
            if (null != failedMessage) {
                fail(failedMessage);
            }
            srcBuffer.putLong(0, i);
            while (publication.offer(srcBuffer) < 0L) {
                if (null != failedMessage) {
                    fail(failedMessage);
                }
                idleStrategy.idle();
                Tests.checkInterruptStatus();
            }
            if (i % BURST_LENGTH == 0) {
                final long timeoutNs = System.nanoTime() + INTER_BURST_DURATION_NS;
                long nowNs;
                do {
                    nowNs = System.nanoTime();
                } while ((timeoutNs - nowNs) > 0);
            }
        }
        subscriberThread.join();
    }
}
Also used : YieldingIdleStrategy(org.agrona.concurrent.YieldingIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 83 with InterruptAfter

use of io.aeron.test.InterruptAfter in project aeron by real-logic.

the class MemoryOrderingTest method shouldReceiveMessagesInOrderWithFirstLongWordIntactFromExclusivePublication.

@Test
@InterruptAfter(10)
void shouldReceiveMessagesInOrderWithFirstLongWordIntactFromExclusivePublication() throws InterruptedException {
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocate(MESSAGE_LENGTH));
    srcBuffer.setMemory(0, MESSAGE_LENGTH, (byte) 7);
    try (Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
        ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID)) {
        final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
        final Thread subscriberThread = new Thread(new Subscriber(subscription));
        subscriberThread.setDaemon(true);
        subscriberThread.start();
        for (int i = 0; i < NUM_MESSAGES; i++) {
            if (null != failedMessage) {
                fail(failedMessage);
            }
            srcBuffer.putLong(0, i);
            while (publication.offer(srcBuffer) < 0L) {
                if (null != failedMessage) {
                    fail(failedMessage);
                }
                idleStrategy.idle();
                Tests.checkInterruptStatus();
            }
            if (i % BURST_LENGTH == 0) {
                final long timeoutNs = System.nanoTime() + INTER_BURST_DURATION_NS;
                long nowNs;
                do {
                    nowNs = System.nanoTime();
                } while ((timeoutNs - nowNs) > 0);
            }
        }
        subscriberThread.join();
    }
}
Also used : YieldingIdleStrategy(org.agrona.concurrent.YieldingIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 84 with InterruptAfter

use of io.aeron.test.InterruptAfter in project aeron by real-logic.

the class LifecycleTest method shouldNotifyOfClientTimestampCounter.

@Test
@InterruptAfter(10)
void shouldNotifyOfClientTimestampCounter() {
    final MediaDriver.Context driverCtx = new MediaDriver.Context().dirDeleteOnStart(true).errorHandler(Tests::onError);
    try (TestMediaDriver mediaDriver = TestMediaDriver.launch(driverCtx, testWatcher)) {
        final Aeron.Context clientCtxOne = new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName());
        final Aeron.Context clientCtxTwo = new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName());
        try (Aeron aeron = Aeron.connect(clientCtxOne)) {
            final AvailableCounterHandler availableHandler = mock(AvailableCounterHandler.class);
            aeron.addAvailableCounterHandler(availableHandler);
            final UnavailableCounterHandler unavailableHandler = mock(UnavailableCounterHandler.class);
            aeron.addUnavailableCounterHandler(unavailableHandler);
            try (Aeron aeronTwo = Aeron.connect(clientCtxTwo)) {
                aeronTwo.addSubscription("aeron:ipc", 1001);
                verify(availableHandler, timeout(5000)).onAvailableCounter(any(), eq(clientCtxTwo.clientId()), anyInt());
            }
            verify(unavailableHandler, timeout(5000)).onUnavailableCounter(any(), eq(clientCtxTwo.clientId()), anyInt());
        }
    } finally {
        driverCtx.deleteDirectory();
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) Tests(io.aeron.test.Tests) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 85 with InterruptAfter

use of io.aeron.test.InterruptAfter in project aeron by real-logic.

the class MultiDriverTest method shouldJoinExistingIdleStreamWithLockStepSendingReceiving.

@Test
@InterruptAfter(10)
void shouldJoinExistingIdleStreamWithLockStepSendingReceiving() throws InterruptedException {
    final int numMessagesToSendPreJoin = 0;
    final int numMessagesToSendPostJoin = NUM_MESSAGES_PER_TERM;
    launch();
    subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
    publication = clientA.addPublication(MULTICAST_URI, STREAM_ID);
    while (!publication.isConnected() && !subscriptionA.isConnected()) {
        Tests.yield();
    }
    final CountDownLatch newImageLatch = new CountDownLatch(1);
    subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID, (image) -> newImageLatch.countDown(), null);
    newImageLatch.await();
    for (int i = 0; i < numMessagesToSendPostJoin; i++) {
        while (publication.offer(buffer, 0, buffer.capacity()) < 0L) {
            Tests.yield();
        }
        final MutableInteger fragmentsRead = new MutableInteger();
        Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
            fragmentsRead.value += subscriptionA.poll(fragmentHandlerA, 10);
            Thread.yield();
        }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
        fragmentsRead.set(0);
        Tests.executeUntil(() -> fragmentsRead.get() > 0, (j) -> {
            fragmentsRead.value += subscriptionB.poll(fragmentHandlerB, 10);
            Thread.yield();
        }, Integer.MAX_VALUE, TimeUnit.MILLISECONDS.toNanos(500));
    }
    assertEquals(numMessagesToSendPreJoin + numMessagesToSendPostJoin, fragmentCountA.value);
    assertEquals(numMessagesToSendPostJoin, fragmentCountB.value);
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) Tests(io.aeron.test.Tests) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) IoUtil(org.agrona.IoUtil) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) File(java.io.File) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) InterruptAfter(io.aeron.test.InterruptAfter) CountDownLatch(java.util.concurrent.CountDownLatch) SystemUtil(org.agrona.SystemUtil) AfterEach(org.junit.jupiter.api.AfterEach) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ThreadingMode(io.aeron.driver.ThreadingMode) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FragmentHandler(io.aeron.logbuffer.FragmentHandler) MutableInteger(org.agrona.collections.MutableInteger) CloseHelper(org.agrona.CloseHelper) MutableInteger(org.agrona.collections.MutableInteger) CountDownLatch(java.util.concurrent.CountDownLatch) 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