Each ntfy keepalive, every 45 seconds, moved the cursor and rewrote the subscription's state file with a synced write: about 1,900 a day per subscription while idle, which wears an SD card. A cursor that only keepalives or replayed repeats moved is now written at most every 10 minutes and at shutdown. A message printed, held or left out, a floor change, a digest, and the cursor's first move from 0 are still written within a second. A saved cursor that is behind costs only a longer replay after a crash, and the saved IDs drop what it sends again; a cursor still at 0 would mean a first start, which replays nothing. Tests check the lazy cadence and a crash that loses the later moves, the first move from 0 with an open event that has no time, and the write at shutdown. The soak now writes its state 325 times instead of 2,702. design.md records the decision. Its job-store entry now treats GORM on SQLite as the first choice and has the plan investigate SQLite in WAL mode, against bbolt and BadgerDB, before anything is built. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
package ntfy
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"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()
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|