diff --git a/internal/pkg/ntfy/state.go b/internal/pkg/ntfy/state.go index a318138..1d49ecd 100644 --- a/internal/pkg/ntfy/state.go +++ b/internal/pkg/ntfy/state.go @@ -75,6 +75,12 @@ func loadState(path string) (state, error) { return st, nil } +// syncFile flushes a file, or a directory, to disk. It is a variable so +// that tests, which write state files thousands of times, can skip the +// flush; on a disk that is slow to sync, each one costs tens of +// milliseconds. +var syncFile = (*os.File).Sync + // saveState writes st to path atomically: to a temporary file in the same // directory, synced to disk, then renamed over path. A crash leaves the // old file or the new one, never part of either. @@ -94,7 +100,7 @@ func saveState(path string, st state) error { tmp := f.Name() _, err = f.Write(data) if err == nil { - err = f.Sync() + err = syncFile(f) } if closeErr := f.Close(); err == nil { err = closeErr @@ -120,7 +126,7 @@ func syncDir(dir string) { if err != nil { return } - _ = d.Sync() + _ = syncFile(d) _ = d.Close() } diff --git a/internal/pkg/ntfy/state_test.go b/internal/pkg/ntfy/state_test.go new file mode 100644 index 0000000..cb4ac34 --- /dev/null +++ b/internal/pkg/ntfy/state_test.go @@ -0,0 +1,79 @@ +package ntfy + +import ( + "errors" + "os" + "path/filepath" + "runtime" + "strings" + "testing" +) + +// TestMain skips flushing state files to disk. The tests write them +// thousands of times, and on a disk that is slow to sync the flushes alone +// outlast the test timeout. TestSaveStateSyncs checks the flush itself. +func TestMain(m *testing.M) { + syncFile = func(*os.File) error { return nil } + m.Run() +} + +// saveState flushes the new file before renaming it over the old one, and, +// where the system allows, the directory after the rename. A failed flush +// leaves the old file as it was and no temporary file behind. +func TestSaveStateSyncs(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "ntfy-home.json") + + defer func(f func(*os.File) error) { syncFile = f }(syncFile) + var synced []string + renamed := false + syncFile = func(f *os.File) error { + name := filepath.Base(f.Name()) + if name == filepath.Base(dir) { + st, err := loadState(path) + renamed = err == nil && st.Cursor == 42 + } + synced = append(synced, name) + return f.Sync() + } + + if err := saveState(path, state{Cursor: 42}); err != nil { + t.Fatal(err) + } + if st, err := loadState(path); err != nil || st.Cursor != 42 { + t.Fatalf("loaded %+v, %v", st, err) + } + want := 2 + if runtime.GOOS == "windows" { + want = 1 + } + if len(synced) != want || !strings.HasPrefix(synced[0], "ntfy-home.json.") || !strings.HasSuffix(synced[0], ".tmp") { + t.Fatalf("synced %q, want %d flushes, the temporary file first", synced, want) + } + if want == 2 { + if synced[1] != filepath.Base(dir) { + t.Errorf("synced %q second, want the directory %q", synced[1], filepath.Base(dir)) + } else if !renamed { + t.Error("the directory was flushed before the new file was renamed into place") + } + } + + syncFile = func(*os.File) error { return errors.New("disk full") } + if err := saveState(path, state{Cursor: 43}); err == nil { + t.Fatal("saveState succeeded although the flush failed") + } + if st, err := loadState(path); err != nil || st.Cursor != 42 { + t.Errorf("after a failed save, loaded %+v, %v; want the old state", st, err) + } + entries, err := os.ReadDir(dir) + if err != nil { + t.Fatal(err) + } + if len(entries) != 1 { + var names []string + for _, e := range entries { + names = append(names, e.Name()) + } + t.Errorf("files left %q, want only the state file", names) + } +}