Skip state-file flushes in the daemon's subscription test
Security / Vulnerability check (push) Successful in 46s
Test / Lint and test (push) Successful in 2m0s

TestServeWithSubscription failed on CI: the message did not print
within 10 seconds. On a first start the open event sets the cursor, and
the subscription writes its state, flushing the file and its directory
to disk, before it reads the next line. The test's fake server sends
open and the message together, so the print waited behind two real
flushes, and on the CI runner, which is slow to flush, it did not come
in time. A flush made to take 11 seconds fails the test the same way,
at the same line.

run and newSubscribers now take the flush. The daemon passes
(*os.File).Sync, as before, and its tests pass one that does nothing,
as the ntfy package's tests do; TestSaveStateSyncs checks the flush
itself. Writing the first cursor at once stays: a saved cursor of 0
means a first start, which replays nothing.

On leil-test-1, on an ext4 file system that another process was
filling, a 4 KiB write with fsync took up to 0.5 seconds, and the old
test's time to print rose from 20 to 160 milliseconds with it. The new
test's run time did not change.

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:13:11 -06:00
1 parent 62e5ec9c66
commit f0b5bf020e
4 files changed
+18 -10

No files matched your search

+5 -3
View File
@@ -28,9 +28,10 @@ var weekdays = map[string]time.Weekday{
}
// newSubscribers returns a subscriber for each [[ntfy.subscriptions]]
// table. It reads the credential files and makes sure the state directory
// can be written, so that a mistake in either stops the daemon at startup.
func newSubscribers(cfg *config.Config, logger *slog.Logger) ([]*ntfy.Subscriber, error) {
// table, which flushes its state file to disk with syncState. It reads the
// credential files and makes sure the state directory can be written, so
// that a mistake in either stops the daemon at startup.
func newSubscribers(cfg *config.Config, logger *slog.Logger, syncState func(*os.File) error) ([]*ntfy.Subscriber, error) {
subs := cfg.Ntfy.Subscriptions
if len(subs) == 0 {
return nil, nil
@@ -63,6 +64,7 @@ func newSubscribers(cfg *config.Config, logger *slog.Logger) ([]*ntfy.Subscriber
Keepalive: sc.Keepalive,
SkipUpdates: cfg.Ntfy.Format.Updates == "skip",
StateFile: filepath.Join(dir, "ntfy-"+sc.Name+".json"),
Sync: syncState,
Logger: logger,
Report: func(err error) { reportNtfy(sc.Name, "", err) },
}
+7 -2
View File
@@ -26,6 +26,11 @@ import (
"uberbringer/internal/pkg/receipt"
)
// skipSync stands in for the flush of a subscription's state file to disk.
// On a busy disk a flush can take seconds, and a first start writes its
// state as soon as open arrives, before it reads the message behind it.
func skipSync(*os.File) error { return nil }
// TestServeWithSubscription runs the daemon with a subscription to a fake
// ntfy server: a message on the stream prints, /healthz lists the
// subscription as connected without changing its status code, and the
@@ -88,7 +93,7 @@ token_file = %q
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
served := make(chan error, 1)
go func() { served <- run(ctx, cfg, BuildInfo{Version: "test"}, io.Discard, listen) }()
go func() { served <- run(ctx, cfg, BuildInfo{Version: "test"}, io.Discard, listen, skipSync) }()
waitFor(t, func() bool {
// "Grüße" in CP437: ü is 0x81, ß is 0xE1.
@@ -153,7 +158,7 @@ func TestNewSubscribersErrors(t *testing.T) {
} {
sub.TokenFile = tt.file
cfg.Ntfy.Subscriptions = []config.NtfySubscription{sub}
if _, err := newSubscribers(&cfg, slog.New(slog.DiscardHandler)); err == nil || !strings.Contains(err.Error(), tt.want) {
if _, err := newSubscribers(&cfg, slog.New(slog.DiscardHandler), skipSync); err == nil || !strings.Contains(err.Error(), tt.want) {
t.Errorf("token_file %s: err = %v, want %q", tt.file, err, tt.want)
}
}
+5 -4
View File
@@ -34,7 +34,7 @@ func serve(ctx context.Context, configPath string, build BuildInfo, out io.Write
if err != nil {
return err
}
return run(ctx, cfg, build, out, listenTCP)
return run(ctx, cfg, build, out, listenTCP, (*os.File).Sync)
}
// listenFunc opens the HTTP listener on addr.
@@ -46,8 +46,9 @@ func listenTCP(ctx context.Context, addr string) (net.Listener, error) {
}
// run wires every component from cfg, opens the HTTP listener with listen
// once the wiring has succeeded, and serves until ctx is cancelled.
func run(ctx context.Context, cfg *config.Config, build BuildInfo, out io.Writer, listen listenFunc) error {
// once the wiring has succeeded, and serves until ctx is cancelled. The
// subscriptions flush their state files to disk with syncState.
func run(ctx context.Context, cfg *config.Config, build BuildInfo, out io.Writer, listen listenFunc, syncState func(*os.File) error) error {
logger := newLogger(cfg.Log, os.Stderr)
printBanner(out, build)
@@ -71,7 +72,7 @@ func run(ctx context.Context, cfg *config.Config, build BuildInfo, out io.Writer
}
defer flushSentry()
subscribers, err := newSubscribers(cfg, logger)
subscribers, err := newSubscribers(cfg, logger, syncState)
if err != nil {
return err
}
+1 -1
View File
@@ -91,7 +91,7 @@ level = "error"
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
served := make(chan error, 1)
go func() { served <- run(ctx, cfg, BuildInfo{Version: "test"}, io.Discard, listen) }()
go func() { served <- run(ctx, cfg, BuildInfo{Version: "test"}, io.Discard, listen, skipSync) }()
base := "http://" + ln.Addr().String()
waitFor(t, func() bool {