A subscription flushed its state file through a package variable, which the ntfy tests replaced with a no-op in their TestMain. Tests in other packages could not reach it, so the daemon's own tests still flushed for real. Options.Sync now takes the flush; nil means (*os.File).Sync, as before. The ntfy tests pass a no-op through their options and to saveState. Behaviour is unchanged. docs/plans/ntfy.md names the option, and its line references follow the code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
package ntfy
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// skipSync stands in for the flush to disk in the tests. They write state
|
|
// files hundreds of times, and on a disk that is slow to sync each flush
|
|
// costs tens of milliseconds. TestSaveStateSyncs checks the flush itself.
|
|
func skipSync(*os.File) error { return nil }
|
|
|
|
// 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")
|
|
|
|
var synced []string
|
|
renamed := false
|
|
sync := 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}, sync); 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")
|
|
}
|
|
}
|
|
|
|
diskFull := func(*os.File) error { return errors.New("disk full") }
|
|
if err := saveState(path, state{Cursor: 43}, diskFull); 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)
|
|
}
|
|
}
|