use of org.apache.ratis.protocol.exceptions.ChecksumException in project incubator-ratis by apache.
the class TestRaftLogReadWrite method testReadWithEntryCorruption.
/**
* Test the log reader to make sure it can detect the checksum mismatch.
*/
@Test
public void testReadWithEntryCorruption() throws IOException {
RaftStorage storage = RaftStorageTestUtils.newRaftStorage(storageDir);
final File openSegment = LogSegmentStartEnd.valueOf(0).getFile(storage);
try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, ByteBuffer.allocateDirect(bufferSize))) {
for (int i = 0; i < 100; i++) {
LogEntryProto entry = LogProtoUtils.toLogEntryProto(new SimpleOperation("m" + i).getLogEntryContent(), 0, i);
out.write(entry);
}
} finally {
storage.close();
}
// corrupt the log file
try (RandomAccessFile raf = new RandomAccessFile(openSegment.getCanonicalFile(), "rw")) {
raf.seek(100);
int correctValue = raf.read();
raf.seek(100);
raf.write(correctValue + 1);
}
try {
readLog(openSegment, 0, RaftLog.INVALID_LOG_INDEX, true);
Assert.fail("The read of corrupted log file should fail");
} catch (ChecksumException e) {
LOG.info("Caught ChecksumException as expected", e);
}
}
Aggregations