org.agrona.concurrent.UnsafeBuffer Java Examples

The following examples show how to use org.agrona.concurrent.UnsafeBuffer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImageTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldPollNoFragmentsToBoundedControlledFragmentHandlerWithMaxPositionBeforeInitialPosition()
{
    final long initialPosition = computePosition(INITIAL_TERM_ID, 0, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID);
    final long maxPosition = initialPosition - HEADER_LENGTH;
    position.setOrdered(initialPosition);
    final Image image = createImage();

    insertDataFrame(INITIAL_TERM_ID, offsetForFrame(0));
    insertDataFrame(INITIAL_TERM_ID, offsetForFrame(1));

    when(mockControlledFragmentHandler.onFragment(any(DirectBuffer.class), anyInt(), anyInt(), any(Header.class)))
        .thenReturn(Action.CONTINUE);

    final int fragmentsRead = image.boundedControlledPoll(
        mockControlledFragmentHandler, maxPosition, Integer.MAX_VALUE);

    assertThat(fragmentsRead, is(0));

    assertThat(position.get(), is(initialPosition));
    verify(mockControlledFragmentHandler, never()).onFragment(
        any(UnsafeBuffer.class), anyInt(), anyInt(), any(Header.class));
}
 
Example #2
Source File: BufferBuilderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAppendTwoBuffersWithoutResizing()
{
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[MIN_ALLOCATED_CAPACITY]);
    final byte[] bytes = "1111111122222222".getBytes(StandardCharsets.UTF_8);
    srcBuffer.putBytes(0, bytes, 0, bytes.length);

    bufferBuilder.append(srcBuffer, 0, bytes.length / 2);
    bufferBuilder.append(srcBuffer, bytes.length / 2, bytes.length / 2);

    final byte[] temp = new byte[bytes.length];
    bufferBuilder.buffer().getBytes(0, temp, 0, bytes.length);

    assertEquals(bytes.length, bufferBuilder.limit());
    assertEquals(MIN_ALLOCATED_CAPACITY, bufferBuilder.capacity());
    assertArrayEquals(temp, bytes);
}
 
Example #3
Source File: BufferBuilderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAppendOneBufferWithoutResizing()
{
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[MIN_ALLOCATED_CAPACITY]);
    final byte[] bytes = "Hello World".getBytes(StandardCharsets.UTF_8);
    srcBuffer.putBytes(0, bytes, 0, bytes.length);

    bufferBuilder.append(srcBuffer, 0, bytes.length);

    final byte[] temp = new byte[bytes.length];
    bufferBuilder.buffer().getBytes(0, temp, 0, bytes.length);

    assertEquals(bytes.length, bufferBuilder.limit());
    assertEquals(MIN_ALLOCATED_CAPACITY, bufferBuilder.capacity());
    assertArrayEquals(temp, bytes);
}
 
Example #4
Source File: ManyToOneRingBufferTest.java    From agrona with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInsertPaddingAndWriteToBuffer()
{
    final int padding = 200;
    final int messageLength = 400;
    final int recordLength = messageLength + HEADER_LENGTH;
    final int alignedRecordLength = align(recordLength, ALIGNMENT);

    final long tail = 2 * CAPACITY - padding;
    final long head = tail;

    // free space is (200 + 300) more than message length (400)
    // but contiguous space (300) is less than message length (400)
    final long headCache = CAPACITY + 300;

    when(buffer.getLongVolatile(HEAD_COUNTER_INDEX)).thenReturn(head);
    when(buffer.getLongVolatile(TAIL_COUNTER_INDEX)).thenReturn(tail);
    when(buffer.getLongVolatile(HEAD_COUNTER_CACHE_INDEX)).thenReturn(headCache);
    when(buffer.compareAndSetLong(TAIL_COUNTER_INDEX, tail, tail + alignedRecordLength + padding))
        .thenReturn(true);

    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[messageLength]);

    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, messageLength));
}
 
