Skip to content

Commit ce5867b

Browse files
authored
fix: stabilize flaky timer and thread count tests (#1973)
## Summary - **GaugeTest.testTimer**: `Thread.sleep(12)` can take 100ms+ on CI. Assert `> 10ms` instead of `closeTo(12ms, 5ms)` - **JvmThreadsMetricsTest.testInvalidThreadIds**: background threads can start between scrapes. Assert only that UNKNOWN count increased by the expected amount, ignore other states Fixes #1971, fixes #1972 ## Test plan - [x] Both tests pass locally - [x] `mise run build` passes - [ ] CI passes Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent d8388fb commit ce5867b

File tree

2 files changed

+8
-12
lines changed
  • prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics
  • prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm

2 files changed

+8
-12
lines changed

prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/GaugeTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ public void testTimer() throws InterruptedException {
8787
try (Timer ignored = noLabels.startTimer()) {
8888
Thread.sleep(12);
8989
}
90-
assertThat(getValue(noLabels))
91-
.isCloseTo(0.012, offset(0.005)); // 5ms delta should be enough so this isn't flaky
90+
assertThat(getValue(noLabels)).isGreaterThan(0.01);
9291
}
9392

9493
@Test

prometheus-metrics-instrumentation-jvm/src/test/java/io/prometheus/metrics/instrumentation/jvm/JvmThreadsMetricsTest.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,8 @@ void testInvalidThreadIds() {
118118
// Number of threads to create with invalid thread ids
119119
int numberOfInvalidThreadIds = 2;
120120

121-
Map<String, Double> expected = getCountByState(registry.scrape());
122-
expected.compute(
123-
"UNKNOWN",
124-
(key, oldValue) ->
125-
oldValue == null ? numberOfInvalidThreadIds : oldValue + numberOfInvalidThreadIds);
121+
Map<String, Double> before = getCountByState(registry.scrape());
122+
double unknownBefore = before.getOrDefault("UNKNOWN", 0.0);
126123

127124
final CountDownLatch countDownLatch = new CountDownLatch(numberOfInvalidThreadIds);
128125

@@ -132,12 +129,12 @@ void testInvalidThreadIds() {
132129
new ThreadWithInvalidId(-i, new TestRunnable(countDownLatch)).start();
133130
}
134131

135-
Map<String, Double> actual = getCountByState(registry.scrape());
132+
Map<String, Double> after = getCountByState(registry.scrape());
133+
double unknownAfter = after.getOrDefault("UNKNOWN", 0.0);
136134

137-
assertThat(actual).hasSameSizeAs(expected);
138-
for (String threadState : expected.keySet()) {
139-
assertThat(actual.get(threadState)).isEqualTo(expected.get(threadState));
140-
}
135+
// The UNKNOWN count should increase by exactly the number of invalid thread ids.
136+
// Other states may change due to background threads, so we only assert on UNKNOWN.
137+
assertThat(unknownAfter - unknownBefore).isEqualTo((double) numberOfInvalidThreadIds);
141138
} finally {
142139
for (int i = 0; i < numberOfInvalidThreadIds; i++) {
143140
countDownLatch.countDown();

0 commit comments

Comments
 (0)