From aabb0a86ec3a78d999e392fc7dab98b488e2bf9f Mon Sep 17 00:00:00 2001 From: Matthias Bertschy Date: Fri, 26 Jun 2026 13:04:48 +0200 Subject: [PATCH] test: deflake TestCompareDynamic_ZeroAllocHotPath The zero-allocation assertion measured allocations by diffing the process-global runtime.MemStats.Mallocs counter around a 10k-iteration loop. Mallocs is process-wide, so a single allocation from a background runtime goroutine (GC assist, sysmon, the testing timer) landing inside the measurement window flipped the delta to 1 and failed the assertion. The microsecond-scale window made this rare but nonzero, producing intermittent failures under the full `go test ./...` run (observed flipping between different subtests across runs). Switch to testing.AllocsPerRun, which pins GOMAXPROCS(1) during measurement and integer-divides total mallocs by the run count. A stray sub-run-count allocation now floors to 0 instead of flaking, while a genuine per-call allocation still produces ~1.0 and fails the contract. Verified 30/30 isolated runs green and the full module suite green. Co-Authored-By: Claude Opus 4.8 (1M context) Docs-exempt: test-only change, no behavioral change Signed-off-by: Matthias Bertschy --- .../tests/compare_dynamic_memoise_test.go | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/pkg/registry/file/dynamicpathdetector/tests/compare_dynamic_memoise_test.go b/pkg/registry/file/dynamicpathdetector/tests/compare_dynamic_memoise_test.go index 756ea5d1f..a9a0fa839 100644 --- a/pkg/registry/file/dynamicpathdetector/tests/compare_dynamic_memoise_test.go +++ b/pkg/registry/file/dynamicpathdetector/tests/compare_dynamic_memoise_test.go @@ -307,22 +307,19 @@ func TestCompareDynamic_ZeroAllocHotPath(t *testing.T) { } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - // Warm-up to absorb any one-off setup cost. - for i := 0; i < 100; i++ { + // testing.AllocsPerRun pins GOMAXPROCS(1) during measurement and + // integer-divides total mallocs by the run count, so a stray + // allocation from a background runtime goroutine (GC assist, sysmon, + // the testing timer) landing in the measurement window floors to 0 + // instead of flaking the assertion. A genuine per-call allocation + // still produces ~1.0 (one per run) and fails the contract. + const runs = 10_000 + allocs := testing.AllocsPerRun(runs, func() { _ = dynamicpathdetector.CompareDynamic(tc.dyn, tc.reg) - } - var before, after runtime.MemStats - runtime.GC() - runtime.ReadMemStats(&before) - const iters = 10_000 - for i := 0; i < iters; i++ { - _ = dynamicpathdetector.CompareDynamic(tc.dyn, tc.reg) - } - runtime.ReadMemStats(&after) - allocs := after.Mallocs - before.Mallocs - require.Equalf(t, uint64(0), allocs, - "%s: %d allocs across %d iters — 0/1-`*` shapes MUST be zero-allocation", - tc.name, allocs, iters) + }) + require.Zerof(t, allocs, + "%s: %.4f allocs/call — 0/1-`*` shapes MUST be zero-allocation per Matthias upstream PR #323", + tc.name, allocs) }) } }