Files
magpie/internal/sessions/main_test.go
T
yetone a87f051974 sessions: the index is written at most once a minute, not after every poll
A Sessions or Usage page polls every 15s, and a live session changes its
file between any two polls, so each poll's refresh saved the whole index
again: tens of MB of JSON for a long history, and encodeCache allocated
~20-30 MB a write (170 MB over 8 polls). The index only spares the next
start a read, and a parse a save hasn't written yet is read again then,
so a save now waits saveGap (1 minute) after the last one written; saves
in a row are still folded into the latest. Saved (a CLI or the app
ending) sets hurry, so every save queued is written at once without the
gap, and an unused wake-up is drained when the saves end so it cuts no
later gap short. The package's tests write saves as they come
(saveGap = 0 in TestMain).

Run:
- TestSaveWaitsOutTheGap (new): a second and third save right after the
  first are not written while the gap stands, and Saved writes them, as
  one, at once. Red with the wait removed ("2 writes with saveGap not
  yet past") and red with Saved not hurrying ("Saved waited out the
  gap", 10s).
- go test -timeout 45m ./internal/sessions ./internal/gui ./internal/usage
  ./internal/usageapi ./internal/cli ./internal/tui: ok at this change on
  205e3b8ae. gofmt clean; GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/sessions ok.
- A/B, 8 sequential polls of the Sessions and Usage pages under a
  sandbox HOME with the real ~/.codex and ~/.claude sessions (live, so
  totals vary run to run): writeCache 169.5 -> 95.9 MB, encodeCache
  163.7 -> 92.5 MB; the run spans a few minutes, so a write a minute
  still lands in it. At the GUI's 15s polls that is one write a minute
  instead of four.
Not tried: quitting the real app and checking the index on disk; the
CLI's exit path calls Saved, as before.

Rebased on 2badacaad (provider and plugin changes): go test -count=1
./internal/sessions ./internal/gui ok at 26ed62752 (15.6s, 162.0s).
2026-10-27 10:21:10 +08:00

19 lines
466 B
Go

package sessions
import (
"testing"
"github.com/yetone/magpie/internal/testenv"
)
// The tests run in a home of their own, never the user's real files, and
// a scan's buffer given back is filled with garbage, so a reader that kept
// a line past its scan fails here rather than for a user.
func TestMain(m *testing.M) {
poisonReleased = true
// a test's saves are written as they come; TestSaveWaitsOutTheGap
// sets the gap
saveGap = 0
testenv.Main(m)
}