Files
magpie/internal/gui/page_memory_test.go
T
yetone 80a315a70a gui: a closed or hidden page lets its Sessions and Usage caches go in 30 s
What a visit to the app's page read stayed until each cache's own idle
timer fired: the Sessions stats after 2 minutes, the Requests chunks
trimmed at 2 and forgotten 5 after that, each chunk's derived keys at 3.
On a real history (acctp, requests-v1.bin 71.5 MB) a web page open 70 s
and closed held 123 MB at the close, 108 MB a minute on, 74-76 MB three
to five minutes on, and was back to the tray's 52 MB only at ten.

The page now says it has gone from view (POST /api/page-away, sent from
the visibilitychange listener every window already has, when hidden: a
browser fires it as a tab closes, and the Mac window's orderOut
dispatches it too). 30 s later (pageAwayFor), unless a Sessions or Usage read
was in flight or ended since, the stats, overviews and tags go
(letGoStats, split out of forgetIdleStats), the Requests chunks and each
chunk's derived caches go (usage.ForgetPages: trim(0) + dropDerived,
idle timers stopped) and the pages go back to the system. A second tab
still polling, or a tab looked away from for under 30 s, keeps them. The
beacon writes nothing, so held's provider.Changed passes it by. The panel
sends it as well when it hides; it reads no Sessions or Usage page, so
there is nothing of its own to keep.

Tests: TestPageAwayLetsStatsGo (red with the forget skipped, and red
when a read after the beacon is ignored, both broken by hand) and
TestForgetPagesLetsChunksGoAtOnce.

The core script budget goes up 83 bytes, with the reason in its
comment: the beacon is folded into the existing listener and packed to
its shortest form (335 over as first written, with a pagehide listener
and a panel check).

Measured on mote (pageclose2.sh, the acctp history, a web page open
70 s and closed, footprint-overlay build like the release):
  before (w88): close 123 MB, +60 s 108, +180 s 76, +300 s 74, +600 s 52;
                peak 154
  after  (w89): close 123 MB, +45 s 92, +90 s 90, +180 s 54, +600 s 53;
                peak 149, 3.08 s CPU over 11 min
  final (w90, visibilitychange only): close 122 MB, +45 s 93, +90 s 90,
                +180 s 55, +600 s 52; peak 152

Run: go test ./internal/gui ./internal/usage on mote (ok);
GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./... (ok);
16 visibility GUI files + i18n-keys in chromium and webkit: red only in
routing-pin-away (the same subtest red alone at origin/main 4739ec92c,
2026-11-14 17:00:00 +08:00

150 lines
3.4 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
// The pages a poll's reads left go back once no read is in flight: not
// while one is, once when the last ends, and a rest a new read cut short
// is waited out again.
func TestPageReadsReleasedAtRest(t *testing.T) {
released := make(chan struct{}, 4)
pageReads.Lock()
pageReads.rest, pageReads.release = 20*time.Millisecond, func() { released <- struct{}{} }
pageReads.Unlock()
t.Cleanup(func() {
pageReads.Lock()
if pageReads.due != nil {
pageReads.due.Stop()
pageReads.due = nil
}
pageReads.rest, pageReads.release = 0, nil
pageReads.Unlock()
})
a, b := readingPage(), readingPage()
a()
// one is still in flight
releaseIfPagesRest()
select {
case <-released:
t.Fatal("released while a read was in flight")
default:
}
b()
b() // done twice ends it once
select {
case <-released:
case <-time.After(10 * time.Second):
t.Fatal("not released once every read ended")
}
// a rest not long enough yet: the release waits out the rest of it
stopDue := func() {
if pageReads.due != nil {
pageReads.due.Stop()
pageReads.due = nil
}
}
pageReads.Lock()
pageReads.rest = time.Hour
pageReads.Unlock()
readingPage()()
pageReads.Lock()
stopDue()
pageReads.Unlock()
releaseIfPagesRest()
pageReads.Lock()
waiting := pageReads.due != nil
stopDue()
pageReads.last = time.Time{}
pageReads.Unlock()
if !waiting {
t.Error("a rest cut short isn't waited out again")
}
select {
case <-released:
t.Fatal("released before the reads rested")
default:
}
releaseIfPagesRest()
select {
case <-released:
default:
t.Fatal("not released once the reads rested")
}
}
// held counts the Sessions and Usage pages' reads, and no other request.
func TestHeldCountsPageReads(t *testing.T) {
in := -1
h := held(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
pageReads.Lock()
in = pageReads.n
pageReads.Unlock()
}))
t.Cleanup(func() {
pageReads.Lock()
if pageReads.due != nil {
pageReads.due.Stop()
pageReads.due = nil
}
pageReads.Unlock()
})
for path, want := range map[string]int{"/api/sessions": 1, "/api/usage/requests": 1, "/api/state": 0} {
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, path, nil))
if in != want {
t.Errorf("%s: %d page reads in flight while it was answered, want %d", path, in, want)
}
}
}
// The app's page gone away (closed or hidden) has the Sessions page's
// stats let go pageAwayFor later, not the minutes the idle timers take;
// a page read since it went away keeps them.
func TestPageAwayLetsStatsGo(t *testing.T) {
forgetStats()
t.Cleanup(forgetStats)
old := pageAwayFor
pageAwayFor = 20 * time.Millisecond
t.Cleanup(func() {
pageAway.Lock()
if pageAway.t != nil {
pageAway.t.Stop()
pageAway.t = nil
}
pageAway.Unlock()
pageAwayFor = old
pageReads.Lock()
if pageReads.due != nil {
pageReads.due.Stop()
pageReads.due = nil
}
pageReads.Unlock()
})
kept := func() bool {
statsMemo.Lock()
defer statsMemo.Unlock()
return statsMemo.m[0] != nil
}
// read again after it went away: kept
statsFor(0)
awayPage()
readingPage()()
time.Sleep(100 * time.Millisecond)
if !kept() {
t.Fatal("stats let go though a page read them after it went away")
}
awayPage()
for deadline := time.Now().Add(10 * time.Second); kept(); time.Sleep(5 * time.Millisecond) {
if time.Now().After(deadline) {
t.Fatal("stats still kept after the page went away")
}
}
}