Search in sources :

Example 6 with TestMediaDriver

use of io.aeron.test.driver.TestMediaDriver in project Aeron by real-logic.

the class SpecifiedPositionPublicationTest method shouldValidateSpecifiedPositionForConcurrentPublicationsInitiallyUnspecified.

@InterruptAfter(5)
@ParameterizedTest
@CsvSource({ CommonContext.IPC_CHANNEL, "aeron:udp?endpoint=localhost:24325" })
void shouldValidateSpecifiedPositionForConcurrentPublicationsInitiallyUnspecified(final String initialUri) {
    final MediaDriver.Context context = new MediaDriver.Context().dirDeleteOnStart(true).ipcPublicationTermWindowLength(LogBufferDescriptor.TERM_MIN_LENGTH).threadingMode(ThreadingMode.SHARED);
    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(initialUri, streamId)) {
        Tests.awaitConnected(subscription);
        Tests.awaitConnected(publication);
        final String channel = new ChannelUriStringBuilder(initialUri).initialPosition(publication.position(), publication.initialTermId(), publication.termBufferLength()).build();
        try (Publication publication2 = aeron.addPublication(channel, streamId)) {
            assertEquals(publication.position(), publication2.position());
            assertEquals(publication.initialTermId(), publication2.initialTermId());
        }
    } finally {
        context.deleteDirectory();
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) InterruptAfter(io.aeron.test.InterruptAfter) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with TestMediaDriver

use of io.aeron.test.driver.TestMediaDriver 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 8 with TestMediaDriver

use of io.aeron.test.driver.TestMediaDriver in project Aeron by real-logic.

the class TaggedFlowControlSystemTest method shouldPreventConnectionUntilRequiredGroupSizeMatchTagIsMet.