Example #5
Source File: EncoderGenerator.java    From artio with Apache License 2.0 6 votes vote down vote up
protected void generateAggregateFile(final Aggregate aggregate, final AggregateType aggregateType)
{
    final String className = encoderClassName(aggregate.name());

    outputManager.withOutput(
        className,
        (out) ->
        {
            out.append(fileHeader(thisPackage));

            if (REQUIRED_SESSION_CODECS.contains(className))
            {
                out.append(importFor("uk.co.real_logic.artio.builder.Abstract" + className));
            }

            generateImports(
                "Encoder",
                aggregateType,
                out,
                DirectBuffer.class,
                MutableDirectBuffer.class,
                UnsafeBuffer.class,
                AsciiSequenceView.class);
            generateAggregateClass(aggregate, aggregateType, className, out);
        });
}
 
Example #6
Source File: ReplaySessionTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotWritePaddingIfReplayLimitReached()
{
    try (ReplaySession replaySession = replaySession(
        RECORDING_POSITION,
        1500,
        1L,
        mockReplayPub,
        mockControlSession,
        null,
        null))
    {
        when(mockReplayPub.isConnected()).thenReturn(true);
        final UnsafeBuffer termBuffer = new UnsafeBuffer(allocateDirectAligned(4096, 64));
        mockPublication(mockReplayPub, termBuffer);

        assertEquals(2, replaySession.doWork());

        validateFrame(termBuffer, 0, FRAME_LENGTH, 0, UNFRAGMENTED, 0, 0);
        validateFrame(termBuffer, FRAME_LENGTH, FRAME_LENGTH, 1, BEGIN_FRAG_FLAG, 0, 0);
        validateFrame(termBuffer, FRAME_LENGTH * 2, 0, 0, (byte)0, 0, 0);
        verify(mockReplayPub).offerBlock(any(MutableDirectBuffer.class), eq(0), eq(2 * FRAME_LENGTH));
        verify(mockReplayPub, never()).appendPadding(anyInt());
        assertTrue(replaySession.isDone());
    }
}
 
Example #7
Source File: TermRebuilderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFillGapButNotMoveTailOrHwm()
{
    final int frameLength = 50;
    final int alignedFrameLength = BitUtil.align(frameLength, FRAME_ALIGNMENT);
    final int srcOffset = 0;
    final UnsafeBuffer packet = new UnsafeBuffer(ByteBuffer.allocateDirect(alignedFrameLength));
    final int termOffset = alignedFrameLength * 2;

    TermRebuilder.insert(termBuffer, termOffset, packet, alignedFrameLength);

    verify(termBuffer).putBytes(
        (alignedFrameLength * 2) + HEADER_LENGTH,
        packet,
        srcOffset + HEADER_LENGTH,
        alignedFrameLength - HEADER_LENGTH);
}
 
Example #8
Source File: DriverEventLogger.java    From aeron with Apache License 2.0 6 votes vote down vote up
public void logSubscriptionRemoval(final String uri, final int streamId, final long id)
{
    final int length = SIZE_OF_INT * 2 + SIZE_OF_LONG + uri.length();
    final int captureLength = captureLength(length);
    final int encodedLength = encodedLength(captureLength);

    final ManyToOneRingBuffer ringBuffer = this.ringBuffer;
    final int index = ringBuffer.tryClaim(toEventCodeId(REMOVE_SUBSCRIPTION_CLEANUP), encodedLength);
    if (index > 0)
    {
        try
        {
            final UnsafeBuffer buffer = (UnsafeBuffer)ringBuffer.buffer();
            encodeSubscriptionRemoval(buffer, index, captureLength, length, uri, streamId, id);
        }
        finally
        {
            ringBuffer.commit(index);
        }
    }
}
 
Example #9
Source File: ImageTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReportCorrectPositionOnReceptionWithNonZeroPositionInInitialTermId()
{
    final int initialMessageIndex = 5;
    final int initialTermOffset = offsetForFrame(initialMessageIndex);
    final long initialPosition = computePosition(
        INITIAL_TERM_ID, initialTermOffset, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID);

    position.setOrdered(initialPosition);
    final Image image = createImage();

    insertDataFrame(INITIAL_TERM_ID, offsetForFrame(initialMessageIndex));

    final int messages = image.poll(mockFragmentHandler, Integer.MAX_VALUE);
    assertThat(messages, is(1));

    verify(mockFragmentHandler).onFragment(
        any(UnsafeBuffer.class),
        eq(initialTermOffset + HEADER_LENGTH),
        eq(DATA.length),
        any(Header.class));

    final InOrder inOrder = Mockito.inOrder(position);
    inOrder.verify(position).setOrdered(initialPosition);
    inOrder.verify(position).setOrdered(initialPosition + ALIGNED_FRAME_LENGTH);
}
 
