From 7fb41d95a01493077c401a78756be656fca9168c Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Thu, 13 Aug 2026 12:54:58 -0700 Subject: [PATCH] chore: make the skip-value allocation test tolerant of unrelated allocations AllocsPerRun samples a process-wide allocation counter, so with runs=1 a single unrelated allocation on another goroutine (a timer, a finalizer) during the measured window fails the test. Average over 100 runs instead: the integer division absorbs stray allocations, while an allocation in the code under test still counts once per run and fails the test every time. The test body is otherwise unchanged. --- jreader/reader_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/jreader/reader_test.go b/jreader/reader_test.go index 5f88e31..dac2ac5 100644 --- a/jreader/reader_test.go +++ b/jreader/reader_test.go @@ -420,7 +420,13 @@ func TestReaderSkipValueAllocations(t *testing.T) { data := []byte(`{"a":1, "b":{"b1":"two", "b2":"three"}, "c":4}`) - allocs := testing.AllocsPerRun(1, func() { + // AllocsPerRun reads an allocation counter that applies to the whole process. An + // unrelated allocation on another goroutine, for example a timer or a finalizer, can + // increase the result of a single run. The average of 100 runs removes these errors, + // because AllocsPerRun divides the total allocation count by the run count and + // truncates the result. An allocation in the code under test occurs in each run, so + // the result stays 1 or more. + allocs := testing.AllocsPerRun(100, func() { r := NewReader(data) obj := r.Object() require.NoError(t, r.Error())