use of org.apache.ratis.protocol.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 = new RaftStorage(storageDir, StartupOption.REGULAR);
File openSegment = storage.getStorageDir().getOpenLogFile(0);
try (LogOutputStream out = new LogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, bufferSize)) {
for (int i = 0; i < 100; i++) {
LogEntryProto entry = ProtoUtils.toLogEntryProto(new SimpleOperation("m" + i).getLogEntryContent(), 0, i, clientId, callId);
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, RaftServerConstants.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