Search in sources :

Example 31 with Context

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

the class RecordingWriterTest method onBlockShouldComputeCrcUsingTheChecksumBuffer.

@Test
void onBlockShouldComputeCrcUsingTheChecksumBuffer() 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(allocateDirectAligned(512, 64));
    frameType(termBuffer, 96, HDR_TYPE_DATA);
    frameTermId(termBuffer, 96, 96);
    frameLengthOrdered(termBuffer, 96, 64);
    frameSessionId(termBuffer, 96, 96);
    termBuffer.setMemory(96 + HEADER_LENGTH, 32, (byte) 96);
    frameType(termBuffer, 160, HDR_TYPE_DATA);
    frameTermId(termBuffer, 160, 160);
    frameLengthOrdered(termBuffer, 160, 288);
    frameSessionId(termBuffer, 160, 160);
    termBuffer.setMemory(160 + HEADER_LENGTH, 256, (byte) 160);
    recordingWriter.onBlock(termBuffer, 96, 352, -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_DATA, frameType(fileBuffer, 0));
    assertEquals(96, frameTermId(fileBuffer, 0));
    assertEquals(64, frameLength(fileBuffer, 0));
    assertEquals(ctx.recordChecksum().compute(termBuffer.addressOffset(), 96 + HEADER_LENGTH, 32), frameSessionId(fileBuffer, 0));
    assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 64));
    assertEquals(160, frameTermId(fileBuffer, 64));
    assertEquals(288, frameLength(fileBuffer, 64));
    assertEquals(ctx.recordChecksum().compute(termBuffer.addressOffset(), 160 + HEADER_LENGTH, 256), frameSessionId(fileBuffer, 64));
    // Ensure that the source buffer was not modified
    assertEquals(96, frameSessionId(termBuffer, 96));
    assertEquals(160, frameSessionId(termBuffer, 160));
}
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 32 with Context

use of io.aeron.archive.Archive.Context 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 33 with Context

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

the class RecordingWriterTest method onBlockShouldWriteHeaderOfThePaddingFrameAndAdvanceFilePositionByThePaddingLength.

@Test
void onBlockShouldWriteHeaderOfThePaddingFrameAndAdvanceFilePositionByThePaddingLength() throws IOException {
    final int segmentOffset = 96;
    final long startPosition = 7 * TERM_LENGTH + segmentOffset;
    final Image image = mockImage(startPosition);
    final RecordingWriter recordingWriter = new RecordingWriter(5, startPosition, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir));
    recordingWriter.init();
    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(1024));
    frameType(termBuffer, 0, HDR_TYPE_PAD);
    frameLengthOrdered(termBuffer, 0, 1024);
    frameSessionId(termBuffer, 0, 111);
    final byte[] data = new byte[992];
    fill(data, (byte) -1);
    termBuffer.putBytes(HEADER_LENGTH, data);
    recordingWriter.onBlock(termBuffer, 0, 1024, -1, -1);
    recordingWriter.close();
    final File segmentFile = segmentFile(5, startPosition);
    assertTrue(segmentFile.exists());
    assertEquals(SEGMENT_LENGTH, segmentFile.length());
    final UnsafeBuffer fileBuffer = new UnsafeBuffer();
    fileBuffer.wrap(readAllBytes(segmentFile.toPath()));
    final byte[] preamble = new byte[segmentOffset];
    fileBuffer.getBytes(0, preamble, 0, segmentOffset);
    assertArrayEquals(new byte[segmentOffset], preamble);
    assertEquals(HDR_TYPE_PAD, frameType(fileBuffer, segmentOffset));
    assertEquals(1024, frameLength(fileBuffer, segmentOffset));
    assertEquals(111, frameSessionId(fileBuffer, segmentOffset));
    final byte[] fileBytes = new byte[992];
    fileBuffer.getBytes(segmentOffset + HEADER_LENGTH, fileBytes, 0, 992);
    assertArrayEquals(new byte[992], 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 34 with Context

use of io.aeron.archive.Archive.Context 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 35 with Context

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

the class RecordingWriterTest method closeShouldCloseTheUnderlyingFile.

@Test
void closeShouldCloseTheUnderlyingFile() throws IOException {
    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir));
    recordingWriter.init();
    recordingWriter.close();
    final File segmentFile = segmentFile(1, 0);
    assertTrue(segmentFile.exists());
    // can delete since the underlying FileChannel was already closed
    delete(segmentFile.toPath());
}
Also used : Context(io.aeron.archive.Archive.Context) Image(io.aeron.Image) File(java.io.File) Test(org.junit.jupiter.api.Test)

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