Skip state-file flushes in the ntfy tests
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:
1 parent
fb76284129
commit
eb0c021044
2 files changed
+87
-2
No files matched your search
@@ -75,6 +75,12 @@ func loadState(path string) (state, error) {
|
|||||||
return st, nil
|
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
|
// 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
|
// directory, synced to disk, then renamed over path. A crash leaves the
|
||||||
// old file or the new one, never part of either.
|
// old file or the new one, never part of either.
|
||||||
@@ -94,7 +100,7 @@ func saveState(path string, st state) error {
|
|||||||
tmp := f.Name()
|
tmp := f.Name()
|
||||||
_, err = f.Write(data)
|
_, err = f.Write(data)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = f.Sync()
|
err = syncFile(f)
|
||||||
}
|
}
|
||||||
if closeErr := f.Close(); err == nil {
|
if closeErr := f.Close(); err == nil {
|
||||||
err = closeErr
|
err = closeErr
|
||||||
@@ -120,7 +126,7 @@ func syncDir(dir string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = d.Sync()
|
_ = syncFile(d)
|
||||||
_ = d.Close()
|
_ = d.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user