Files
magpie/internal/gui/sessions_stats_cap_test.go
T
yetone 9dd5917a41 gui: the session-stats memo lets go of the range asked least recently, counted by asks rather than the clock (review of CaramelizedCUDA's cap, #1827)
9f0c9593 caps the memo at 8 ranges and stamps each with time.Now() when
asked. Its test only checked the count and that the newest range stays,
which passes with the eviction reversed (k.used.After) or with a hit that
doesn't restamp. TestStatsMemoKeepsTheRangeAskedAgain fills the memo, asks
range 1 again, adds one more and checks 1 is kept and 2 let go.

The stamp is now the memo's ask count: Windows' clock can read the same
for two asks in a row, so with time stamps the range asked again could
tie with one asked once and either could go.

Run (macOS, go1.26.8, sandbox HOME, private caches):
- reversed comparison (> for <): the new test fails, "the range asked
  again was let go before one asked once"; a hit that doesn't restamp:
  the same failure. Both pass with the change.
- go test -tags nogui -count=1 ./internal/gui ./internal/plugin: ok.
- gofmt -l clean; go vet on both packages for darwin, linux (nogui) and
  windows/amd64; go build for darwin, linux nogui and windows.
Not run: the whole suite (machine loaded), the new test on Windows.
2026-10-12 23:33:56 +08:00

46 lines
1.3 KiB
Go

package gui
import "testing"
// ?days is any number a caller sends, and each distinct value kept a whole
// sessions.Stats in statsMemo for good: 64 values, 64 snapshots. The memo
// keeps a few, the least recently asked let go, and the newest one stays.
func TestStatsMemoKeepsAFewDays(t *testing.T) {
forgetStats()
t.Cleanup(forgetStats)
for d := 1; d <= 64; d++ {
statsFor(d)
}
statsMemo.Lock()
n, newest := len(statsMemo.m), statsMemo.m[64] != nil
statsMemo.Unlock()
if n > 8 {
t.Fatalf("after 64 distinct ?days values the memo holds %d, want at most 8", n)
}
if !newest {
t.Fatal("the value asked last was let go")
}
}
// The range the page keeps asking for (the one it shows) is the one kept: a
// range asked again is newer than one asked once before it, so a run of new
// numbers lets go of the others first.
func TestStatsMemoKeepsTheRangeAskedAgain(t *testing.T) {
forgetStats()
t.Cleanup(forgetStats)
for d := 1; d <= statsMemoMost; d++ {
statsFor(d)
}
statsFor(1)
statsFor(100)
statsMemo.Lock()
kept1, kept2 := statsMemo.m[1] != nil, statsMemo.m[2] != nil
statsMemo.Unlock()
if !kept1 {
t.Fatal("the range asked again was let go before one asked once")
}
if kept2 {
t.Fatal("the least recently asked range is still kept past the cap")
}
}