Search in sources :

Example 1 with BufferClaim

use of io.aeron.logbuffer.BufferClaim in project Aeron by real-logic.

the class BufferClaimMessageTest method shouldReceivePublishedMessageWithInterleavedAbort.

@Theory
@Test(timeout = 10000)
public void shouldReceivePublishedMessageWithInterleavedAbort(final String channel) throws Exception {
    final BufferClaim bufferClaim = new BufferClaim();
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(MESSAGE_LENGTH));
    final MediaDriver.Context ctx = new MediaDriver.Context();
    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect();
        Publication publication = aeron.addPublication(channel, STREAM_ID);
        Subscription subscription = aeron.addSubscription(channel, STREAM_ID)) {
        publishMessage(srcBuffer, publication);
        while (publication.tryClaim(MESSAGE_LENGTH, bufferClaim) < 0L) {
            Thread.yield();
        }
        publishMessage(srcBuffer, publication);
        bufferClaim.abort();
        final int expectedNumberOfFragments = 2;
        int numFragments = 0;
        do {
            numFragments += subscription.poll(mockFragmentHandler, FRAGMENT_COUNT_LIMIT);
        } while (numFragments < expectedNumberOfFragments);
        verify(mockFragmentHandler, times(expectedNumberOfFragments)).onFragment(any(DirectBuffer.class), anyInt(), eq(MESSAGE_LENGTH), any(Header.class));
    } finally {
        ctx.deleteAeronDirectory();
    }
}
Also used : DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) Header(io.aeron.logbuffer.Header) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) DataPoint(org.junit.experimental.theories.DataPoint) BufferClaim(io.aeron.logbuffer.BufferClaim) Theory(org.junit.experimental.theories.Theory) Test(org.junit.Test)

Example 2 with BufferClaim

use of io.aeron.logbuffer.BufferClaim in project Aeron by real-logic.

the class PublicationTest method shouldEnsureThePublicationIsOpenBeforeClaim.

@Test
public void shouldEnsureThePublicationIsOpenBeforeClaim() {
    publication.close();
    final BufferClaim bufferClaim = new BufferClaim();
    assertThat(publication.tryClaim(SEND_BUFFER_CAPACITY, bufferClaim), is(Publication.CLOSED));
}
Also used : BufferClaim(io.aeron.logbuffer.BufferClaim) Test(org.junit.Test)

Example 3 with BufferClaim

use of io.aeron.logbuffer.BufferClaim in project Aeron by real-logic.

the class PublicationUnblockTest method shouldUnblockNonCommittedMessage.

@Theory
@Test(timeout = 10000)
public void shouldUnblockNonCommittedMessage(final String channel) throws Exception {
    final FragmentHandler mockFragmentHandler = mock(FragmentHandler.class);
    final MediaDriver.Context ctx = new MediaDriver.Context();
    ctx.publicationUnblockTimeoutNs(TimeUnit.MILLISECONDS.toNanos(10));
    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron client = Aeron.connect(new Aeron.Context());
        Publication publicationA = client.addPublication(channel, STREAM_ID);
        Publication publicationB = client.addPublication(channel, STREAM_ID);
        Subscription subscription = client.addSubscription(channel, STREAM_ID)) {
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[ctx.mtuLength()]);
        final int length = 128;
        final BufferClaim bufferClaim = new BufferClaim();
        srcBuffer.setMemory(0, length, (byte) 66);
        while (publicationA.tryClaim(length, bufferClaim) < 0L) {
            Thread.yield();
        }
        bufferClaim.buffer().setMemory(bufferClaim.offset(), length, (byte) 65);
        bufferClaim.commit();
        while (publicationB.offer(srcBuffer, 0, length) < 0L) {
            Thread.yield();
        }
        while (publicationA.tryClaim(length, bufferClaim) < 0L) {
            Thread.yield();
        }
        while (publicationB.offer(srcBuffer, 0, length) < 0L) {
            Thread.yield();
        }
        final int expectedFragments = 3;
        int numFragments = 0;
        do {
            numFragments += subscription.poll(mockFragmentHandler, FRAGMENT_COUNT_LIMIT);
        } while (numFragments < expectedFragments);
        assertThat(numFragments, is(3));
    } finally {
        ctx.deleteAeronDirectory();
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) FragmentHandler(io.aeron.logbuffer.FragmentHandler) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) DataPoint(org.junit.experimental.theories.DataPoint) BufferClaim(io.aeron.logbuffer.BufferClaim) Theory(org.junit.experimental.theories.Theory) Test(org.junit.Test)

Example 4 with BufferClaim

use of io.aeron.logbuffer.BufferClaim in project Aeron by real-logic.

the class BufferClaimMessageTest method shouldTransferReservedValue.

@Theory
@Test(timeout = 10000)
public void shouldTransferReservedValue(final String channel) throws Exception {
    final BufferClaim bufferClaim = new BufferClaim();
    final MediaDriver.Context ctx = new MediaDriver.Context();
    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect();
        Publication publication = aeron.addPublication(channel, STREAM_ID);
        Subscription subscription = aeron.addSubscription(channel, STREAM_ID)) {
        while (publication.tryClaim(MESSAGE_LENGTH, bufferClaim) < 0L) {
            Thread.yield();
        }
        final long reservedValue = System.currentTimeMillis();
        bufferClaim.reservedValue(reservedValue);
        bufferClaim.commit();
        final boolean[] done = new boolean[1];
        while (!done[0]) {
            subscription.poll((buffer, offset, length, header) -> {
                assertThat(length, is(MESSAGE_LENGTH));
                assertThat(header.reservedValue(), is(reservedValue));
                done[0] = true;
            }, FRAGMENT_COUNT_LIMIT);
        }
    } finally {
        ctx.deleteAeronDirectory();
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) BufferClaim(io.aeron.logbuffer.BufferClaim) Theory(org.junit.experimental.theories.Theory) Test(org.junit.Test)

Aggregations

BufferClaim (io.aeron.logbuffer.BufferClaim)4 Test (org.junit.Test)4 MediaDriver (io.aeron.driver.MediaDriver)3 Theory (org.junit.experimental.theories.Theory)3 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)2 DataPoint (org.junit.experimental.theories.DataPoint)2 FragmentHandler (io.aeron.logbuffer.FragmentHandler)1 Header (io.aeron.logbuffer.Header)1 DirectBuffer (org.agrona.DirectBuffer)1