Example #10
Source File: TermAppenderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldPadLogWhenAppendingWithInsufficientRemainingCapacity()
{
    final int msgLength = 120;
    final int headerLength = DEFAULT_HEADER.capacity();
    final int requiredFrameSize = align(headerLength + msgLength, FRAME_ALIGNMENT);
    final int tailValue = TERM_BUFFER_LENGTH - align(msgLength, FRAME_ALIGNMENT);
    final UnsafeBuffer buffer = new UnsafeBuffer(new byte[128]);
    final int frameLength = TERM_BUFFER_LENGTH - tailValue;

    logMetaDataBuffer.putLong(TERM_TAIL_COUNTER_OFFSET, packTail(TERM_ID, tailValue));

    assertEquals(FAILED, termAppender.appendUnfragmentedMessage(headerWriter, buffer, 0, msgLength, RVS, TERM_ID));
    assertEquals(
        packTail(TERM_ID, tailValue + requiredFrameSize), rawTailVolatile(logMetaDataBuffer, PARTITION_INDEX));

    final InOrder inOrder = inOrder(termBuffer, headerWriter);
    inOrder.verify(headerWriter, times(1)).write(termBuffer, tailValue, frameLength, TERM_ID);
    inOrder.verify(termBuffer, times(1)).putShort(typeOffset(tailValue), (short)PADDING_FRAME_TYPE, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tailValue, frameLength);
}
 
Example #11
Source File: AeronUtil.java    From benchmarks with Apache License 2.0 6 votes vote down vote up
static int sendMessages(
    final ExclusivePublication publication,
    final UnsafeBuffer offerBuffer,
    final int numberOfMessages,
    final int messageLength,
    final long timestamp,
    final long checksum)
{
    int count = 0;
    for (int i = 0; i < numberOfMessages; i++)
    {
        offerBuffer.putLong(0, timestamp, LITTLE_ENDIAN);
        offerBuffer.putLong(messageLength - SIZE_OF_LONG, checksum, LITTLE_ENDIAN);

        final long result = publication.offer(offerBuffer, 0, messageLength, null);
        if (result < 0)
        {
            checkPublicationResult(result);
            break;
        }
        count++;
    }

    return count;
}
 
Example #12
Source File: ManyToOneRingBufferBenchmark.java    From benchmarks with Apache License 2.0 6 votes vote down vote up
@Setup
public synchronized void setup()
{
    for (int i = 0; i < MAX_THREAD_COUNT; i++)
    {
        responseQueues[i] = new OneToOneConcurrentArrayQueue<>(RESPONSE_QUEUE_CAPACITY);
    }

    values = new int[burstLength];
    for (int i = 0; i < burstLength; i++)
    {
        values[i] = -(burstLength - i);
    }

    ringBuffer = new ManyToOneRingBuffer(new UnsafeBuffer(ByteBuffer.allocateDirect(BUFFER_LENGTH)));

    consumerThread = new Thread(new Subscriber(ringBuffer, running, responseQueues));

    consumerThread.setName("consumer");
    consumerThread.start();
}
 
Example #13
Source File: ReplayIndex.java    From artio with Apache License 2.0 6 votes vote down vote up
SessionIndex(final long fixSessionId)
{
    replayIndexFile = replayIndexFile(fixSessionId);
    final boolean exists = replayIndexFile.exists();
    this.wrappedBuffer = bufferFactory.map(replayIndexFile, indexFileSize);
    this.buffer = new UnsafeBuffer(wrappedBuffer);

    recordCapacity = recordCapacity(buffer.capacity());
    if (!exists)
    {
        indexHeaderEncoder
            .wrap(buffer, 0)
            .blockLength(replayIndexRecord.sbeBlockLength())
            .templateId(replayIndexRecord.sbeTemplateId())
            .schemaId(replayIndexRecord.sbeSchemaId())
            .version(replayIndexRecord.sbeSchemaVersion());
    }
    else
    {
        // Reset the positions in order to avoid wraps at the start.
        final long resetPosition = beginChange(buffer);
        endChangeOrdered(buffer, resetPosition);
    }
}
 
