Search in sources :

Example 16 with MutableDirectBuffer

use of org.agrona.MutableDirectBuffer in project aeron by real-logic.

the class ExclusiveTermAppenderTest method appendBlock.

@Test
void appendBlock() {
    final int termId = 43;
    final int termOffset = 128;
    final MutableDirectBuffer buffer = mock(MutableDirectBuffer.class);
    final int offset = 16;
    final int length = 1024;
    final int lengthOfFirstFrame = 148;
    when(buffer.getInt(offset, LITTLE_ENDIAN)).thenReturn(lengthOfFirstFrame);
    final int resultOffset = termAppender.appendBlock(termId, termOffset, buffer, offset, length);
    assertEquals(termOffset + length, resultOffset);
    final long rawTail = rawTail(metadataBuffer, 0);
    assertEquals(termId, termId(rawTail));
    assertEquals(termOffset + length, termOffset(rawTail));
    final InOrder inOrder = inOrder(termBuffer, buffer);
    inOrder.verify(buffer).getInt(offset, LITTLE_ENDIAN);
    inOrder.verify(buffer).putInt(offset, 0, LITTLE_ENDIAN);
    inOrder.verify(termBuffer).putBytes(termOffset, buffer, offset, length);
    inOrder.verify(termBuffer).putIntOrdered(termOffset, lengthOfFirstFrame);
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) MutableDirectBuffer(org.agrona.MutableDirectBuffer) Test(org.junit.jupiter.api.Test)

Example 17 with MutableDirectBuffer

use of org.agrona.MutableDirectBuffer in project aeron by real-logic.

the class ClientConductorTest method setUp.

