From dfa42c07cd3cb2229b8a0999f50b77b6eba20565 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 6 Nov 2026 12:47:51 +0800 Subject: [PATCH] agent tests: an allocation count is the fewest of five rounds TestAlmaProvidersSeenDecodeOnce (v0.1.3114) went red in the whole internal/agent package on mote: 401 allocations against its bar of 400, green alone. testing.AllocsPerRun counts every goroutine's allocations, and another test's watcher or poll left running lands in a round now and then. leastAllocs takes the fewest of five rounds, which such a stray reaches in one round, not in all five; leastBytes does the same for bytes (runtime.MemStats TotalAlloc). Run (mote, at this tree): - gofmt -l internal, go vet ./..., GOOS=windows and GOOS=linux go vet ./internal/agent/ ./internal/provider/: clean - go test -count=1 -timeout 45m ./... (all three commits' tree): 85 packages ok, internal/gateway 748s, internal/provider 193s. Two red: internal/gui's TestMainWindowCorners/macOS_26_SDK, mote's macOS 27 (#3949, filed before, untouched here), and internal/agent's TestCodexEffortOptionsReadTheKeptList, whose bar counted the environment's copy (see the effort test's comment), fixed in its test. - then go test -count=1 -timeout 45m ./internal/agent/ with that fix: ok (200.1s) Broken by hand: with the bytes.Equal check in almaProvidersSeen removed the look measures 7232 allocations and goes red, as before. --- internal/agent/alma_seen_test.go | 4 ++-- internal/agent/least_allocs_test.go | 35 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 internal/agent/least_allocs_test.go diff --git a/internal/agent/alma_seen_test.go b/internal/agent/alma_seen_test.go index a351ff0f5..34a91d7ec 100644 --- a/internal/agent/alma_seen_test.go +++ b/internal/agent/alma_seen_test.go @@ -46,7 +46,7 @@ func TestAlmaProvidersSeenDecodeOnce(t *testing.T) { if err != nil || len(ps) != 1 || ps[0].Name != "magpie" || len(ps[0].Known) != 400 || ps[0].Models[0] != "gpt-5.4" { t.Fatalf("first look: %v %+v", err, ps) } - if n := testing.AllocsPerRun(20, func() { + if n := leastAllocs(20, func() { if ps, err := almaProvidersSeen(); err != nil || len(ps) != 1 { t.Fatal(err, ps) } @@ -59,7 +59,7 @@ func TestAlmaProvidersSeenDecodeOnce(t *testing.T) { // the same answer asked for again, past the time one is kept, is not // decoded again either almaReadFor = 0 - if n := testing.AllocsPerRun(5, func() { + if n := leastAllocs(5, func() { if ps, err := almaProvidersSeen(); err != nil || len(ps) != 1 { t.Fatal(err, ps) } diff --git a/internal/agent/least_allocs_test.go b/internal/agent/least_allocs_test.go new file mode 100644 index 000000000..f014c4863 --- /dev/null +++ b/internal/agent/least_allocs_test.go @@ -0,0 +1,35 @@ +package agent + +import ( + "runtime" + "testing" +) + +// leastAllocs is testing.AllocsPerRun's fewest over five rounds: it counts +// every goroutine's allocations, and another test's left running (a +// watcher, a poll) lands in one round now and then, never in all five. +func leastAllocs(runs int, f func()) float64 { + least := testing.AllocsPerRun(runs, f) + for range 4 { + least = min(least, testing.AllocsPerRun(runs, f)) + } + return least +} + +// leastBytes is the fewest bytes a run of f allocated, over five rounds of +// runs, for the reason leastAllocs gives. +func leastBytes(runs int, f func()) uint64 { + var least uint64 + for i := range 5 { + var before, after runtime.MemStats + runtime.ReadMemStats(&before) + for range runs { + f() + } + runtime.ReadMemStats(&after) + if n := (after.TotalAlloc - before.TotalAlloc) / uint64(runs); i == 0 || n < least { + least = n + } + } + return least +}