Example #14
Source File: ArchiveEventLogger.java    From aeron with Apache License 2.0 6 votes vote down vote up
private void log(
    final ArchiveEventCode eventCode, final DirectBuffer srcBuffer, final int srcOffset, final int length)
{
    final int captureLength = captureLength(length);
    final int encodedLength = encodedLength(captureLength);
    final ManyToOneRingBuffer ringBuffer = this.ringBuffer;
    final int index = ringBuffer.tryClaim(toEventCodeId(eventCode), encodedLength);

    if (index > 0)
    {
        try
        {
            encode((UnsafeBuffer)ringBuffer.buffer(), index, captureLength, length, srcBuffer, srcOffset);
        }
        finally
        {
            ringBuffer.commit(index);
        }
    }
}
 
Example #15
Source File: RocksDb.java    From benchmarks with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("PMD.CloseResource")
public void setup(final BenchmarkParams b) throws IOException {
  super.setup(b);
  wkb = new UnsafeBuffer(new byte[keySize]);
  wvb = new UnsafeBuffer(new byte[valSize]);
  loadLibrary();
  final Options options = new Options();
  options.setCreateIfMissing(true);
  options.setCompressionType(NO_COMPRESSION);
  try {
    db = open(options, tmp.getAbsolutePath());
  } catch (final RocksDBException ex) {
    throw new IOException(ex);
  }
}
 
Example #16
Source File: ArchiveMarkFile.java    From aeron with Apache License 2.0 6 votes vote down vote up
public ArchiveMarkFile(
    final File directory,
    final String filename,
    final EpochClock epochClock,
    final long timeoutMs,
    final IntConsumer versionCheck,
    final Consumer<String> logger)
{
    markFile = new MarkFile(
        directory,
        filename,
        MarkFileHeaderDecoder.versionEncodingOffset(),
        MarkFileHeaderDecoder.activityTimestampEncodingOffset(),
        timeoutMs,
        epochClock,
        versionCheck,
        logger);

    buffer = markFile.buffer();
    headerEncoder.wrap(buffer, 0);
    headerDecoder.wrap(buffer, 0, MarkFileHeaderDecoder.BLOCK_LENGTH, MarkFileHeaderDecoder.SCHEMA_VERSION);

    errorBuffer = headerDecoder.errorBufferLength() > 0 ?
        new UnsafeBuffer(buffer, headerDecoder.headerLength(), headerDecoder.errorBufferLength()) :
        new UnsafeBuffer(buffer, 0, 0);
}
 
Example #17
Source File: RocksDbStorage.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Add an ndarray to the storage
 *
 * @param array the array to add
 */
@Override
public void addUpdate(NDArrayMessage array) {
    UnsafeBuffer directBuffer = (UnsafeBuffer) NDArrayMessage.toBuffer(array);
    byte[] data = directBuffer.byteArray();
    if (data == null) {
        data = new byte[directBuffer.capacity()];
        directBuffer.getBytes(0, data, 0, data.length);
    }
    byte[] key = ByteBuffer.allocate(4).putInt(size).array();
    try {
        db.put(key, data);
    } catch (RocksDBException e) {
        throw new RuntimeException(e);
    }

    size++;

}
 
Example #18
Source File: ArchiveTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private void validateRecordingFragment(
    final UnsafeBuffer buffer,
    final int offset,
    final int length,
    @SuppressWarnings("unused") final int frameType,
    @SuppressWarnings("unused") final byte flags,
    @SuppressWarnings("unused") final long reservedValue)
{
    if (!FrameDescriptor.isPaddingFrame(buffer, offset - HEADER_LENGTH))
    {
        final int expectedLength = messageLengths[messageCount] - HEADER_LENGTH;
        if (length != expectedLength)
        {
            fail("Message length=" + length + " expected=" + expectedLength + " messageCount=" + messageCount);
        }

        assertEquals(messageCount, buffer.getInt(offset));
        assertEquals((byte)'z', buffer.getByte(offset + 4));

        remaining -= BitUtil.align(messageLengths[messageCount], FrameDescriptor.FRAME_ALIGNMENT);
        messageCount++;
    }
}
 
