Files
magpie/internal/davsync/load_held_test.go
T
yetone 1576ccdbbc davsync: the sync setup is read once a request, not on every Providers poll
/api/providers asks archiveState, which asks davsync.S3Bucket and so
Load, on every poll, and Load opened sync.json each time, most often a
file that isn't there. In a profile of 200 lists against a copy of the
owner's real config it was 0.19s of 4.07s.

Load now reads the file's bytes through filememo (once until it
changes, remembered missing while a request holds it) and unmarshals a
Config of its own for each caller. Configure and Restore write through
edit.WriteAtomic, which forgets already; Off removes the file and now
calls filememo.Forget too, so sync turned off in a request is off for
the next Load in it.

Run (on mote unless named):
- go test -count=1 ./internal/davsync ok (36.9s); ./internal/gui red on
  TestMainWindowCorners only (macOS 27 on mote, #3949); GOOS=linux and
  windows go vet ./internal/davsync ok; gofmt clean (here).
- New TestLoadReadOnceARequest, -count=3 ok: 20 loads in one held
  request read once, a caller's change stays its own, and Off is seen
  at once. Broken by hand: Load reading the file each time -> "read 20
  times, want 1"; no Forget in Off -> "still on after Off".
- Here, 200 GET /api/providers on the sandbox with the real config
  copy (offline, proxy :9): davsync.Load 0.19s -> gone from the
  profile; total CPU within this machine's noise (4.53s -> 4.62s).
- Then rebased onto 8d8df0639 (nothing in internal/davsync or filememo); not rerun.

Not run: the full ./... suite; a real WebDAV or S3 sync (the write
paths are unchanged).
2026-11-01 13:46:44 +08:00

48 lines
1.2 KiB
Go

package davsync
import (
"os"
"testing"
"time"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/filememo"
)
// TestLoadReadOnceARequest: every Providers page poll asked Load, and
// each read sync.json; a request reads it once, and sync turned off in
// it is off at once.
func TestLoadReadOnceARequest(t *testing.T) {
newComputer(t).use(t)
if _, ok := Load(); ok {
t.Fatal("on with no sync.json")
}
os.MkdirAll(path(""), 0o755)
if err := edit.WriteAtomic(path("sync.json"), []byte(`{"url":"https://dav.example.com/dav/","user":"me"}`)); err != nil {
t.Fatal(err)
}
// written a while ago, so a request keeps what it read without a look
ago := time.Now().Add(-time.Hour)
if err := os.Chtimes(path("sync.json"), ago, ago); err != nil {
t.Fatal(err)
}
defer filememo.Hold()()
reads := loadReads.Load()
for range 20 {
c, ok := Load()
if !ok || c.User != "me" {
t.Fatalf("loaded %+v, %v", c, ok)
}
c.User = "changed"
}
if n := loadReads.Load() - reads; n != 1 {
t.Fatalf("20 loads in one request read %d times, want 1", n)
}
if err := Off(); err != nil {
t.Fatal(err)
}
if c, ok := Load(); ok {
t.Fatalf("still on after Off: %+v", c)
}
}