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:
57_WolveandClaude Opus 5.5 committed 2026-12-01 12:08:36 -06:00
1 parent c548833656
commit 62e5ec9c66
9 files changed
+49 -49

No files matched your search

+1
View File
@@ -421,6 +421,7 @@ func testOptions(f *fakeServer, dir string) Options {
BypassPriority: 5,
Keepalive: 45 * time.Second,
StateFile: filepath.Join(dir, "ntfy-home.json"),
Sync: skipSync,
Transport: f,
}
}
+1 -1
View File
@@ -49,7 +49,7 @@ func TestNoGoroutineLeakOverRealConnections(t *testing.T) {
o := Options{
Name: "home", Server: srv.URL, Topics: []string{"alerts"}, MinPriority: 1,
MaxBacklog: time.Hour, RateLimit: 1000, RateWindow: time.Minute, BypassPriority: 5,
Keepalive: 45 * time.Second, StateFile: t.TempDir() + "/ntfy-home.json",
Keepalive: 45 * time.Second, StateFile: t.TempDir() + "/ntfy-home.json", Sync: skipSync,
}
s, err := New(o)
if err != nil {
+1 -1
View File
@@ -45,7 +45,7 @@ func TestRedirectsAreNotFollowed(t *testing.T) {
o := Options{
Name: "home", Server: srv.URL, Topics: []string{"alerts"}, Token: "tk_secret",
MinPriority: 1, MaxBacklog: time.Hour, RateLimit: 5, RateWindow: time.Minute, BypassPriority: 5,
Keepalive: 45 * time.Second, StateFile: t.TempDir() + "/ntfy-home.json",
Keepalive: 45 * time.Second, StateFile: t.TempDir() + "/ntfy-home.json", Sync: skipSync,
Transport: srv.Client().Transport,
}
s, err := New(o)
+5 -5
View File
@@ -127,7 +127,7 @@ func TestBacklogUsesTheServerClock(t *testing.T) {
serverNow := func() int64 { return time.Now().Add(f.skew).Unix() }
// The subscription last ran ten hours ago by the server's clock.
if err := saveState(o.StateFile, state{Cursor: serverNow() - 10*3600}); err != nil {
if err := saveState(o.StateFile, state{Cursor: serverNow() - 10*3600}, skipSync); err != nil {
t.Fatal(err)
}
old := f.publish(Message{Title: "five hours ago", Time: serverNow() - 5*3600})
@@ -164,7 +164,7 @@ func TestBacklogFollowsTheStreamClock(t *testing.T) {
f.skew, f.authFails, f.noDate = tt.skew, true, tt.noDate
o := testOptions(f, t.TempDir())
serverNow := func() int64 { return time.Now().Add(f.skew).Unix() }
if err := saveState(o.StateFile, state{Cursor: serverNow() - 2*3600}); err != nil {
if err := saveState(o.StateFile, state{Cursor: serverNow() - 2*3600}, skipSync); err != nil {
t.Fatal(err)
}
old := f.publish(Message{Title: "ninety minutes ago", Time: serverNow() - 5400})
@@ -235,7 +235,7 @@ func TestBacklogIgnoresAnEarlierClock(t *testing.T) {
f := newFakeServer()
f.authSkew = tt.authSkew
o := testOptions(f, t.TempDir())
if err := saveState(o.StateFile, state{Cursor: time.Now().Unix() - 2*3600}); err != nil {
if err := saveState(o.StateFile, state{Cursor: time.Now().Unix() - 2*3600}, skipSync); err != nil {
t.Fatal(err)
}
old := f.publish(Message{Title: "ninety minutes ago", Time: time.Now().Unix() - 5400})
@@ -286,7 +286,7 @@ func TestBacklogPrefersTheOpenEvent(t *testing.T) {
f := newFakeServer()
f.dateSkew = tt.dateSkew
o := testOptions(f, t.TempDir())
if err := saveState(o.StateFile, state{Cursor: time.Now().Unix() - 2*3600}); err != nil {
if err := saveState(o.StateFile, state{Cursor: time.Now().Unix() - 2*3600}, skipSync); err != nil {
t.Fatal(err)
}
old := f.publish(Message{Title: "ninety minutes ago", Time: time.Now().Unix() - 5400})
@@ -327,7 +327,7 @@ func TestBacklogFromTheDate(t *testing.T) {
f.skew, f.openNoTime = 10*time.Hour, true
o := testOptions(f, t.TempDir())
serverNow := func() int64 { return time.Now().Add(f.skew).Unix() }
if err := saveState(o.StateFile, state{Cursor: serverNow() - 10*3600}); err != nil {
if err := saveState(o.StateFile, state{Cursor: serverNow() - 10*3600}, skipSync); err != nil {
t.Fatal(err)
}
old := f.publish(Message{Title: "five hours ago", Time: serverNow() - 5*3600})
+10 -15
View File
@@ -75,16 +75,11 @@ 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 hundreds 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.
func saveState(path string, st state) error {
// directory, flushed to disk with sync, then renamed over path, and the
// directory flushed after the rename. A crash leaves the old file or the
// new one, never part of either.
func saveState(path string, st state, sync func(*os.File) error) error {
st.Version = stateVersion
data, err := json.Marshal(st)
if err != nil {
@@ -100,7 +95,7 @@ func saveState(path string, st state) error {
tmp := f.Name()
_, err = f.Write(data)
if err == nil {
err = syncFile(f)
err = sync(f)
}
if closeErr := f.Close(); err == nil {
err = closeErr
@@ -112,13 +107,13 @@ func saveState(path string, st state) error {
_ = os.Remove(tmp)
return err
}
syncDir(dir)
syncDir(dir, sync)
return nil
}
// syncDir makes a rename in dir durable. Windows cannot open a directory
// to sync it, and its renames need no such step.
func syncDir(dir string) {
// syncDir makes a rename in dir durable by flushing dir with sync. Windows
// cannot open a directory to sync it, and its renames need no such step.
func syncDir(dir string, sync func(*os.File) error) {
if runtime.GOOS == "windows" {
return
}
@@ -126,7 +121,7 @@ func syncDir(dir string) {
if err != nil {
return
}
_ = syncFile(d)
_ = sync(d)
_ = d.Close()
}
+8 -12
View File
@@ -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 {
+9 -1
View File
@@ -13,6 +13,7 @@ import (
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
@@ -133,6 +134,10 @@ type Options struct {
// StateFile is where the subscription keeps its state.
StateFile string
// Sync flushes a written state file to disk, and then its directory
// where the system allows. Nil means (*os.File).Sync.
Sync func(*os.File) error
// Transport sends the requests. Nil means a transport with dial, TLS
// handshake and response header timeouts and no overall timeout,
// since the response is a stream.
@@ -197,6 +202,9 @@ func New(o Options) (*Subscriber, error) {
if o.Report == nil {
o.Report = func(error) {}
}
if o.Sync == nil {
o.Sync = (*os.File).Sync
}
base := strings.TrimRight(u.String(), "/") + "/" + strings.Join(o.Topics, ",")
s := &Subscriber{
o: o,
@@ -816,7 +824,7 @@ func (r *run) save(now time.Time) {
st.Held = &h
}
r.savedAt = now
if err := saveState(r.o.StateFile, st); err != nil {
if err := saveState(r.o.StateFile, st, r.o.Sync); err != nil {
if !r.saveFailed {
r.log.Error("ntfy state not saved", "err", err)
r.o.Report(fmt.Errorf("saving state: %w", err))
+2 -2
View File
@@ -179,7 +179,7 @@ func TestStaleCursorReplaysUnclamped(t *testing.T) {
f := newFakeServer()
o := testOptions(f, dir)
old := f.publish(Message{Title: "three hours ago"})
if err := saveState(o.StateFile, state{Cursor: old.Time}); err != nil {
if err := saveState(o.StateFile, state{Cursor: old.Time}, skipSync); err != nil {
t.Fatal(err)
}
time.Sleep(3 * time.Hour)
@@ -213,7 +213,7 @@ func TestBacklogFloorIsSaved(t *testing.T) {
f := newFakeServer()
o := testOptions(f, t.TempDir())
o.MaxBacklog = 2 * time.Minute
if err := saveState(o.StateFile, state{Cursor: time.Now().Unix()}); err != nil {
if err := saveState(o.StateFile, state{Cursor: time.Now().Unix()}, skipSync); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)