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));
}
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));
}
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);
}
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));
}
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());
}
Aggregations