Pass the state-file flush to a subscription in its options
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>
This commit is contained in:
1 parent
c548833656
commit
62e5ec9c66
9 files changed
+49
-49
No files matched your search
@@ -9,13 +9,10 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestMain skips flushing state files to disk. The tests write them
|
||||
// hundreds of times, and on a disk that is slow to sync each flush costs
|
||||
// tens of milliseconds. TestSaveStateSyncs checks the flush itself.
|
||||
func TestMain(m *testing.M) {
|
||||
syncFile = func(*os.File) error { return nil }
|
||||
m.Run()
|
||||
}
|
||||
// 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
|
||||
@@ -24,10 +21,9 @@ 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 {
|
||||
sync := func(f *os.File) error {
|
||||
name := filepath.Base(f.Name())
|
||||
if name == filepath.Base(dir) {
|
||||
st, err := loadState(path)
|
||||
@@ -37,7 +33,7 @@ func TestSaveStateSyncs(t *testing.T) {
|
||||
return f.Sync()
|
||||
}
|
||||
|
||||
if err := saveState(path, state{Cursor: 42}); err != nil {
|
||||
if err := saveState(path, state{Cursor: 42}, sync); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if st, err := loadState(path); err != nil || st.Cursor != 42 {
|
||||
@@ -58,8 +54,8 @@ func TestSaveStateSyncs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
syncFile = func(*os.File) error { return errors.New("disk full") }
|
||||
if err := saveState(path, state{Cursor: 43}); err == nil {
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user