Search in sources :

Example 36 with UnsafeBuffer

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

the class RawLogFactoryTest method shouldCreateCorrectLengthAndZeroedFilesForImage.

@Test
public void shouldCreateCorrectLengthAndZeroedFilesForImage() throws Exception {
    final String canonicalForm = udpChannel.canonicalForm();
    final int imageTermBufferMaxLength = TERM_BUFFER_LENGTH / 2;
    final RawLog rawLog = rawLogFactory.newNetworkedImage(canonicalForm, SESSION_ID, STREAM_ID, CREATION_ID, imageTermBufferMaxLength);
    assertThat(rawLog.termLength(), is(imageTermBufferMaxLength));
    final UnsafeBuffer[] termBuffers = rawLog.termBuffers();
    assertThat(termBuffers.length, is(PARTITION_COUNT));
    for (final UnsafeBuffer termBuffer : termBuffers) {
        assertThat(termBuffer.capacity(), is(imageTermBufferMaxLength));
        assertThat(termBuffer.getByte(0), is((byte) 0));
        assertThat(termBuffer.getByte(imageTermBufferMaxLength - 1), is((byte) 0));
    }
    final UnsafeBuffer metaData = rawLog.metaData();
    assertThat(metaData.capacity(), is(LogBufferDescriptor.LOG_META_DATA_LENGTH));
    assertThat(metaData.getByte(0), is((byte) 0));
    assertThat(metaData.getByte(LogBufferDescriptor.LOG_META_DATA_LENGTH - 1), is((byte) 0));
    rawLog.close();
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 37 with UnsafeBuffer

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

the class RawLogFactoryTest method shouldCreateCorrectLengthAndZeroedFilesForPublication.

@Test
public void shouldCreateCorrectLengthAndZeroedFilesForPublication() throws Exception {
    final String canonicalForm = udpChannel.canonicalForm();
    final RawLog rawLog = rawLogFactory.newNetworkPublication(canonicalForm, SESSION_ID, STREAM_ID, CREATION_ID, TERM_BUFFER_LENGTH);
    assertThat(rawLog.termLength(), is(TERM_BUFFER_LENGTH));
    final UnsafeBuffer[] termBuffers = rawLog.termBuffers();
    assertThat(termBuffers.length, is(PARTITION_COUNT));
    for (final UnsafeBuffer termBuffer : termBuffers) {
        assertThat(termBuffer.capacity(), is(TERM_BUFFER_LENGTH));
        assertThat(termBuffer.getByte(0), is((byte) 0));
        assertThat(termBuffer.getByte(TERM_BUFFER_LENGTH - 1), is((byte) 0));
    }
    final UnsafeBuffer metaData = rawLog.metaData();
    assertThat(metaData.capacity(), is(LogBufferDescriptor.LOG_META_DATA_LENGTH));
    assertThat(metaData.getByte(0), is((byte) 0));
    assertThat(metaData.getByte(LogBufferDescriptor.LOG_META_DATA_LENGTH - 1), is((byte) 0));
    rawLog.close();
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 38 with UnsafeBuffer

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

the class SenderTest method shouldBeAbleToSendOnChannelTwice.

@Test
public void shouldBeAbleToSendOnChannelTwice() throws Exception {
    final StatusMessageFlyweight msg = mock(StatusMessageFlyweight.class);
    when(msg.consumptionTermId()).thenReturn(INITIAL_TERM_ID);
    when(msg.consumptionTermOffset()).thenReturn(0);
    when(msg.receiverWindowLength()).thenReturn(2 * ALIGNED_FRAME_LENGTH);
    publication.onStatusMessage(msg, rcvAddress);
    //        publication.senderPositionLimit(
    //            flowControl.onStatusMessage(INITIAL_TERM_ID, 0, (2 * ALIGNED_FRAME_LENGTH), rcvAddress));
    final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(PAYLOAD.length));
    buffer.putBytes(0, PAYLOAD);
    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null);
    sender.doWork();
    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null);
    sender.doWork();
    assertThat(receivedFrames.size(), is(3));
    setupHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));
    assertThat(setupHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_SETUP));
    dataHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));
    assertThat(dataHeader.frameLength(), is(FRAME_LENGTH));
    assertThat(dataHeader.termId(), is(INITIAL_TERM_ID));
    assertThat(dataHeader.streamId(), is(STREAM_ID));
    assertThat(dataHeader.sessionId(), is(SESSION_ID));
    assertThat(dataHeader.termOffset(), is(offsetOfMessage(1)));
    assertThat(dataHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_DATA));
    assertThat(dataHeader.flags(), is(DataHeaderFlyweight.BEGIN_AND_END_FLAGS));
    assertThat(dataHeader.version(), is((short) HeaderFlyweight.CURRENT_VERSION));
    dataHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));
    assertThat(dataHeader.frameLength(), is(FRAME_LENGTH));
    assertThat(dataHeader.termId(), is(INITIAL_TERM_ID));
    assertThat(dataHeader.streamId(), is(STREAM_ID));
    assertThat(dataHeader.sessionId(), is(SESSION_ID));
    assertThat(dataHeader.termOffset(), is(offsetOfMessage(2)));
    assertThat(dataHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_DATA));
    assertThat(dataHeader.flags(), is(DataHeaderFlyweight.BEGIN_AND_END_FLAGS));
    assertThat(dataHeader.version(), is((short) HeaderFlyweight.CURRENT_VERSION));
}
Also used : StatusMessageFlyweight(io.aeron.protocol.StatusMessageFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 39 with UnsafeBuffer

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

the class SenderTest method shouldSendLastDataFrameAsHeartbeatWhenIdle.

@Test
public void shouldSendLastDataFrameAsHeartbeatWhenIdle() throws Exception {
    final StatusMessageFlyweight msg = mock(StatusMessageFlyweight.class);
    when(msg.consumptionTermId()).thenReturn(INITIAL_TERM_ID);
    when(msg.consumptionTermOffset()).thenReturn(0);
    when(msg.receiverWindowLength()).thenReturn(ALIGNED_FRAME_LENGTH);
    publication.onStatusMessage(msg, rcvAddress);
    //        publication.senderPositionLimit(
    //            flowControl.onStatusMessage(INITIAL_TERM_ID, 0, ALIGNED_FRAME_LENGTH, rcvAddress));
    final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(PAYLOAD.length));
    buffer.putBytes(0, PAYLOAD);
    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null);
    sender.doWork();
    // should send ticks
    assertThat(receivedFrames.size(), is(2));
    // skip setup & data frame
    receivedFrames.remove();
    receivedFrames.remove();
    currentTimestamp += Configuration.PUBLICATION_HEARTBEAT_TIMEOUT_NS - 1;
    sender.doWork();
    // should not send yet
    assertThat(receivedFrames.size(), is(0));
    currentTimestamp += 10;
    sender.doWork();
    // should send ticks
    assertThat(receivedFrames.size(), greaterThanOrEqualTo(1));
    dataHeader.wrap(receivedFrames.remove());
    assertThat(dataHeader.frameLength(), is(0));
    assertThat(dataHeader.termOffset(), is(offsetOfMessage(2)));
}
Also used : StatusMessageFlyweight(io.aeron.protocol.StatusMessageFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 40 with UnsafeBuffer

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

the class SenderTest method shouldNotBeAbleToSendAfterUsingUpYourWindow.

@Test
public void shouldNotBeAbleToSendAfterUsingUpYourWindow() throws Exception {
    final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(PAYLOAD.length));
    buffer.putBytes(0, PAYLOAD);
    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null);
    final StatusMessageFlyweight msg = mock(StatusMessageFlyweight.class);
    when(msg.consumptionTermId()).thenReturn(INITIAL_TERM_ID);
    when(msg.consumptionTermOffset()).thenReturn(0);
    when(msg.receiverWindowLength()).thenReturn(ALIGNED_FRAME_LENGTH);
    publication.onStatusMessage(msg, rcvAddress);
    //        publication.senderPositionLimit(
    //            flowControl.onStatusMessage(INITIAL_TERM_ID, 0, ALIGNED_FRAME_LENGTH, rcvAddress));
    sender.doWork();
    assertThat(receivedFrames.size(), is(2));
    // skip setup
    receivedFrames.remove();
    dataHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));
    assertThat(dataHeader.frameLength(), is(FRAME_LENGTH));
    assertThat(dataHeader.termId(), is(INITIAL_TERM_ID));
    assertThat(dataHeader.streamId(), is(STREAM_ID));
    assertThat(dataHeader.sessionId(), is(SESSION_ID));
    assertThat(dataHeader.termOffset(), is(offsetOfMessage(1)));
    assertThat(dataHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_DATA));
    assertThat(dataHeader.flags(), is(DataHeaderFlyweight.BEGIN_AND_END_FLAGS));
    assertThat(dataHeader.version(), is((short) HeaderFlyweight.CURRENT_VERSION));
    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null);
    sender.doWork();
    assertThat(receivedFrames.size(), is(0));
}
Also used : StatusMessageFlyweight(io.aeron.protocol.StatusMessageFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) 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