Skip to content

Commit 4cd2d3a

Browse files
authored
test: fix two flaky tests (darwin start_time regex, memstats HeapReleased drift) (#2050)
* test(process_collector): match scientific-notation start time on darwin TestDarwinProcessCollector asserted process_start_time_seconds against [0-9.]{10,}, but expfmt renders large gauge values in scientific notation (for example 1.783414e+09), whose leading digit run is shorter than ten characters. The check then fails intermittently on macOS depending on the process start time. Match a leading digit like the sibling metric checks, which asserts the metric is present with a numeric value. Signed-off-by: Kemal Akkoyun <kemal.akkoyun@datadoghq.com> * 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> --------- Signed-off-by: Kemal Akkoyun <kemal.akkoyun@datadoghq.com>
1 parent b0d896b commit 4cd2d3a

2 files changed

Lines changed: 11 additions & 6 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)

prometheus/process_collector_darwin_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ func TestDarwinProcessCollector(t *testing.T) {
5555
regexp.MustCompile("\nprocess_max_fds [1-9]"),
5656
regexp.MustCompile("\nprocess_open_fds [1-9]"),
5757
regexp.MustCompile("\nprocess_virtual_memory_max_bytes (-1|[1-9])"),
58-
regexp.MustCompile("\nprocess_start_time_seconds [0-9.]{10,}"),
58+
regexp.MustCompile("\nprocess_start_time_seconds [0-9]"),
5959
regexp.MustCompile("\nfoobar_process_cpu_seconds_total [0-9]"),
6060
regexp.MustCompile("\nfoobar_process_max_fds [1-9]"),
6161
regexp.MustCompile("\nfoobar_process_open_fds [1-9]"),
6262
regexp.MustCompile("\nfoobar_process_virtual_memory_max_bytes (-1|[1-9])"),
63-
regexp.MustCompile("\nfoobar_process_start_time_seconds [0-9.]{10,}"),
63+
regexp.MustCompile("\nfoobar_process_start_time_seconds [0-9]"),
6464
} {
6565
if !re.Match(buf.Bytes()) {
6666
t.Errorf("want body to match %s\n%s", re, buf.String())

0 commit comments

Comments
 (0)