Search in sources :

Example 86 with InterruptAfter

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

the class ClientErrorHandlerTest method shouldHaveCorrectTermBufferLength.

@Test
@InterruptAfter(10)
@SuppressWarnings("try")
void shouldHaveCorrectTermBufferLength() {
    final MediaDriver.Context ctx = new MediaDriver.Context().errorHandler(Tests::onError).dirDeleteOnStart(true);
    final ErrorHandler mockErrorHandlerOne = mock(ErrorHandler.class);
    final Aeron.Context clientCtxOne = new Aeron.Context().errorHandler(mockErrorHandlerOne);
    final ErrorHandler mockErrorHandlerTwo = mock(ErrorHandler.class);
    final Aeron.Context clientCtxTwo = new Aeron.Context().errorHandler(mockErrorHandlerTwo).subscriberErrorHandler(RethrowingErrorHandler.INSTANCE);
    try (TestMediaDriver ignore = TestMediaDriver.launch(ctx, testWatcher);
        Aeron aeronOne = Aeron.connect(clientCtxOne);
        Aeron aeronTwo = Aeron.connect(clientCtxTwo);
        Publication publication = aeronOne.addPublication(CHANNEL, STREAM_ID);
        Subscription subscriptionOne = aeronOne.addSubscription(CHANNEL, STREAM_ID);
        Subscription subscriptionTwo = aeronTwo.addSubscription(CHANNEL, STREAM_ID)) {
        awaitConnected(subscriptionOne);
        awaitConnected(subscriptionTwo);
        assertEquals(clientCtxOne.errorHandler(), clientCtxOne.subscriberErrorHandler());
        assertNotEquals(clientCtxTwo.errorHandler(), clientCtxTwo.subscriberErrorHandler());
        final UnsafeBuffer buffer = new UnsafeBuffer(new byte[100]);
        while (publication.offer(buffer) < 0) {
            Tests.yield();
        }
        final RuntimeException expectedException = new RuntimeException("Expected");
        final FragmentHandler handler = (buffer1, offset, length, header) -> {
            throw expectedException;
        };
        while (0 == subscriptionOne.poll(handler, 1)) {
            Tests.yield();
        }
        verify(mockErrorHandlerOne).onError(expectedException);
        try {
            while (0 == subscriptionTwo.poll(handler, 1)) {
                Tests.yield();
            }
            fail("Expected exception");
        } catch (final Exception ex) {
            assertEquals(expectedException, ex);
        }
        verify(mockErrorHandlerTwo, never()).onError(any());
    } finally {
        ctx.deleteDirectory();
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) ErrorHandler(org.agrona.ErrorHandler) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) Mockito(org.mockito.Mockito) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Assertions(org.junit.jupiter.api.Assertions) FragmentHandler(io.aeron.logbuffer.FragmentHandler) Tests.awaitConnected(io.aeron.test.Tests.awaitConnected) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) ErrorHandler(org.agrona.ErrorHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MediaDriver(io.aeron.driver.MediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 87 with InterruptAfter

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

the class CounterTest method shouldBeAbleToAddCounter.

@Test
@InterruptAfter(10)
void shouldBeAbleToAddCounter() {
    final AvailableCounterHandler availableCounterHandlerClientA = mock(AvailableCounterHandler.class);
    clientA.addAvailableCounterHandler(availableCounterHandlerClientA);
    final AvailableCounterHandler availableCounterHandlerClientB = mock(AvailableCounterHandler.class);
    clientB.addAvailableCounterHandler(availableCounterHandlerClientB);
    final Counter counter = clientA.addCounter(COUNTER_TYPE_ID, keyBuffer, 0, keyBuffer.capacity(), labelBuffer, 0, COUNTER_LABEL.length());
    assertFalse(counter.isClosed());
    assertEquals(counter.registrationId(), clientA.countersReader().getCounterRegistrationId(counter.id()));
    assertEquals(clientA.clientId(), clientA.countersReader().getCounterOwnerId(counter.id()));
    verify(availableCounterHandlerClientA, timeout(5000L)).onAvailableCounter(any(CountersReader.class), eq(counter.registrationId()), eq(counter.id()));
    verify(availableCounterHandlerClientB, timeout(5000L)).onAvailableCounter(any(CountersReader.class), eq(counter.registrationId()), eq(counter.id()));
}
Also used : ReadableCounter(io.aeron.status.ReadableCounter) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 88 with InterruptAfter

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

the class BufferClaimMessageTest method shouldTransferReservedValue.

@ParameterizedTest
@MethodSource("channels")
@InterruptAfter(10)
void shouldTransferReservedValue(final String channel) {
    final BufferClaim bufferClaim = new BufferClaim();
    try (Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
        Publication publication = aeron.addPublication(channel, STREAM_ID)) {
        while (publication.tryClaim(MESSAGE_LENGTH, bufferClaim) < 0L) {
            Tests.yield();
        }
        final long reservedValue = System.currentTimeMillis();
        bufferClaim.reservedValue(reservedValue);
        bufferClaim.commit();
        final MutableBoolean done = new MutableBoolean();
        while (!done.get()) {
            final int fragments = subscription.poll((buffer, offset, length, header) -> {
                assertEquals(MESSAGE_LENGTH, length);
                assertEquals(reservedValue, header.reservedValue());
                done.value = true;
            }, FRAGMENT_COUNT_LIMIT);
            if (0 == fragments) {
                Tests.yield();
            }
        }
    }
}
Also used : MutableBoolean(org.agrona.collections.MutableBoolean) BufferClaim(io.aeron.logbuffer.BufferClaim) InterruptAfter(io.aeron.test.InterruptAfter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 89 with InterruptAfter

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

the class ControlledMessageTest method shouldReceivePublishedMessage.

@Test
@InterruptAfter(10)
void shouldReceivePublishedMessage() {
    try (Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[PAYLOAD_LENGTH * 4]);
        for (int i = 0; i < 4; i++) {
            srcBuffer.setMemory(i * PAYLOAD_LENGTH, PAYLOAD_LENGTH, (byte) (65 + i));
        }
        for (int i = 0; i < 4; i++) {
            while (publication.offer(srcBuffer, i * PAYLOAD_LENGTH, PAYLOAD_LENGTH) < 0L) {
                Tests.yield();
            }
        }
        final FragmentCollector fragmentCollector = new FragmentCollector();
        int numFragments = 0;
        do {
            final int fragments = subscription.controlledPoll(fragmentCollector, FRAGMENT_COUNT_LIMIT);
            if (0 == fragments) {
                Tests.yield();
            }
            numFragments += fragments;
        } while (numFragments < 4);
        final UnsafeBuffer collectedBuffer = fragmentCollector.collectedBuffer();
        for (int i = 0; i < srcBuffer.capacity(); i++) {
            assertEquals(srcBuffer.getByte(i), collectedBuffer.getByte(i), "same at i=" + i);
        }
    }
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 90 with InterruptAfter

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

the class MultiSubscriberTest method shouldReceiveMessageOnSeparateSubscriptions.

@Test
@InterruptAfter(10)
void shouldReceiveMessageOnSeparateSubscriptions() {
    final FragmentHandler mockFragmentHandlerOne = mock(FragmentHandler.class);
    final FragmentHandler mockFragmentHandlerTwo = mock(FragmentHandler.class);
    final FragmentAssembler adapterOne = new FragmentAssembler(mockFragmentHandlerOne);
    final FragmentAssembler adapterTwo = new FragmentAssembler(mockFragmentHandlerTwo);
    try (Subscription subscriptionOne = aeron.addSubscription(CHANNEL_1, STREAM_ID);
        Subscription subscriptionTwo = aeron.addSubscription(CHANNEL_2, STREAM_ID);
        Publication publication = aeron.addPublication(CHANNEL_1, STREAM_ID)) {
        final byte[] expectedBytes = "Hello, World! here is a small message".getBytes();
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(expectedBytes);
        assertEquals(0, subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT));
        assertEquals(0, subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT));
        while (!subscriptionOne.isConnected() || !subscriptionTwo.isConnected()) {
            Tests.yield();
        }
        while (publication.offer(srcBuffer) < 0L) {
            Tests.yield();
        }
        while (subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT) == 0) {
            Tests.yield();
        }
        while (subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT) == 0) {
            Tests.yield();
        }
        verifyData(srcBuffer, mockFragmentHandlerOne);
        verifyData(srcBuffer, mockFragmentHandlerTwo);
    }
}
Also used : FragmentHandler(io.aeron.logbuffer.FragmentHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) 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