@SuppressWarnings("methodlength")
@SlowTest
@Test
@InterruptAfter(20)
void shouldPreventConnectionUntilRequiredGroupSizeMatchTagIsMet() {
    final Long groupTag = 2701L;
    final Integer groupSize = 3;
    final ChannelUriStringBuilder builder = new ChannelUriStringBuilder().media("udp").endpoint("224.20.30.39:24326").networkInterface("localhost");
    final String uriWithGroupTag = builder.groupTag(groupTag).flowControl((String) null).build();
    final String uriPlain = builder.groupTag((Long) null).flowControl((String) null).build();
    final String uriWithTaggedFlowControl = builder.groupTag((Long) null).taggedFlowControl(groupTag, groupSize, null).build();
    driverBContext.imageLivenessTimeoutNs(TimeUnit.MILLISECONDS.toNanos(500));
    launch();
    TestMediaDriver driverC = null;
    Aeron clientC = null;
    TestMediaDriver driverD = null;
    Aeron clientD = null;
    Publication publication = null;
    Subscription subscription0 = null;
    Subscription subscription1 = null;
    Subscription subscription2 = null;
    Subscription subscription3 = null;
    Subscription subscription4 = null;
    Subscription subscription5 = null;
    try {
        driverC = TestMediaDriver.launch(new MediaDriver.Context().publicationTermBufferLength(TERM_BUFFER_LENGTH).aeronDirectoryName(ROOT_DIR + "C").timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(100)).errorHandler(Tests::onError).threadingMode(ThreadingMode.SHARED), testWatcher);
        clientC = Aeron.connect(new Aeron.Context().aeronDirectoryName(driverC.aeronDirectoryName()));
        driverD = TestMediaDriver.launch(new MediaDriver.Context().publicationTermBufferLength(TERM_BUFFER_LENGTH).aeronDirectoryName(ROOT_DIR + "D").timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(100)).errorHandler(Tests::onError).threadingMode(ThreadingMode.SHARED), testWatcher);
        clientD = Aeron.connect(new Aeron.Context().aeronDirectoryName(driverD.aeronDirectoryName()));
        publication = clientA.addPublication(uriWithTaggedFlowControl, STREAM_ID);
        subscription0 = clientA.addSubscription(uriPlain, STREAM_ID);
        subscription1 = clientA.addSubscription(uriPlain, STREAM_ID);
        subscription2 = clientA.addSubscription(uriPlain, STREAM_ID);
        subscription3 = clientB.addSubscription(uriWithGroupTag, STREAM_ID);
        subscription4 = clientC.addSubscription(uriWithGroupTag, STREAM_ID);
        awaitConnectionAndStatusMessages(clientA.countersReader(), subscription0, subscription1, subscription2, subscription3, subscription4);
        assertFalse(publication.isConnected());
        subscription5 = clientD.addSubscription(uriWithGroupTag, STREAM_ID);
        // Should now have 3 receivers and publication should eventually be connected.
        while (!publication.isConnected()) {
            Tests.sleep(1);
        }
        subscription5.close();
        subscription5 = null;
        // Lost a receiver and publication should eventually be disconnected.
        while (publication.isConnected()) {
            Tests.sleep(1);
        }
        subscription5 = clientD.addSubscription(uriWithGroupTag, STREAM_ID);
        // Aaaaaand reconnect.
        while (!publication.isConnected()) {
            Tests.sleep(1);
        }
    } finally {
        CloseHelper.closeAll(publication, subscription0, subscription1, subscription2, subscription3, subscription4, subscription5, clientC, clientD, driverC, driverD);
    }
}
Also used : TestMediaDriver(io.aeron.test.driver.TestMediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with TestMediaDriver

use of io.aeron.test.driver.TestMediaDriver in project Aeron by real-logic.

the class AsyncResourceTest method shouldAddAsyncPublications.

@Test
@Timeout(10)
void shouldAddAsyncPublications() {
    final Aeron.Context clientCtx = new Aeron.Context().errorHandler(Tests::onError);
    final MediaDriver.Context driverCtx = new MediaDriver.Context().dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
    try (TestMediaDriver ignore = TestMediaDriver.launch(driverCtx, testWatcher);
        Aeron aeron = Aeron.connect(clientCtx)) {
        final long registrationIdOne = aeron.asyncAddPublication(AERON_IPC, STREAM_ID);
        final long registrationIdTwo = aeron.asyncAddExclusivePublication(AERON_IPC, STREAM_ID);
        ConcurrentPublication publicationOne;
        while (null == (publicationOne = aeron.getPublication(registrationIdOne))) {
            Tests.yield();
        }
        ExclusivePublication publicationTwo;
        while (null == (publicationTwo = aeron.getExclusivePublication(registrationIdTwo))) {
            Tests.yield();
        }
        assertFalse(aeron.isCommandActive(registrationIdOne));
        assertFalse(aeron.isCommandActive(registrationIdTwo));
        assertFalse(aeron.hasActiveCommands());
        assertNotNull(publicationOne);
        assertNotNull(publicationTwo);
    } 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) SlowTest(io.aeron.test.SlowTest) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 10 with TestMediaDriver

use of io.aeron.test.driver.TestMediaDriver in project Aeron by real-logic.

the class AsyncResourceTest method shouldAsyncRemovePublication.

@Test
@Timeout(10)
void shouldAsyncRemovePublication() {
    final Aeron.Context clientCtx = new Aeron.Context().errorHandler(Tests::onError);
    final MediaDriver.Context driverCtx = new MediaDriver.Context().dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
    try (TestMediaDriver ignore = TestMediaDriver.launch(driverCtx, testWatcher);
        Aeron aeron = Aeron.connect(clientCtx)) {
        final long registrationId = aeron.asyncAddPublication(AERON_IPC, STREAM_ID);
        Publication publication;
        while (null == (publication = aeron.getPublication(registrationId))) {
            Tests.yield();
        }
        assertFalse(aeron.hasActiveCommands());
        assertEquals(registrationId, publication.registrationId());
        aeron.asyncRemovePublication(registrationId);
        assertTrue(publication.isClosed());
        assertNull(aeron.getPublication(registrationId));
    } 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) SlowTest(io.aeron.test.SlowTest) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

TestMediaDriver (io.aeron.test.driver.TestMediaDriver)42 MediaDriver (io.aeron.driver.MediaDriver)32 Test (org.junit.jupiter.api.Test)32 InterruptAfter (io.aeron.test.InterruptAfter)24 Tests (io.aeron.test.Tests)22 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)18 RegistrationException (io.aeron.exceptions.RegistrationException)16 FragmentHandler (io.aeron.logbuffer.FragmentHandler)16 SystemTestWatcher (io.aeron.test.SystemTestWatcher)16 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)16 DirectBuffer (org.agrona.DirectBuffer)14 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)14 InterruptingTestCallback (io.aeron.test.InterruptingTestCallback)12 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 Objects.requireNonNull (java.util.Objects.requireNonNull)10 MutableDirectBuffer (org.agrona.MutableDirectBuffer)10 MutableLong (org.agrona.collections.MutableLong)10 Assertions.assertNotEquals (org.junit.jupiter.api.Assertions.assertNotEquals)10 Assumptions.assumeTrue (org.junit.jupiter.api.Assumptions.assumeTrue)10