Search in sources :

Example 1 with Header

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

the class ReplaySessionTest method before.

@Before
public void before() {
    when(position.getWeak()).then((invocation) -> positionLong);
    when(position.get()).then((invocation) -> positionLong);
    when(mockArchiveConductor.catalog()).thenReturn(mockCatalog);
    doAnswer((invocation) -> {
        positionLong = invocation.getArgument(0);
        return null;
    }).when(position).setOrdered(anyLong());
    doAnswer((invocation) -> {
        final long delta = invocation.getArgument(0);
        positionLong += delta;
        return null;
    }).when(position).getAndAddOrdered(anyLong());
    context = new Archive.Context().archiveDir(archiveDir).epochClock(epochClock);
    recordingSummary.recordingId = RECORDING_ID;
    recordingSummary.startPosition = START_POSITION;
    recordingSummary.segmentFileLength = context.segmentFileLength();
    recordingSummary.initialTermId = INITIAL_TERM_ID;
    recordingSummary.termBufferLength = TERM_BUFFER_LENGTH;
    recordingSummary.mtuLength = MTU_LENGTH;
    recordingSummary.streamId = STREAM_ID;
    recordingSummary.sessionId = SESSION_ID;
    final RecordingWriter writer = new RecordingWriter(RECORDING_ID, START_POSITION, JOIN_POSITION, TERM_BUFFER_LENGTH, context, ARCHIVE_DIR_CHANNEL, position);
    final UnsafeBuffer buffer = new UnsafeBuffer(allocateDirectAligned(TERM_BUFFER_LENGTH, 64));
    final DataHeaderFlyweight headerFwt = new DataHeaderFlyweight();
    final Header header = new Header(INITIAL_TERM_ID, Integer.numberOfLeadingZeros(TERM_BUFFER_LENGTH));
    header.buffer(buffer);
    recordFragment(writer, buffer, headerFwt, header, 0, FrameDescriptor.UNFRAGMENTED, HDR_TYPE_DATA);
    recordFragment(writer, buffer, headerFwt, header, 1, FrameDescriptor.BEGIN_FRAG_FLAG, HDR_TYPE_DATA);
    recordFragment(writer, buffer, headerFwt, header, 2, FrameDescriptor.END_FRAG_FLAG, HDR_TYPE_DATA);
    recordFragment(writer, buffer, headerFwt, header, 3, FrameDescriptor.UNFRAGMENTED, HDR_TYPE_PAD);
    writer.close();
    recordingSummary.stopPosition = START_POSITION + 4 * FRAME_LENGTH;
}
Also used : Header(io.aeron.logbuffer.Header) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Before(org.junit.Before)

Example 2 with Header

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

the class FragmentAssemblerTest method shouldAssembleTwoPartMessage.

@Test
public void shouldAssembleTwoPartMessage() {
    when(header.flags()).thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG).thenReturn(FrameDescriptor.END_FRAG_FLAG);
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
    final int offset = 0;
    final int length = srcBuffer.capacity() / 2;
    srcBuffer.setMemory(0, length, (byte) 65);
    srcBuffer.setMemory(length, length, (byte) 66);
    adapter.onFragment(srcBuffer, offset, length, header);
    adapter.onFragment(srcBuffer, length, length, header);
    final ArgumentCaptor<UnsafeBuffer> bufferArg = ArgumentCaptor.forClass(UnsafeBuffer.class);
    final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);
    verify(delegateFragmentHandler, times(1)).onFragment(bufferArg.capture(), eq(offset), eq(length * 2), headerArg.capture());
    final UnsafeBuffer capturedBuffer = bufferArg.getValue();
    for (int i = 0; i < srcBuffer.capacity(); i++) {
        assertThat("same at i=" + i, capturedBuffer.getByte(i), is(srcBuffer.getByte(i)));
    }
    final Header capturedHeader = headerArg.getValue();
    assertThat(capturedHeader.sessionId(), is(SESSION_ID));
    assertThat(capturedHeader.flags(), is(FrameDescriptor.END_FRAG_FLAG));
}
Also used : Header(io.aeron.logbuffer.Header) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 3 with Header

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

the class AuthenticationTest method launchService.

private void launchService(final MutableLong sessionId, final MutableReference<byte[]> encodedPrincipal, final AtomicLong msgCounter) {
    final ClusteredService service = new StubClusteredService() {

        private int counterValue = 0;

        public void onSessionOpen(final ClientSession session, final long timestampMs) {
            sessionId.value = session.id();
            encodedPrincipal.set(session.encodedPrincipal());
        }

        public void onSessionMessage(final long clusterSessionId, final long correlationId, final long timestampMs, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            assertThat(buffer.getInt(offset), is(counterValue));
            msgCounter.getAndIncrement();
            counterValue++;
        }
    };
    container = null;
    container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).errorHandler(Throwable::printStackTrace).deleteDirOnStart(true));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) ClientSession(io.aeron.cluster.service.ClientSession) ClusteredService(io.aeron.cluster.service.ClusteredService) ClusteredServiceContainer(io.aeron.cluster.service.ClusteredServiceContainer)

