Skip state-file flushes in the ntfy tests
Security / Vulnerability check (push) Successful in 38s
Test / Lint and test (push) Successful in 2m4s

The ntfy tests write subscription state files about 5,000 times, and each
write flushed the temporary file and its directory to disk. On the CI
runner, where a flush costs tens of milliseconds, the package outran the
five-minute test timeout in TestSoak.

saveState and syncDir now flush through a syncFile variable, which the
package's TestMain replaces with a no-op. TestSaveStateSyncs puts the real
flush back and checks that the temporary file is flushed before the
rename, the directory after it, and that a failed flush leaves the old
state file and no temporary file. Production behaviour is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
57_WolveandClaude Opus 5.5 committed 2026-11-30 20:01:44 -06:00
1 parent fb76284129
commit eb0c021044
2 files changed
+87 -2

No files matched your search

+79
View File
@@ -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)
}
}