From 04acc048e3efa3f307faeb5f17f7069db727334d Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Thu, 19 Mar 2026 18:32:29 +0000 Subject: [PATCH] fix: stabilize flaky timer and thread count tests GaugeTest.testTimer: Thread.sleep(12) can take 100ms+ on CI due to scheduling delays. Assert only that the timer measured > 10ms, not that it's close to exactly 12ms. JvmThreadsMetricsTest.testInvalidThreadIds: background threads can start between scrapes, changing counts for arbitrary states. Assert only that the UNKNOWN count increased by the expected amount. Fixes #1971, fixes #1972 Signed-off-by: Gregor Zeitlinger --- .../metrics/core/metrics/GaugeTest.java | 3 +-- .../jvm/JvmThreadsMetricsTest.java | 17 +++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/GaugeTest.java b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/GaugeTest.java index be25711c7..9d101e8be 100644 --- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/GaugeTest.java +++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/GaugeTest.java @@ -87,8 +87,7 @@ public void testTimer() throws InterruptedException { try (Timer ignored = noLabels.startTimer()) { Thread.sleep(12); } - assertThat(getValue(noLabels)) - .isCloseTo(0.012, offset(0.005)); // 5ms delta should be enough so this isn't flaky + assertThat(getValue(noLabels)).isGreaterThan(0.01); } @Test diff --git a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetricsTest.java b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetricsTest.java index f001fad37..2d36373d5 100644 --- a/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetricsTest.java +++ b/prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetricsTest.java @@ -118,11 +118,8 @@ void testInvalidThreadIds() { // Number of threads to create with invalid thread ids int numberOfInvalidThreadIds = 2; - Map expected = getCountByState(registry.scrape()); - expected.compute( - "UNKNOWN", - (key, oldValue) -> - oldValue == null ? numberOfInvalidThreadIds : oldValue + numberOfInvalidThreadIds); + Map before = getCountByState(registry.scrape()); + double unknownBefore = before.getOrDefault("UNKNOWN", 0.0); final CountDownLatch countDownLatch = new CountDownLatch(numberOfInvalidThreadIds); @@ -132,12 +129,12 @@ void testInvalidThreadIds() { new ThreadWithInvalidId(-i, new TestRunnable(countDownLatch)).start(); } - Map actual = getCountByState(registry.scrape()); + Map after = getCountByState(registry.scrape()); + double unknownAfter = after.getOrDefault("UNKNOWN", 0.0); - assertThat(actual).hasSameSizeAs(expected); - for (String threadState : expected.keySet()) { - assertThat(actual.get(threadState)).isEqualTo(expected.get(threadState)); - } + // The UNKNOWN count should increase by exactly the number of invalid thread ids. + // Other states may change due to background threads, so we only assert on UNKNOWN. + assertThat(unknownAfter - unknownBefore).isEqualTo((double) numberOfInvalidThreadIds); } finally { for (int i = 0; i < numberOfInvalidThreadIds; i++) { countDownLatch.countDown();