fix(raft): address WAL code quality issues — CRC perf, DRY, safety, assertions
- SyncAsync: remove redundant FlushAsync, use single Flush(flushToDisk:true) - ComputeCrc: use incremental Crc32.Append to avoid contiguous buffer heap allocation - Load: cast pos+length to long to guard against int overflow in bounds check - AppendAsync: delegate to WriteEntryTo (DRY — eliminates duplicated record-building logic) - Load: extract ParseEntries static helper to eliminate goto pattern with early returns - Entries: change return type from IEnumerable to IReadOnlyList for index access and Count property - RaftNode.PersistAsync: remove redundant term.txt write (meta.json now owns term+votedFor) - RaftWalTests: tighten ShouldBeGreaterThanOrEqualTo(1) -> ShouldBe(1) in truncation/CRC tests; use .Count property directly on IReadOnlyList instead of .Count() LINQ extension
This commit is contained in:
@@ -37,7 +37,7 @@ public class RaftWalTests : IDisposable
|
||||
|
||||
// Recover
|
||||
using var recovered = RaftWal.Load(walPath);
|
||||
var entries = recovered.Entries.ToList();
|
||||
var entries = recovered.Entries;
|
||||
entries.Count.ShouldBe(3);
|
||||
entries[0].Index.ShouldBe(1);
|
||||
entries[0].Term.ShouldBe(1);
|
||||
@@ -60,7 +60,7 @@ public class RaftWalTests : IDisposable
|
||||
await wal.CompactAsync(5); // remove entries 1-5
|
||||
|
||||
using var recovered = RaftWal.Load(walPath);
|
||||
recovered.Entries.Count().ShouldBe(5);
|
||||
recovered.Entries.Count.ShouldBe(5);
|
||||
recovered.Entries.First().Index.ShouldBe(6);
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ public class RaftWalTests : IDisposable
|
||||
fs.SetLength(fs.Length - 3);
|
||||
|
||||
using var recovered = RaftWal.Load(walPath);
|
||||
recovered.Entries.Count().ShouldBeGreaterThanOrEqualTo(1);
|
||||
recovered.Entries.Count.ShouldBe(1);
|
||||
recovered.Entries.First().Command.ShouldBe("good-entry");
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ public class RaftWalTests : IDisposable
|
||||
}
|
||||
|
||||
using var recovered = RaftWal.Load(walPath);
|
||||
recovered.Entries.Count().ShouldBe(0);
|
||||
recovered.Entries.Count.ShouldBe(0);
|
||||
}
|
||||
|
||||
// Go reference: server/raft.go WAL CRC integrity check
|
||||
@@ -139,9 +139,9 @@ public class RaftWalTests : IDisposable
|
||||
bytes[^5] ^= 0xFF;
|
||||
File.WriteAllBytes(walPath, bytes);
|
||||
|
||||
// Load should recover at least the first record, stopping at the corrupt second
|
||||
// Load should recover exactly the first record, stopping at the corrupt second
|
||||
using var recovered = RaftWal.Load(walPath);
|
||||
recovered.Entries.Count().ShouldBeGreaterThanOrEqualTo(1);
|
||||
recovered.Entries.Count.ShouldBe(1);
|
||||
recovered.Entries.First().Command.ShouldBe("valid");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user