@BeforeEach
public void setUp() {
    final Aeron.Context ctx = new Aeron.Context().clientLock(mockClientLock).epochClock(epochClock).nanoClock(nanoClock).awaitingIdleStrategy(new NoOpIdleStrategy()).toClientBuffer(mockToClientReceiver).driverProxy(driverProxy).logBuffersFactory(logBuffersFactory).errorHandler(mockClientErrorHandler).availableImageHandler(mockAvailableImageHandler).unavailableImageHandler(mockUnavailableImageHandler).closeHandler(mockCloseHandler).keepAliveIntervalNs(KEEP_ALIVE_INTERVAL).driverTimeoutMs(AWAIT_TIMEOUT).interServiceTimeoutNs(TimeUnit.MILLISECONDS.toNanos(INTER_SERVICE_TIMEOUT_MS));
    ctx.countersMetaDataBuffer(counterMetaDataBuffer);
    ctx.countersValuesBuffer(counterValuesBuffer);
    when(mockClientLock.tryLock()).thenReturn(TRUE);
    when(driverProxy.addPublication(CHANNEL, STREAM_ID_1)).thenReturn(CORRELATION_ID);
    when(driverProxy.addPublication(CHANNEL, STREAM_ID_2)).thenReturn(CORRELATION_ID_2);
    when(driverProxy.removePublication(CORRELATION_ID)).thenReturn(CLOSE_CORRELATION_ID);
    when(driverProxy.addSubscription(anyString(), anyInt())).thenReturn(CORRELATION_ID);
    when(driverProxy.removeSubscription(CORRELATION_ID)).thenReturn(CLOSE_CORRELATION_ID);
    conductor = new ClientConductor(ctx, mockAeron);
    publicationReady.wrap(publicationReadyBuffer, 0);
    subscriptionReady.wrap(subscriptionReadyBuffer, 0);
    operationSuccess.wrap(operationSuccessBuffer, 0);
    errorResponse.wrap(errorMessageBuffer, 0);
    clientTimeout.wrap(clientTimeoutBuffer, 0);
    publicationReady.correlationId(CORRELATION_ID);
    publicationReady.registrationId(CORRELATION_ID);
    publicationReady.sessionId(SESSION_ID_1);
    publicationReady.streamId(STREAM_ID_1);
    publicationReady.logFileName(SESSION_ID_1 + "-log");
    operationSuccess.correlationId(CLOSE_CORRELATION_ID);
    final UnsafeBuffer[] termBuffersSession1 = new UnsafeBuffer[PARTITION_COUNT];
    final UnsafeBuffer[] termBuffersSession2 = new UnsafeBuffer[PARTITION_COUNT];
    for (int i = 0; i < PARTITION_COUNT; i++) {
        termBuffersSession1[i] = new UnsafeBuffer(allocateDirect(TERM_BUFFER_LENGTH));
        termBuffersSession2[i] = new UnsafeBuffer(allocateDirect(TERM_BUFFER_LENGTH));
    }
    final UnsafeBuffer logMetaDataSession1 = new UnsafeBuffer(allocateDirect(TERM_BUFFER_LENGTH));
    final UnsafeBuffer logMetaDataSession2 = new UnsafeBuffer(allocateDirect(TERM_BUFFER_LENGTH));
    final MutableDirectBuffer header1 = DataHeaderFlyweight.createDefaultHeader(SESSION_ID_1, STREAM_ID_1, 0);
    final MutableDirectBuffer header2 = DataHeaderFlyweight.createDefaultHeader(SESSION_ID_2, STREAM_ID_2, 0);
    LogBufferDescriptor.storeDefaultFrameHeader(logMetaDataSession1, header1);
    LogBufferDescriptor.storeDefaultFrameHeader(logMetaDataSession2, header2);
    final LogBuffers logBuffersSession1 = mock(LogBuffers.class);
    final LogBuffers logBuffersSession2 = mock(LogBuffers.class);
    when(logBuffersFactory.map(SESSION_ID_1 + "-log")).thenReturn(logBuffersSession1);
    when(logBuffersFactory.map(SESSION_ID_2 + "-log")).thenReturn(logBuffersSession2);
    when(logBuffersFactory.map(SESSION_ID_1 + "-log")).thenReturn(logBuffersSession1);
    when(logBuffersFactory.map(SESSION_ID_2 + "-log")).thenReturn(logBuffersSession2);
    when(logBuffersSession1.duplicateTermBuffers()).thenReturn(termBuffersSession1);
    when(logBuffersSession2.duplicateTermBuffers()).thenReturn(termBuffersSession2);
    when(logBuffersSession1.metaDataBuffer()).thenReturn(logMetaDataSession1);
    when(logBuffersSession2.metaDataBuffer()).thenReturn(logMetaDataSession2);
    when(logBuffersSession1.termLength()).thenReturn(TERM_BUFFER_LENGTH);
    when(logBuffersSession2.termLength()).thenReturn(TERM_BUFFER_LENGTH);
}
Also used : MutableDirectBuffer(org.agrona.MutableDirectBuffer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 18 with MutableDirectBuffer

use of org.agrona.MutableDirectBuffer in project aeron by real-logic.

the class ReplaySessionTest method mockPublication.

private void mockPublication(final ExclusivePublication replay, final UnsafeBuffer termBuffer) {
    when(replay.offerBlock(any(MutableDirectBuffer.class), anyInt(), anyInt())).then((invocation) -> {
        final MutableDirectBuffer buffer = invocation.getArgument(0);
        final int offset = invocation.getArgument(1);
        final int length = invocation.getArgument(2);
        termBuffer.putBytes(offerBlockOffset, buffer, offset, length);
        messageCounter++;
        offerBlockOffset += length;
        return (long) length;
    });
    when(replay.appendPadding(anyInt())).then((invocation) -> {
        final int claimedSize = invocation.getArgument(0);
        messageCounter++;
        return (long) claimedSize;
    });
}
Also used : MutableDirectBuffer(org.agrona.MutableDirectBuffer)

Example 19 with MutableDirectBuffer

use of org.agrona.MutableDirectBuffer in project aeron by real-logic.

the class ControlResponseProxy method attemptSendSignal.

void attemptSendSignal(final long controlSessionId, final long correlationId, final long recordingId, final long subscriptionId, final long position, final RecordingSignal recordingSignal, final Publication controlPublication) {
    final int messageLength = MESSAGE_HEADER_LENGTH + RecordingSignalEventEncoder.BLOCK_LENGTH;
    int attempts = SEND_ATTEMPTS;
    do {
        final long result = controlPublication.tryClaim(messageLength, bufferClaim);
        if (result > 0) {
            final MutableDirectBuffer buffer = bufferClaim.buffer();
            final int bufferOffset = bufferClaim.offset();
            recordingSignalEventEncoder.wrapAndApplyHeader(buffer, bufferOffset, messageHeaderEncoder).controlSessionId(controlSessionId).correlationId(correlationId).recordingId(recordingId).subscriptionId(subscriptionId).position(position).signal(recordingSignal);
            bufferClaim.commit();
            break;
        }
    } while (--attempts > 0);
}
Also used : MutableDirectBuffer(org.agrona.MutableDirectBuffer)

Example 20 with MutableDirectBuffer

use of org.agrona.MutableDirectBuffer in project aeron by real-logic.

the class FileSender method sendChunk.

private static void sendChunk(final Publication publication, final BufferClaim bufferClaim, final long correlationId, final UnsafeBuffer fileBuffer, final int chunkOffset, final int chunkLength) {
    long result;
    while ((result = publication.tryClaim(CHUNK_PAYLOAD_OFFSET + chunkLength, bufferClaim)) < 0) {
        checkResult(result);
        Thread.yield();
    }
    final MutableDirectBuffer buffer = bufferClaim.buffer();
    final int offset = bufferClaim.offset();
    buffer.putInt(offset + VERSION_OFFSET, VERSION, LITTLE_ENDIAN);
    buffer.putInt(offset + TYPE_OFFSET, FILE_CHUNK_TYPE, LITTLE_ENDIAN);
    buffer.putLong(offset + CORRELATION_ID_OFFSET, correlationId, LITTLE_ENDIAN);
    buffer.putLong(offset + CHUNK_OFFSET_OFFSET, chunkOffset, LITTLE_ENDIAN);
    buffer.putLong(offset + CHUNK_LENGTH_OFFSET, chunkLength, LITTLE_ENDIAN);
    buffer.putBytes(offset + CHUNK_PAYLOAD_OFFSET, fileBuffer, chunkOffset, chunkLength);
    bufferClaim.commit();
}
Also used : MutableDirectBuffer(org.agrona.MutableDirectBuffer)

Aggregations

MutableDirectBuffer (org.agrona.MutableDirectBuffer)42 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)18 BeforeEach (org.junit.jupiter.api.BeforeEach)12 Test (org.junit.jupiter.api.Test)12 DirectBuffer (org.agrona.DirectBuffer)11 MediaDriver (io.aeron.driver.MediaDriver)10 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)10 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)10 RegistrationException (io.aeron.exceptions.RegistrationException)8 FragmentHandler (io.aeron.logbuffer.FragmentHandler)8 InterruptAfter (io.aeron.test.InterruptAfter)8 InterruptingTestCallback (io.aeron.test.InterruptingTestCallback)8 SystemTestWatcher (io.aeron.test.SystemTestWatcher)8 Tests (io.aeron.test.Tests)8 TestMediaDriver (io.aeron.test.driver.TestMediaDriver)8 Objects.requireNonNull (java.util.Objects.requireNonNull)8 MutableLong (org.agrona.collections.MutableLong)8 Assertions.assertNotEquals (org.junit.jupiter.api.Assertions.assertNotEquals)8 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)8 Assumptions.assumeTrue (org.junit.jupiter.api.Assumptions.assumeTrue)8