carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package safefile
2
3import (
4 "errors"
5 "io"
6 "os"
7 "path/filepath"
8 "testing"
9)
10
11func TestPlan14DurableWriteInterruptedSync(t *testing.T) {
12 for _, point := range []string{"file-sync", "parent-sync"} {
13 t.Run(point, func(t *testing.T) {
14 dir := t.TempDir()
15 path := filepath.Join(dir, "floor")
16 if err := os.WriteFile(path, []byte("old"), 0600); err != nil {
17 t.Fatal(err)
18 }
19 fd, err := OpenDir(dir)
20 if err != nil {
21 t.Fatal(err)
22 }
23 defer fd.Close()
24 interrupted := errors.New("sync interrupted")
25 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 {
26 if e := op(); e != nil {
27 return e
28 }
29 if stage == point {
30 return interrupted
31 }
32 return nil
33 })
34 if !errors.Is(err, interrupted) {
35 t.Fatal("lost durability failure", err)
36 }
37 body, err := os.ReadFile(path)
38 if err != nil {
39 t.Fatal(err)
40 }
41 want := "old"
42 if point == "parent-sync" {
43 want = "new"
44 }
45 if string(body) != want {
46 t.Fatal("publication crossed unsynced boundary", string(body))
47 }
48 if err := WriteAt(int(fd.Fd()), "floor", []byte("new"), 0600); err != nil {
49 t.Fatal("retry", err)
50 }
51 })
52 }
53}
54