tests: a decoder test counts its cheapest read, so another goroutine's allocations don't fail it (#3283)

TestPackedScanKeepsItsDecoder went red on macOS CI at 3a213e1dc, a
CSS-only commit: 665,955 bytes against a 512 KiB bar. allocated()
averaged runtime.MemStats.TotalAlloc over its calls, and TotalAlloc
counts every goroutine in the process, so another test's leftover work
was billed to the scan. It now returns the least any one call
allocated. A decoder made anew costs its window on every call (about
3 MiB for a packed rollout), so the least still shows it; work that
overlaps one call no longer does. TestDshFramesKeepTheirDecoder shares
the helper and gets the same fix.

Run at 8c68b6802 (origin/main), internal/sessions only (test-only change):
- Injected noise (a goroutine allocating 256 KiB every 8 ms): old
  helper red 3 of 4 runs, new helper green 4 of 4. Two forced
  runtime.GC() calls between scans leave the old helper green, so the
  pool being emptied is not the cause.
- Broken by hand: openLines making a new decoder each read turns
  TestPackedScanKeepsItsDecoder red (3,330,376 bytes); frameDecoders.New
  in place of Get turns TestDshFramesKeepTheirDecoder red (165,512).
- go test ./internal/sessions/ ok; the two tests -count=10 ok; gofmt
  clean; vet darwin, linux (-tags nogui), windows ok.

Not run: the full suite. Nothing outside this test file changes.
This commit is contained in:
yetone
2026-10-22 22:03:16 +08:00
parent 8c68b6802a
commit ba1dc7f975
+10 -6
View File
@@ -108,17 +108,21 @@ func dshPacked(t testing.TB, dir string, size int) (string, int64) {
return p, last
}
// allocated is the bytes fn allocates a call, on average over n calls
// after a first one.
// allocated is the bytes fn allocates a call, the least of n calls after
// a first one. TotalAlloc counts every goroutine in the process, so
// another test's leftover work lands on whichever call it overlaps; a
// decoder made anew lands on every call, and the least still shows it.
func allocated(n int, fn func()) uint64 {
fn()
var a, b runtime.MemStats
runtime.ReadMemStats(&a)
least := ^uint64(0)
for i := 0; i < n; i++ {
var a, b runtime.MemStats
runtime.ReadMemStats(&a)
fn()
runtime.ReadMemStats(&b)
least = min(least, b.TotalAlloc-a.TotalAlloc)
}
runtime.ReadMemStats(&b)
return (b.TotalAlloc - a.TotalAlloc) / uint64(n)
return least
}
func scanPacked(t testing.TB, p string) int {