Example 4 with Header

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

the class ClusterNodeTest method shouldScheduleEventInService.

@Test(timeout = 10_000)
public void shouldScheduleEventInService() {
    container = launchTimedService();
    aeronCluster = connectToCluster();
    final Aeron aeron = aeronCluster.context().aeron();
    final SessionDecorator sessionDecorator = new SessionDecorator(aeronCluster.clusterSessionId());
    final Publication publication = aeronCluster.ingressPublication();
    final ExpandableArrayBuffer msgBuffer = new ExpandableArrayBuffer();
    final long msgCorrelationId = aeron.nextCorrelationId();
    final String msg = "Hello World!";
    msgBuffer.putStringWithoutLengthAscii(0, msg);
    while (sessionDecorator.offer(publication, msgCorrelationId, msgBuffer, 0, msg.length()) < 0) {
        TestUtil.checkInterruptedStatus();
        Thread.yield();
    }
    final MutableInteger messageCount = new MutableInteger();
    final EgressAdapter adapter = new EgressAdapter(new StubEgressListener() {

        public void onMessage(final long correlationId, final long clusterSessionId, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            assertThat(correlationId, is(msgCorrelationId));
            assertThat(buffer.getStringWithoutLengthAscii(offset, length), is(msg + "-scheduled"));
            messageCount.value += 1;
        }
    }, aeronCluster.egressSubscription(), FRAGMENT_LIMIT);
    while (messageCount.get() == 0) {
        if (adapter.poll() <= 0) {
            TestUtil.checkInterruptedStatus();
            Thread.yield();
        }
    }
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) MutableInteger(org.agrona.collections.MutableInteger) Publication(io.aeron.Publication) SessionDecorator(io.aeron.cluster.client.SessionDecorator) Aeron(io.aeron.Aeron) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) EgressAdapter(io.aeron.cluster.client.EgressAdapter) Test(org.junit.Test)

Example 5 with Header

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

the class ClusterNodeTest method shouldEchoMessageViaService.

@Test(timeout = 10_000)
public void shouldEchoMessageViaService() {
    container = launchEchoService();
    aeronCluster = connectToCluster();
    final Aeron aeron = aeronCluster.context().aeron();
    final SessionDecorator sessionDecorator = new SessionDecorator(aeronCluster.clusterSessionId());
    final Publication publication = aeronCluster.ingressPublication();
    final ExpandableArrayBuffer msgBuffer = new ExpandableArrayBuffer();
    final long msgCorrelationId = aeron.nextCorrelationId();
    final String msg = "Hello World!";
    msgBuffer.putStringWithoutLengthAscii(0, msg);
    while (sessionDecorator.offer(publication, msgCorrelationId, msgBuffer, 0, msg.length()) < 0) {
        TestUtil.checkInterruptedStatus();
        Thread.yield();
    }
    final MutableInteger messageCount = new MutableInteger();
    final EgressAdapter adapter = new EgressAdapter(new StubEgressListener() {

        public void onMessage(final long correlationId, final long clusterSessionId, final long timestamp, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            assertThat(correlationId, is(msgCorrelationId));
            assertThat(buffer.getStringWithoutLengthAscii(offset, length), is(msg));
            messageCount.value += 1;
        }
    }, aeronCluster.egressSubscription(), FRAGMENT_LIMIT);
    while (messageCount.get() == 0) {
        if (adapter.poll() <= 0) {
            TestUtil.checkInterruptedStatus();
            Thread.yield();
        }
    }
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) MutableInteger(org.agrona.collections.MutableInteger) Publication(io.aeron.Publication) SessionDecorator(io.aeron.cluster.client.SessionDecorator) Aeron(io.aeron.Aeron) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) EgressAdapter(io.aeron.cluster.client.EgressAdapter) Test(org.junit.Test)

Aggregations

Header (io.aeron.logbuffer.Header)44 DirectBuffer (org.agrona.DirectBuffer)24 Test (org.junit.jupiter.api.Test)22 ClientSession (io.aeron.cluster.service.ClientSession)13 ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)13 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)13 ClusteredService (io.aeron.cluster.service.ClusteredService)12 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)10 MediaDriver (io.aeron.driver.MediaDriver)10 StubClusteredService (io.aeron.test.cluster.StubClusteredService)10 CloseHelper (org.agrona.CloseHelper)9 Test (org.junit.Test)9 Subscription (io.aeron.Subscription)8 Archive (io.aeron.archive.Archive)8 ArchiveThreadingMode (io.aeron.archive.ArchiveThreadingMode)8 AeronCluster (io.aeron.cluster.client.AeronCluster)8 ThreadingMode (io.aeron.driver.ThreadingMode)8 MutableInteger (org.agrona.collections.MutableInteger)8 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)8 EgressListener (io.aeron.cluster.client.EgressListener)7