Replacement-path test: the store's own file is fenced (seeded bytes read back zero)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-02 11:50:04 -08:00
1 parent 2fe1b06d1b
commit 6ee693c3c2
1 file changed
+19 -2
@@ -132,8 +132,9 @@ func TestWALStoreV3FenceSliceFlushFaultAfterADirectWriteIsNotCounted(t *testing.
// The O_DIRECT descriptor is used only on the very file the store holds: if
// the store's path now names another file (replaced after the open), the
// fence writes through the store's own descriptor and the other file is
// never touched. The control is the test above (same file: O_DIRECT used).
// fence writes through the store's own descriptor (bytes seeded in the
// fence interval read back as zeros there) and the other file is never
// touched. The control is the test above (same file: O_DIRECT used).
func TestWALStoreV3FenceNeverWritesAFileThatReplacedTheStorePath(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("the O_DIRECT path is Linux only; elsewhere the fence never reopens the path")
@@ -153,6 +154,15 @@ func TestWALStoreV3FenceNeverWritesAFileThatReplacedTheStorePath(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// Seed non-zero bytes in the WAL's last 64 KiB, inside the fence interval
// [E, floor + WALSize): the fence must zero them in the store's own file.
seedOff := int64(walOffsetV3 + walSize - 64<<10)
if _, err := s.fd.WriteAt(bytes.Repeat([]byte{0xCD}, 64<<10), seedOff); err != nil {
t.Fatal(err)
}
if err := s.fd.Sync(); err != nil {
t.Fatal(err)
}
if err := os.Rename(path, path+".held"); err != nil { // the store keeps its descriptor
t.Fatal(err)
}
@@ -177,4 +187,11 @@ func TestWALStoreV3FenceNeverWritesAFileThatReplacedTheStorePath(t *testing.T) {
if !bytes.Equal(after, foreign) {
t.Fatal("the fence wrote the file that replaced the store's path")
}
held, err := os.ReadFile(path + ".held") // the store's own file, renamed
if err != nil {
t.Fatal(err)
}
if got := held[seedOff : seedOff+64<<10]; !bytes.Equal(got, make([]byte, 64<<10)) {
t.Fatal("the store's own file was not fenced: the seeded bytes in the fence interval are not zero")
}
}