Search in sources :

Example 26 with TestMediaDriver

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

the class SpecifiedPositionPublicationTest method shouldStartAtSpecifiedPositionForPublications.

@InterruptAfter(5)
@ParameterizedTest
@CsvSource({ CommonContext.IPC_CHANNEL + ",true", "aeron:udp?endpoint=localhost:24325,true", CommonContext.IPC_CHANNEL + ",false", "aeron:udp?endpoint=localhost:24325,false" })
void shouldStartAtSpecifiedPositionForPublications(final String initialUri, final boolean exclusive) {
    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 PositionCalculator positionCalculator = new PositionCalculator(startPosition, termLength, termOffset);
    final long nextPosition = positionCalculator.addMessage(DataHeaderFlyweight.HEADER_LENGTH + msg.capacity());
    final String channel = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId, termLength).build();
    final int streamId = 1001;
    final Function<Aeron, Publication> publicationSupplier = exclusive ? (a) -> a.addExclusivePublication(channel, streamId) : (a) -> a.addPublication(channel, streamId);
    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 = publicationSupplier.apply(aeron)) {
        Tests.awaitConnected(subscription);
        Tests.awaitConnected(publication);
        assertEquals(startPosition, publication.position());
        Tests.await(() -> publication.offer(msg) > 0);
        assertEquals(nextPosition, publication.position());
        final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> assertEquals(nextPosition, header.position());
        Tests.await(() -> subscription.poll(fragmentHandler, 1) == 1);
    } 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 27 with TestMediaDriver

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

the class TimestampingSystemTest method shouldSupportMediaReceiveTimestampsInCDriver.

@Test
@InterruptAfter(10)
@EnabledOnOs(OS.LINUX)
void shouldSupportMediaReceiveTimestampsInCDriver() {
    assumeTrue(TestMediaDriver.shouldRunCMediaDriver());
    final DirectBuffer buffer = new UnsafeBuffer(new byte[64]);
    try (TestMediaDriver driver = driver();
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(driver.aeronDirectoryName()))) {
        final Subscription sub = aeron.addSubscription(CHANNEL_WITH_MEDIA_TIMESTAMP, 1000);
        while (null == sub.resolvedEndpoint()) {
            Tests.yieldingIdle("Failed to resolve endpoint");
        }
        final String uri = "aeron:udp?endpoint=" + sub.resolvedEndpoint();
        final Publication pub = aeron.addPublication(uri, 1000);
        Tests.awaitConnected(pub);
        while (0 > pub.offer(buffer, 0, buffer.capacity(), (termBuffer, termOffset, frameLength) -> SENTINEL_VALUE)) {
            Tests.yieldingIdle("Failed to offer message");
        }
        final FragmentHandler fragmentHandler = (buffer1, offset, length, header) -> assertNotEquals(SENTINEL_VALUE, header.reservedValue());
        while (1 > sub.poll(fragmentHandler, 1)) {
            Tests.yieldingIdle("Failed to receive message");
        }
    }
}
Also used : MutableDirectBuffer(org.agrona.MutableDirectBuffer) DirectBuffer(org.agrona.DirectBuffer) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) BeforeEach(org.junit.jupiter.api.BeforeEach) RegistrationException(io.aeron.exceptions.RegistrationException) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) OS(org.junit.jupiter.api.condition.OS) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) MutableLong(org.agrona.collections.MutableLong) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Objects.requireNonNull(java.util.Objects.requireNonNull) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) MutableDirectBuffer(org.agrona.MutableDirectBuffer) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) DirectBuffer(org.agrona.DirectBuffer) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 28 with TestMediaDriver

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

the class TimestampingSystemTest method shouldErrorIfSubscriptionConfigurationForTimestampsDoesNotMatch.

@Test
void shouldErrorIfSubscriptionConfigurationForTimestampsDoesNotMatch() {
    systemTestWatcher.ignoreErrorsMatching((s) -> s.contains("option conflicts"));
    try (TestMediaDriver driver = driver();
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(driver.aeronDirectoryName()))) {
        aeron.addSubscription("aeron:udp?endpoint=localhost:23436|channel-rcv-ts-offset=reserved", 1000);
        assertThrows(RegistrationException.class, () -> aeron.addSubscription("aeron:udp?endpoint=localhost:23436", 1000));
        assertThrows(RegistrationException.class, () -> aeron.addSubscription("aeron:udp?endpoint=localhost:23436|channel-rcv-ts-offset=8", 1000));
    }
}
Also used : TestMediaDriver(io.aeron.test.driver.TestMediaDriver) Test(org.junit.jupiter.api.Test)

Example 29 with TestMediaDriver

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

the class TimestampingSystemTest method shouldSupportReceiveTimestampsOnMds.