Example #19
Source File: LogBuffers.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Duplicate the underlying {@link ByteBuffer}s and wrap them for thread local access.
 *
 * @return duplicates of the wrapped underlying {@link ByteBuffer}s.
 */
public UnsafeBuffer[] duplicateTermBuffers()
{
    final UnsafeBuffer[] buffers = new UnsafeBuffer[PARTITION_COUNT];

    for (int i = 0; i < PARTITION_COUNT; i++)
    {
        buffers[i] = new UnsafeBuffer(termBuffers[i].duplicate().order(ByteOrder.LITTLE_ENDIAN));
    }

    return buffers;
}
 
Example #20
Source File: PublishFromArbitraryPositionTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void publishMessage(
    final UnsafeBuffer buffer, final ExclusivePublication publication, final Random rnd)
{
    while (publication.offer(buffer, 0, 1 + rnd.nextInt(MAX_MESSAGE_LENGTH - 1)) < 0L)
    {
        Tests.yield();
    }
}
 
Example #21
Source File: JavaGeneratorTest.java    From simple-binary-encoding with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGenerateBasicMessage() throws Exception
{
    final UnsafeBuffer buffer = new UnsafeBuffer(new byte[4096]);
    generator().generate();

    final Object msgFlyweight = wrap(buffer, compileCarEncoder().getConstructor().newInstance());

    final Object groupFlyweight = fuelFiguresCount(msgFlyweight, 0);

    assertNotNull(groupFlyweight);

    assertThat(msgFlyweight.toString(), startsWith("[Car]"));
}
 
Example #22
Source File: ImageTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUpdatePositionToEndOfCommittedFragmentOnCommit()
{
    final long initialPosition = computePosition(INITIAL_TERM_ID, 0, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID);
    position.setOrdered(initialPosition);
    final Image image = createImage();

    insertDataFrame(INITIAL_TERM_ID, offsetForFrame(0));
    insertDataFrame(INITIAL_TERM_ID, offsetForFrame(1));
    insertDataFrame(INITIAL_TERM_ID, offsetForFrame(2));

    when(mockControlledFragmentHandler.onFragment(any(DirectBuffer.class), anyInt(), anyInt(), any(Header.class)))
        .thenReturn(Action.CONTINUE, Action.COMMIT, Action.CONTINUE);

    final int fragmentsRead = image.controlledPoll(mockControlledFragmentHandler, Integer.MAX_VALUE);

    assertThat(fragmentsRead, is(3));

    final InOrder inOrder = Mockito.inOrder(position, mockControlledFragmentHandler);
    // first fragment, continue
    inOrder.verify(mockControlledFragmentHandler).onFragment(
        any(UnsafeBuffer.class), eq(HEADER_LENGTH), eq(DATA.length), any(Header.class));

    // second fragment, commit
    inOrder.verify(mockControlledFragmentHandler).onFragment(
        any(UnsafeBuffer.class),
        eq(ALIGNED_FRAME_LENGTH + HEADER_LENGTH),
        eq(DATA.length),
        any(Header.class));
    inOrder.verify(position).setOrdered(initialPosition + (ALIGNED_FRAME_LENGTH * 2));

    // third fragment, continue, but position is updated because last
    inOrder.verify(mockControlledFragmentHandler).onFragment(
        any(UnsafeBuffer.class),
        eq(2 * ALIGNED_FRAME_LENGTH + HEADER_LENGTH),
        eq(DATA.length),
        any(Header.class));
    inOrder.verify(position).setOrdered(initialPosition + (ALIGNED_FRAME_LENGTH * 3));
}
 
Example #23
Source File: IrDecoder.java    From simple-binary-encoding with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a {@link Ir} decoder for data encoded in a {@link ByteBuffer}.
 *
 * @param buffer containing the serialised {@link Ir}.
 */
public IrDecoder(final ByteBuffer buffer)
{
    channel = null;
    length = buffer.limit();
    directBuffer = new UnsafeBuffer(buffer);
    offset = 0;
}
 
