Search in sources :

Example 36 with Context

use of io.aeron.archive.Archive.Context in project aeron by real-logic.

the class RecordingWriterTest method onBlockShouldRollOverToTheNextSegmentFile.

@Test
void onBlockShouldRollOverToTheNextSegmentFile() throws IOException {
    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(13, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir));
    recordingWriter.init();
    final byte[] data1 = new byte[992];
    fill(data1, (byte) 13);
    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(TERM_LENGTH));
    frameType(termBuffer, 0, HDR_TYPE_DATA);
    frameLengthOrdered(termBuffer, 0, 1024);
    termBuffer.putBytes(HEADER_LENGTH, data1);
    for (int i = 0; i < SEGMENT_LENGTH / 1024; i++) {
        recordingWriter.onBlock(termBuffer, 0, 1024, -1, -1);
    }
    frameType(termBuffer, 0, HDR_TYPE_DATA);
    frameLengthOrdered(termBuffer, 0, 192);
    final byte[] data2 = new byte[160];
    fill(data2, (byte) 22);
    termBuffer.putBytes(HEADER_LENGTH, data2);
    recordingWriter.onBlock(termBuffer, 0, 192, -1, -1);
    recordingWriter.close();
    final File segmentFile1 = segmentFile(13, 0);
    assertTrue(segmentFile1.exists());
    assertEquals(SEGMENT_LENGTH, segmentFile1.length());
    final UnsafeBuffer fileBuffer = new UnsafeBuffer();
    fileBuffer.wrap(readAllBytes(segmentFile1.toPath()));
    assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 0));
    assertEquals(1024, frameLength(fileBuffer, 0));
    byte[] fileBytes = new byte[992];
    fileBuffer.getBytes(HEADER_LENGTH, fileBytes, 0, 992);
    assertArrayEquals(data1, fileBytes);
    final File segmentFile2 = segmentFile(13, SEGMENT_LENGTH);
    assertTrue(segmentFile2.exists());
    assertEquals(SEGMENT_LENGTH, segmentFile2.length());
    fileBuffer.wrap(readAllBytes(segmentFile2.toPath()));
    assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 0));
    assertEquals(192, frameLength(fileBuffer, 0));
    fileBytes = new byte[160];
    fileBuffer.getBytes(HEADER_LENGTH, fileBytes, 0, 160);
    assertArrayEquals(data2, fileBytes);
}
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 37 with Context

use of io.aeron.archive.Archive.Context in project aeron by real-logic.

the class ArchiveTest method dataBufferIsAllocatedOnDemand.

@Test
void dataBufferIsAllocatedOnDemand() {
    final Context context = new Context();
    final UnsafeBuffer buffer = context.dataBuffer();
    assertNotNull(buffer);
    assertEquals(context.fileIoMaxLength(), buffer.capacity());
    assertSame(buffer, context.dataBuffer());
}
Also used : Context(io.aeron.archive.Archive.Context) CommonContext(io.aeron.CommonContext) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 38 with Context

use of io.aeron.archive.Archive.Context in project aeron by real-logic.

the class ArchiveTest method recordChecksumBufferIsAllocatedOnDemandIfThreadingModeIsDEDICATED.

@Test
void recordChecksumBufferIsAllocatedOnDemandIfThreadingModeIsDEDICATED() {
    final Checksum recordChecksum = mock(Checksum.class);
    final Archive.Context context = new Archive.Context().recordChecksum(recordChecksum).threadingMode(DEDICATED);
    final UnsafeBuffer buffer = context.recordChecksumBuffer();
    assertNotNull(buffer);
    assertEquals(context.fileIoMaxLength(), buffer.capacity());
    assertSame(buffer, context.recordChecksumBuffer());
    assertNotSame(context.dataBuffer(), buffer);
}
Also used : Context(io.aeron.archive.Archive.Context) CommonContext(io.aeron.CommonContext) AeronArchive(io.aeron.archive.client.AeronArchive) Checksum(io.aeron.archive.checksum.Checksum) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Context(io.aeron.archive.Archive.Context) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 39 with Context