@Test
@InterruptAfter(10)
void shouldSupportReceiveTimestampsOnMds() {
    final MutableDirectBuffer buffer = new UnsafeBuffer(new byte[64]);
    try (TestMediaDriver driver = driver();
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(driver.aeronDirectoryName()))) {
        final Subscription mdsSub = aeron.addSubscription("aeron:udp?control-mode=manual|channel-rcv-ts-offset=0", 1000);
        final Publication pub1 = aeron.addPublication("aeron:udp?endpoint=localhost:23424", 1000);
        final Publication pub2 = aeron.addPublication("aeron:udp?endpoint=localhost:23425", 1000);
        mdsSub.addDestination("aeron:udp?endpoint=localhost:23424");
        mdsSub.addDestination("aeron:udp?endpoint=localhost:23425");
        while (!pub1.isConnected() || !pub2.isConnected()) {
            Tests.yieldingIdle("Failed to connect");
        }
        buffer.putLong(0, SENTINEL_VALUE);
        while (0 > pub1.offer(buffer, 0, buffer.capacity())) {
            Tests.yieldingIdle("Failed to offer message");
        }
        while (0 > pub2.offer(buffer, 0, buffer.capacity())) {
            Tests.yieldingIdle("Failed to offer message");
        }
        final MutableLong sendTimestamp = new MutableLong(SENTINEL_VALUE);
        final FragmentHandler fragmentHandler = (buffer1, offset, length, header) -> sendTimestamp.set(buffer1.getLong(offset));
        while (1 > mdsSub.poll(fragmentHandler, 1)) {
            Tests.yieldingIdle("Failed to receive message");
        }
        assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
        while (1 > mdsSub.poll(fragmentHandler, 1)) {
            Tests.yieldingIdle("Failed to receive message");
        }
        assertNotEquals(SENTINEL_VALUE, sendTimestamp.longValue());
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MediaDriver(io.aeron.driver.MediaDriver) SystemTestWatcher(io.aeron.test.SystemTestWatcher) Tests(io.aeron.test.Tests) BeforeEach(org.junit.jupiter.api.BeforeEach) RegistrationException(io.aeron.exceptions.RegistrationException) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) OS(org.junit.jupiter.api.condition.OS) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) MutableLong(org.agrona.collections.MutableLong) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Objects.requireNonNull(java.util.Objects.requireNonNull) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) MutableDirectBuffer(org.agrona.MutableDirectBuffer) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) DirectBuffer(org.agrona.DirectBuffer) MutableLong(org.agrona.collections.MutableLong) FragmentHandler(io.aeron.logbuffer.FragmentHandler) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MutableDirectBuffer(org.agrona.MutableDirectBuffer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 30 with TestMediaDriver

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

the class MinFlowControlSystemTest method shouldPreventConnectionUntilGroupMinSizeIsMet.

@SlowTest
@Test
@InterruptAfter(20)
void shouldPreventConnectionUntilGroupMinSizeIsMet() {
    final Integer groupSize = 3;
    final ChannelUriStringBuilder builder = new ChannelUriStringBuilder().media("udp").endpoint("224.20.30.39:24326").networkInterface("localhost");
    final String uriPlain = builder.flowControl((String) null).build();
    final String uriWithMinFlowControl = builder.groupTag((Long) null).minFlowControl(groupSize, null).build();
    driverBContext.imageLivenessTimeoutNs(TimeUnit.MILLISECONDS.toNanos(500));
    launch();
    final CountersReader countersReader = clientA.countersReader();
    TestMediaDriver driverC = null;
    Aeron clientC = null;
    Publication publication = null;
    Subscription subscription0 = null;
    Subscription subscription1 = null;
    Subscription subscription2 = 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()));
        subscription0 = clientA.addSubscription(uriPlain, STREAM_ID);
        subscription1 = clientB.addSubscription(uriPlain, STREAM_ID);
        publication = clientA.addPublication(uriWithMinFlowControl, STREAM_ID);
        awaitConnectionAndStatusMessages(countersReader, subscription0, subscription1);
        assertFalse(publication.isConnected());
        subscription2 = clientC.addSubscription(uriPlain, STREAM_ID);
        // Should now have 3 receivers and publication should eventually be connected.
        while (!publication.isConnected()) {
            Tests.sleep(1);
        }
        subscription2.close();
        subscription2 = null;
        // Lost a receiver and publication should eventually be disconnected.
        while (publication.isConnected()) {
            Tests.sleep(1);
        }
        subscription2 = clientC.addSubscription(uriPlain, STREAM_ID);
        while (!publication.isConnected()) {
            Tests.sleep(1);
        }
    } finally {
        CloseHelper.closeAll(publication, subscription0, subscription1, subscription2, clientC, driverC);
    }
}
Also used : TestMediaDriver(io.aeron.test.driver.TestMediaDriver) MediaDriver(io.aeron.driver.MediaDriver) TestMediaDriver(io.aeron.test.driver.TestMediaDriver) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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