Files
magpie/internal/gui/warm_hold_test.go
T
yetone fcbbb8e479 gui: the start's warm-up and read-ahead look at each file once a second (#1665)
The warm-up after a start (sessions, usage, Codex effort) and the usage
read-ahead asked for settings.json, logins.json and the rest for each
session and row they read: a Windows sandbox's second start stat'ed
settings.json 677 times in 25s, 286 of them settings.load and
AccountDiscoveryOff under the warm-up. Both now hold what was read
(filememo.Hold), as the GUI's requests and a catalog sync's round do. A
write still reads the disk (edit.Read, settings' writes, logins.json
under loginsMu), so nothing is written from a held read; a file another
app changes is seen up to a second late while they run.

Measured on xiaomi-gpu (Windows 11), an empty sandbox HOME, magpie web
--gateway, second start, 27s, with a build counting each look at the
disk (instrumentation not committed):
  before   1113 looks, settings.json 677
  after     483 looks, settings.json 114
The catalog sync's round stays at ~100ms.

Run:
- TestWarmUpHoldsWhatItReads: red with the warm-up's Hold taken out by
  hand (the second read saw the other app's change), green with it.
- mote: gofmt -l internal/gui clean; GOOS=windows and darwin go vet
  ./internal/gui and GOOS=linux go vet -tags nogui ./internal/gui clean;
  go test -count=1 ./internal/gui ./internal/usage: usage ok, gui red
  only on TestMainWindowCorners (#3949, mote's macOS 27, red on main
  before this).
2026-10-28 13:51:37 +08:00

48 lines
1.2 KiB
Go

package gui
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/filememo"
)
// The warm-up's reads look at a file once a second, not for each session
// or row that asks (#1665: settings.json ~300 times on Windows): a file
// another app changed meanwhile is read as it was, inside that second.
func TestWarmUpHoldsWhatItReads(t *testing.T) {
p := filepath.Join(t.TempDir(), "f.json")
old := time.Now().Add(-time.Hour)
write := func(s string, at time.Time) {
if err := os.WriteFile(p, []byte(s), 0o600); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(p, at, at); err != nil {
t.Fatal(err)
}
}
read := func() string {
b, err := filememo.Read("warm-hold-test", p, func(b []byte) (string, error) { return string(b), nil })
if err != nil {
t.Fatal(err)
}
return string(b)
}
write("one", old)
var got string
warmUp(func() {
read()
write("two", old.Add(time.Minute))
got = read()
})
if got != "one" {
t.Fatalf("the warm-up looked at the file again for its second read: got %q", got)
}
// and nothing is held once it ends
if got := read(); got != "two" {
t.Fatalf("after the warm-up the file reads %q, not what is on the disk", got)
}
}