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

+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))