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

+8 -2
View File
@@ -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()
}