use of io.aeron.archive.Archive.Context in project Aeron by real-logic.
the class RecordingWriterTest method onBlockThrowsArchiveExceptionIfCurrentThreadWasInterrupted.
@Test
void onBlockThrowsArchiveExceptionIfCurrentThreadWasInterrupted() throws IOException {
final Image image = mockImage(0L);
final RecordingWriter recordingWriter = new RecordingWriter(1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir));
recordingWriter.init();
assertFalse(Thread.interrupted());
try {
Thread.currentThread().interrupt();
final ArchiveException exception = assertThrows(ArchiveException.class, () -> recordingWriter.onBlock(new UnsafeBuffer(allocate(32)), 0, 10, 5, 8));
assertEquals(GENERIC, exception.errorCode());
assertEquals("ERROR - file closed by interrupt, recording aborted", exception.getMessage());
} finally {
assertTrue(Thread.interrupted());
}
}
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());
}
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 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);
}
use of io.aeron.archive.Archive.Context in project Aeron by real-logic.
the class RecordingWriterTest method onBlockShouldWriteHeaderAndContentsOfTheNonPaddingFrame.
@Test
void onBlockShouldWriteHeaderAndContentsOfTheNonPaddingFrame() throws IOException {
final Image image = mockImage(0L);
final RecordingWriter recordingWriter = new RecordingWriter(1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir));
recordingWriter.init();
final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(128));
frameType(termBuffer, 0, HDR_TYPE_DATA);
frameLengthOrdered(termBuffer, 0, 128);
final byte[] data = new byte[96];
fill(data, (byte) 7);
termBuffer.putBytes(HEADER_LENGTH, data);
recordingWriter.onBlock(termBuffer, 0, 128, -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(128, frameLength(fileBuffer, 0));
assertEquals(0, frameSessionId(fileBuffer, 0));
final byte[] fileBytes = new byte[96];
fileBuffer.getBytes(HEADER_LENGTH, fileBytes, 0, 96);
assertArrayEquals(data, fileBytes);
}
Aggregations