package safefile import ( "errors" "io" "os" "path/filepath" "testing" ) func TestPlan14DurableWriteInterruptedSync(t *testing.T) { for _, point := range []string{"file-sync", "parent-sync"} { t.Run(point, func(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "floor") if err := os.WriteFile(path, []byte("old"), 0600); err != nil { t.Fatal(err) } fd, err := OpenDir(dir) if err != nil { t.Fatal(err) } defer fd.Close() interrupted := errors.New("sync interrupted") err = writeAtSteps(int(fd.Fd()), "floor", 0600, func(w io.Writer) error { _, e := w.Write([]byte("new")); return e }, func(stage string, op func() error) error { if e := op(); e != nil { return e } if stage == point { return interrupted } return nil }) if !errors.Is(err, interrupted) { t.Fatal("lost durability failure", err) } body, err := os.ReadFile(path) if err != nil { t.Fatal(err) } want := "old" if point == "parent-sync" { want = "new" } if string(body) != want { t.Fatal("publication crossed unsynced boundary", string(body)) } if err := WriteAt(int(fd.Fd()), "floor", []byte("new"), 0600); err != nil { t.Fatal("retry", err) } }) } }