Search in sources :

Example 16 with Image

use of io.aeron.Image in project aeron by real-logic.

the class ClusterNodeRestartTest method launchService.

private void launchService(final boolean initialLaunch, final AtomicLong msgCounter) {
    final ClusteredService service = new StubClusteredService() {

        private int counterValue = 0;

        public void onSessionMessage(final long clusterSessionId, final long correlationId, final long timestampMs, final DirectBuffer buffer, final int offset, final int length, final Header header) {
            final int sentValue = buffer.getInt(offset);
            assertThat(sentValue, is(counterValue));
            counterValue++;
            serviceState.set(Integer.toString(counterValue));
            msgCounter.getAndIncrement();
        }

        public void onTakeSnapshot(final Publication snapshotPublication) {
            final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
            int length = 0;
            buffer.putInt(length, counterValue);
            length += SIZE_OF_INT;
            length += buffer.putIntAscii(length, counterValue);
            snapshotPublication.offer(buffer, 0, length);
        }

        public void onLoadSnapshot(final Image snapshotImage) {
            while (true) {
                final int fragments = snapshotImage.poll((buffer, offset, length, header) -> {
                    counterValue = buffer.getInt(offset);
                    final String s = buffer.getStringWithoutLengthAscii(offset + SIZE_OF_INT, length - SIZE_OF_INT);
                    serviceState.set(s);
                }, 1);
                if (fragments == 1) {
                    break;
                }
                TestUtil.checkInterruptedStatus();
                Thread.yield();
            }
        }
    };
    container = null;
    container = ClusteredServiceContainer.launch(new ClusteredServiceContainer.Context().clusteredService(service).terminationHook(() -> {
    }).errorHandler(Throwable::printStackTrace).deleteDirOnStart(initialLaunch));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) Header(io.aeron.logbuffer.Header) Publication(io.aeron.Publication) ClusteredService(io.aeron.cluster.service.ClusteredService) Image(io.aeron.Image) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer)

Example 17 with Image

use of io.aeron.Image in project Aeron by real-logic.

the class RecordingSessionTest method mockImage.

private static Image mockImage(final Subscription subscription) {
    final Image image = mock(Image.class);
    when(image.sessionId()).thenReturn(SESSION_ID);
    when(image.initialTermId()).thenReturn(INITIAL_TERM_ID);
    when(image.sourceIdentity()).thenReturn(SOURCE_IDENTITY);
    when(image.termBufferLength()).thenReturn(TERM_BUFFER_LENGTH);
    when(image.mtuLength()).thenReturn(MTU_LENGTH);
    when(image.joinPosition()).thenReturn(START_POSITION);
    when(image.subscription()).thenReturn(subscription);
    return image;
}
Also used : Image(io.aeron.Image)

Example 18 with Image

use of io.aeron.Image in project Aeron by real-logic.

the class RecordingWriterTest method onBlockShouldNotComputeCrcForThePaddingFrame.

@Test
void onBlockShouldNotComputeCrcForThePaddingFrame() throws IOException {
    final Image image = mockImage(0L);
    final Context ctx = new Context().archiveDir(archiveDir).recordChecksumBuffer(new UnsafeBuffer(allocateDirectAligned(512, 64))).recordChecksum(crc32());
    final RecordingWriter recordingWriter = new RecordingWriter(1, 0, SEGMENT_LENGTH, image, ctx);
    recordingWriter.init();
    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(512));
    final int length = 128;
    final byte[] data = new byte[length - HEADER_LENGTH];
    final int sessionId = 5;
    final int termId = 18;
    fill(data, (byte) 99);
    frameType(termBuffer, 0, HDR_TYPE_PAD);
    frameTermId(termBuffer, 0, termId);
    frameLengthOrdered(termBuffer, 0, length);
    frameSessionId(termBuffer, 0, sessionId);
    termBuffer.putBytes(HEADER_LENGTH, data);
    recordingWriter.onBlock(termBuffer, 0, HEADER_LENGTH, -1, -1);
    recordingWriter.close();
    final File segmentFile = segmentFile(1, 0);
    assertTrue(segmentFile.exists());
    assertEquals(SEGMENT_LENGTH, segmentFile.length());
    final UnsafeBuffer fileBuffer = new UnsafeBuffer();
    fileBuffer.wrap(readAllBytes(segmentFile.toPath()));
    assertEquals(HDR_TYPE_PAD, frameType(fileBuffer, 0));
    assertEquals(termId, frameTermId(fileBuffer, 0));
    assertEquals(length, frameLength(fileBuffer, 0));
    assertEquals(sessionId, frameSessionId(fileBuffer, 0));
}
Also used : Context(io.aeron.archive.Archive.Context) Image(io.aeron.Image) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 19 with Image

use of io.aeron.Image in project Aeron by real-logic.

the class RecordingWriterTest method onBlockThrowsNullPointerExceptionIfInitWasNotCalled.

@Test
void onBlockThrowsNullPointerExceptionIfInitWasNotCalled() {
    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir));
    assertThrows(NullPointerException.class, () -> recordingWriter.onBlock(new UnsafeBuffer(allocate(32)), 0, 10, 5, 8));
}
Also used : Context(io.aeron.archive.Archive.Context) Image(io.aeron.Image) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test)

Example 20 with Image

use of io.aeron.Image in project Aeron by real-logic.

the class RecordingWriterTest method initThrowsIOExceptionIfItCannotOpenAFileChannel.

@Test
void initThrowsIOExceptionIfItCannotOpenAFileChannel() throws IOException {
    final File notADirectory = new File(archiveDir, "dummy.txt");
    assertTrue(notADirectory.createNewFile());
    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(notADirectory));
    assertThrows(IOException.class, recordingWriter::init);
}
Also used : Context(io.aeron.archive.Archive.Context) Image(io.aeron.Image) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

Image (io.aeron.Image)34 Test (org.junit.jupiter.api.Test)26 Context (io.aeron.archive.Archive.Context)20 File (java.io.File)20 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)14 ClusteredService (io.aeron.cluster.service.ClusteredService)7 ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)7 ExclusivePublication (io.aeron.ExclusivePublication)6 Archive (io.aeron.archive.Archive)6 ArchiveThreadingMode (io.aeron.archive.ArchiveThreadingMode)6 CLUSTER_MEMBERS (io.aeron.cluster.ClusterTestConstants.CLUSTER_MEMBERS)6 INGRESS_ENDPOINTS (io.aeron.cluster.ClusterTestConstants.INGRESS_ENDPOINTS)6 AeronCluster (io.aeron.cluster.client.AeronCluster)6 ClientSession (io.aeron.cluster.service.ClientSession)6 Cluster (io.aeron.cluster.service.Cluster)6 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)6 MediaDriver (io.aeron.driver.MediaDriver)6 ThreadingMode (io.aeron.driver.ThreadingMode)6 FragmentHandler (io.aeron.logbuffer.FragmentHandler)6 ClusterTests (io.aeron.test.cluster.ClusterTests)6