Skip to content

Commit dd9aa65

Browse files
committed
test(collector): stabilize TestMemStatsEquivalence against HeapReleased drift
The test read runtime.ReadMemStats and runtime/metrics separately and compared them within 5%, with the code itself noting it hoped no GC ran in between. HeapReleased is driven by the background scavenger and drifted between the two reads, failing intermittently (seen on Linux arm64). Force a full scavenge with debug.FreeOSMemory and disable GC for the measurement window so both reads observe the same runtime state. Signed-off-by: Kemal Akkoyun <kemal.akkoyun@datadoghq.com>
1 parent 6cdc504 commit dd9aa65

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

prometheus/go_collector_latest_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"reflect"
2222
"regexp"
2323
"runtime"
24+
"runtime/debug"
2425
"runtime/metrics"
2526
"strings"
2627
"sync"
@@ -270,12 +271,16 @@ func TestMemStatsEquivalence(t *testing.T) {
270271
samplesMap[descs[i].Name] = &samples[i]
271272
}
272273

273-
// Force a GC cycle to try to reach a clean slate.
274-
runtime.GC()
274+
// Reach a stable slate and hold it for a single measurement window.
275+
// FreeOSMemory runs a GC and returns freed memory to the OS, leaving the
276+
// background scavenger nothing to release; disabling GC for the window
277+
// keeps msReal and msFake observing the same runtime state. Without this,
278+
// HeapReleased could drift between the two reads and fail the comparison.
279+
debug.FreeOSMemory()
280+
defer debug.SetGCPercent(debug.SetGCPercent(-1))
275281

276-
// Populate msReal.
282+
// Populate msReal, then msFake back-to-back within the frozen window.
277283
runtime.ReadMemStats(&msReal)
278-
// Populate msFake and hope that no GC happened in between (:
279284
metrics.Read(samples)
280285

281286
memStatsFromRM(&msFake, samplesMap)

0 commit comments

Comments
 (0)