Search in sources :

Example 61 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class TermAppenderTest method shouldAppendFrameTwiceToLog.

@Test
public void shouldAppendFrameTwiceToLog() {
    final int headerLength = DEFAULT_HEADER.capacity();
    final UnsafeBuffer buffer = new UnsafeBuffer(new byte[128]);
    final int msgLength = 20;
    final int frameLength = msgLength + headerLength;
    final int alignedFrameLength = align(frameLength, FRAME_ALIGNMENT);
    int tail = 0;
    logMetaDataBuffer.putLong(TERM_TAIL_COUNTER_OFFSET, pack(TERM_ID, tail));
    assertThat(termAppender.appendUnfragmentedMessage(headerWriter, buffer, 0, msgLength, RVS), is((long) alignedFrameLength));
    assertThat(termAppender.appendUnfragmentedMessage(headerWriter, buffer, 0, msgLength, RVS), is((long) alignedFrameLength * 2));
    assertThat(rawTailVolatile(logMetaDataBuffer, PARTITION_INDEX), is(pack(TERM_ID, tail + (alignedFrameLength * 2))));
    final InOrder inOrder = inOrder(termBuffer, headerWriter);
    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, frameLength, TERM_ID);
    inOrder.verify(termBuffer, times(1)).putBytes(headerLength, buffer, 0, msgLength);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameLength);
    tail = alignedFrameLength;
    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, frameLength, TERM_ID);
    inOrder.verify(termBuffer, times(1)).putBytes(tail + headerLength, buffer, 0, msgLength);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameLength);
}
Also used : InOrder(org.mockito.InOrder) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 62 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class TermAppenderTest method shouldFragmentMessageOverTwoFrames.

@Test
public void shouldFragmentMessageOverTwoFrames() {
    final int msgLength = MAX_PAYLOAD_LENGTH + 1;
    final int headerLength = DEFAULT_HEADER.capacity();
    final int frameLength = headerLength + 1;
    final int requiredCapacity = align(headerLength + 1, FRAME_ALIGNMENT) + MAX_FRAME_LENGTH;
    final UnsafeBuffer buffer = new UnsafeBuffer(new byte[msgLength]);
    int tail = 0;
    logMetaDataBuffer.putLong(TERM_TAIL_COUNTER_OFFSET, pack(TERM_ID, tail));
    assertThat(termAppender.appendFragmentedMessage(headerWriter, buffer, 0, msgLength, MAX_PAYLOAD_LENGTH, RVS), is((long) requiredCapacity));
    assertThat(rawTailVolatile(logMetaDataBuffer, PARTITION_INDEX), is(pack(TERM_ID, tail + requiredCapacity)));
    final InOrder inOrder = inOrder(termBuffer, headerWriter);
    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, MAX_FRAME_LENGTH, TERM_ID);
    inOrder.verify(termBuffer, times(1)).putBytes(tail + headerLength, buffer, 0, MAX_PAYLOAD_LENGTH);
    inOrder.verify(termBuffer, times(1)).putByte(flagsOffset(tail), BEGIN_FRAG_FLAG);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, MAX_FRAME_LENGTH);
    tail = MAX_FRAME_LENGTH;
    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, frameLength, TERM_ID);
    inOrder.verify(termBuffer, times(1)).putBytes(tail + headerLength, buffer, MAX_PAYLOAD_LENGTH, 1);
    inOrder.verify(termBuffer, times(1)).putByte(flagsOffset(tail), END_FRAG_FLAG);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameLength);
}
Also used : InOrder(org.mockito.InOrder) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 63 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer 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 64 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class SpySubscriptionTest method shouldReceivePublishedMessage.

@Theory
@Test(timeout = 10000)
public void shouldReceivePublishedMessage(final String channel) throws Exception {
    final MediaDriver.Context ctx = new MediaDriver.Context();
    final Aeron.Context aeronCtx = new Aeron.Context();
    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect(aeronCtx);
        Publication publication = aeron.addPublication(channel, STREAM_ID);
        Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
        Subscription spy = aeron.addSubscription(spyForChannel(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) {
                Thread.yield();
            }
        }
        int numFragments = 0;
        int numSpyFragments = 0;
        do {
            numFragments += subscription.poll(mockFragmentHandler, FRAGMENT_COUNT_LIMIT);
            numSpyFragments += spy.poll(mockSpyFragmentHandler, FRAGMENT_COUNT_LIMIT);
        } while (numSpyFragments < 4 || numFragments < 4);
        verify(mockFragmentHandler, times(4)).onFragment(any(DirectBuffer.class), anyInt(), eq(PAYLOAD_LENGTH), any(Header.class));
        verify(mockSpyFragmentHandler, times(4)).onFragment(any(DirectBuffer.class), anyInt(), eq(PAYLOAD_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) Theory(org.junit.experimental.theories.Theory) Test(org.junit.Test)

Example 65 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class ControlledMessageTest method shouldReceivePublishedMessage.

@Theory
@Test(timeout = 10000)
public void shouldReceivePublishedMessage() throws Exception {
    final MediaDriver.Context ctx = new MediaDriver.Context();
    ctx.threadingMode(ThreadingMode.SHARED);
    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect();
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID);
        Subscription subscription = aeron.addSubscription(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) {
                Thread.yield();
            }
        }
        final FragmentCollector fragmentCollector = new FragmentCollector();
        int numFragments = 0;
        do {
            numFragments += subscription.controlledPoll(fragmentCollector, FRAGMENT_COUNT_LIMIT);
        } while (numFragments < 4);
        final UnsafeBuffer collectedBuffer = fragmentCollector.collectedBuffer();
        for (int i = 0; i < srcBuffer.capacity(); i++) {
            assertThat("same at i=" + i, collectedBuffer.getByte(i), is(srcBuffer.getByte(i)));
        }
    } finally {
        ctx.deleteAeronDirectory();
    }
}
Also used : MediaDriver(io.aeron.driver.MediaDriver) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Theory(org.junit.experimental.theories.Theory) Test(org.junit.Test)

Aggregations

UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)78 Test (org.junit.Test)42 StatusMessageFlyweight (io.aeron.protocol.StatusMessageFlyweight)8 MediaDriver (io.aeron.driver.MediaDriver)7 MutableDirectBuffer (org.agrona.MutableDirectBuffer)6 Header (io.aeron.logbuffer.Header)5 InOrder (org.mockito.InOrder)5 MappedByteBuffer (java.nio.MappedByteBuffer)4 Theory (org.junit.experimental.theories.Theory)4 ReceiveChannelEndpoint (io.aeron.driver.media.ReceiveChannelEndpoint)3 File (java.io.File)3 Date (java.util.Date)3 DirectBuffer (org.agrona.DirectBuffer)3 Before (org.junit.Before)3 DataPoint (org.junit.experimental.theories.DataPoint)3 SendChannelEndpoint (io.aeron.driver.media.SendChannelEndpoint)2 BufferClaim (io.aeron.logbuffer.BufferClaim)2 FragmentHandler (io.aeron.logbuffer.FragmentHandler)2 PrintStream (java.io.PrintStream)2 InetSocketAddress (java.net.InetSocketAddress)2