use of io.aeron.archive.Archive.Context in project aeron by real-logic.

the class ArchiveTest method replayBufferIsAllocatedOnDemandIfThreadingModeIsDEDICATED.

@Test
void replayBufferIsAllocatedOnDemandIfThreadingModeIsDEDICATED() {
    final Context context = new Context().threadingMode(DEDICATED);
    final UnsafeBuffer buffer = context.replayBuffer();
    assertNotNull(buffer);
    assertEquals(context.fileIoMaxLength(), buffer.capacity());
    assertSame(buffer, context.replayBuffer());
    assertNotSame(context.dataBuffer(), buffer);
}
Also used : Context(io.aeron.archive.Archive.Context) CommonContext(io.aeron.CommonContext) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 40 with Context

use of io.aeron.archive.Archive.Context in project aeron by real-logic.

the class ArchiveTest method shouldAllowMultipleConnectionsInParallel.

@Test
public void shouldAllowMultipleConnectionsInParallel() throws InterruptedException {
    final int numberOfArchiveClients = 5;
    final long connectTimeoutNs = TimeUnit.SECONDS.toNanos(10);
    final CountDownLatch latch = new CountDownLatch(numberOfArchiveClients);
    final ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(numberOfArchiveClients);
    final ManyToOneConcurrentLinkedQueue<AeronArchive> archiveClientQueue = new ManyToOneConcurrentLinkedQueue<>();
    final MediaDriver.Context driverCtx = new MediaDriver.Context().errorHandler(Tests::onError).clientLivenessTimeoutNs(connectTimeoutNs).dirDeleteOnStart(true).publicationUnblockTimeoutNs(connectTimeoutNs * 2).threadingMode(ThreadingMode.SHARED);
    final Archive.Context archiveCtx = new Archive.Context().threadingMode(SHARED).connectTimeoutNs(connectTimeoutNs);
    executor.prestartAllCoreThreads();
    try (ArchivingMediaDriver driver = ArchivingMediaDriver.launch(driverCtx, archiveCtx)) {
        for (int i = 0; i < numberOfArchiveClients; i++) {
            executor.execute(() -> {
                final AeronArchive.Context ctx = new AeronArchive.Context().messageTimeoutNs(connectTimeoutNs);
                final AeronArchive archive = AeronArchive.connect(ctx);
                archiveClientQueue.add(archive);
                latch.countDown();
            });
        }
        assertTrue(latch.await(driver.archive().context().connectTimeoutNs() * 2, TimeUnit.NANOSECONDS));
        AeronArchive archiveClient;
        while (null != (archiveClient = archiveClientQueue.poll())) {
            archiveClient.close();
        }
    } finally {
        executor.shutdownNow();
        archiveCtx.deleteDirectory();
        driverCtx.deleteDirectory();
    }
}
Also used : Context(io.aeron.archive.Archive.Context) CommonContext(io.aeron.CommonContext) AeronArchive(io.aeron.archive.client.AeronArchive) CountDownLatch(java.util.concurrent.CountDownLatch) AeronArchive(io.aeron.archive.client.AeronArchive) Tests(io.aeron.test.Tests) MediaDriver(io.aeron.driver.MediaDriver) ManyToOneConcurrentLinkedQueue(org.agrona.concurrent.ManyToOneConcurrentLinkedQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Context(io.aeron.archive.Archive.Context) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Context (io.aeron.archive.Archive.Context)40 Test (org.junit.jupiter.api.Test)36 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)30 Image (io.aeron.Image)20 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 CommonContext (io.aeron.CommonContext)16 File (java.io.File)16 AeronArchive (io.aeron.archive.client.AeronArchive)14 Checksum (io.aeron.archive.checksum.Checksum)6 EnumSource (org.junit.jupiter.params.provider.EnumSource)4 ArchiveException (io.aeron.archive.client.ArchiveException)2 MediaDriver (io.aeron.driver.MediaDriver)2 Tests (io.aeron.test.Tests)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 ManyToOneConcurrentLinkedQueue (org.agrona.concurrent.ManyToOneConcurrentLinkedQueue)2