Example #24
Source File: BufferAlignmentAgentTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
private void testUnsafeBuffer(final UnsafeBuffer buffer, final int offset)
{
    testAlignedReadMethods(buffer, offset);
    testUnAlignedReadMethods(buffer, offset);
    testAlignedWriteMethods(buffer, offset);
    testUnAlignedWriteMethods(buffer, offset);
    testAlignedAtomicMethods(buffer, offset);
    testUnAlignedAtomicMethods(buffer, offset);
}
 
Example #25
Source File: HeaderWriter.java    From aeron with Apache License 2.0 5 votes vote down vote up
public void write(final UnsafeBuffer termBuffer, final int offset, final int length, final int termId)
{
    termBuffer.putLongOrdered(
        offset + FRAME_LENGTH_FIELD_OFFSET, versionFlagsType | ((((long)reverseBytes(-length))) << 32));
    UnsafeAccess.UNSAFE.storeFence();

    termBuffer.putLong(offset + TERM_OFFSET_FIELD_OFFSET, sessionId | ((((long)reverseBytes(offset))) << 32));
    termBuffer.putLong(offset + STREAM_ID_FIELD_OFFSET, streamId | (reverseBytes(termId) & 0xFFFF_FFFFL));
}
 
Example #26
Source File: ReplaySession.java    From aeron with Apache License 2.0 5 votes vote down vote up
static boolean isInvalidHeader(
    final UnsafeBuffer buffer, final int streamId, final int termId, final int termOffset)
{
    return
        termOffset(buffer, 0) != termOffset ||
        termId(buffer, 0) != termId ||
        streamId(buffer, 0) != streamId;
}
 
Example #27
Source File: ImageTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUpdatePositionOnRethrownExceptionInControlledPoll()
{
    final long initialPosition = computePosition(INITIAL_TERM_ID, 0, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID);
    position.setOrdered(initialPosition);
    final Image image = createImage();

    insertDataFrame(INITIAL_TERM_ID, offsetForFrame(0));

    when(mockControlledFragmentHandler.onFragment(any(DirectBuffer.class), anyInt(), anyInt(), any(Header.class)))
        .thenThrow(new RuntimeException());

    doThrow(new RuntimeException()).when(errorHandler).onError(any());

    boolean thrown = false;
    try
    {
        image.controlledPoll(mockControlledFragmentHandler, Integer.MAX_VALUE);
    }
    catch (final Exception ignore)
    {
        thrown = true;
    }

    assertTrue(thrown);
    assertThat(image.position(), is(initialPosition + ALIGNED_FRAME_LENGTH));

    verify(mockControlledFragmentHandler).onFragment(
        any(UnsafeBuffer.class), eq(HEADER_LENGTH), eq(DATA.length), any(Header.class));
}
 
Example #28
Source File: ManyToOneRingBufferTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldThrowExceptionForCapacityThatIsNotPowerOfTwo()
{
    final int capacity = 777;
    final int totalBufferLength = capacity + RingBufferDescriptor.TRAILER_LENGTH;
    assertThrows(IllegalStateException.class,
        () -> new ManyToOneRingBuffer(new UnsafeBuffer(new byte[totalBufferLength])));
}
 
Example #29
Source File: SenderAndTargetSessionIdStrategyTest.java    From artio with Apache License 2.0 5 votes vote down vote up
@Test
public void validatesSpaceInBufferOnSave()
{
    final AtomicBuffer buffer = new UnsafeBuffer(new byte[5]);
    final CompositeKey key = strategy.onInitiateLogon("SIGMAX", null, null, "ABC_DEFG04", null, null);

    final int length = strategy.save(key, buffer, 1);

    assertEquals(INSUFFICIENT_SPACE, length);
}
 
Example #30
Source File: HeaderWriter.java    From aeron with Apache License 2.0 5 votes vote down vote up
NativeBigEndianHeaderWriter(final UnsafeBuffer defaultHeader)
{
    super(
        defaultHeader.getInt(VERSION_FIELD_OFFSET) & 0xFFFF_FFFFL,
        defaultHeader.getInt(SESSION_ID_FIELD_OFFSET) & 0xFFFF_FFFFL,
        ((long)defaultHeader.getInt(STREAM_ID_FIELD_OFFSET)) << 32);
}