From 96e4d5a6f15af3bed7dc906f6f470c25463a2712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20St=C3=A4ber?= Date: Sun, 7 Apr 2024 22:58:53 +0200 Subject: [PATCH] Fix flaky SlidingWindowTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabian Stäber --- .../prometheus/metrics/core/metrics/SlidingWindow.java | 2 +- .../metrics/core/metrics/SlidingWindowTest.java | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/SlidingWindow.java b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/SlidingWindow.java index 62164a48b..e4e86acdd 100644 --- a/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/SlidingWindow.java +++ b/prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/SlidingWindow.java @@ -43,7 +43,7 @@ public SlidingWindow(Class clazz, Supplier constructor, ObjDoubleConsumer< this.ringBuffer[i] = constructor.get(); } this.currentBucket = 0; - this.lastRotateTimestampMillis = System.currentTimeMillis(); + this.lastRotateTimestampMillis = currentTimeMillis.getAsLong(); this.durationBetweenRotatesMillis = TimeUnit.SECONDS.toMillis(maxAgeSeconds) / ageBuckets; } diff --git a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/SlidingWindowTest.java b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/SlidingWindowTest.java index 3b57220f8..3461d959e 100644 --- a/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/SlidingWindowTest.java +++ b/prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/SlidingWindowTest.java @@ -10,9 +10,9 @@ public class SlidingWindowTest { - static class Observer { + class Observer { - List values = new ArrayList<>(); + final List values = new ArrayList<>(); public void observe(double value) { values.add(value); @@ -23,10 +23,11 @@ void assertValues(double... expectedValues) { for (double expectedValue : expectedValues) { expectedList.add(expectedValue); } - Assert.assertEquals(expectedList, values); + Assert.assertEquals("Start time: " + startTime + ", current time: " + currentTimeMillis.get() + ", elapsed time: " + (currentTimeMillis.get() - startTime), expectedList, values); } } + private long startTime; private final AtomicLong currentTimeMillis = new AtomicLong(); private SlidingWindow ringBuffer; private final long maxAgeSeconds = 30; @@ -35,7 +36,8 @@ void assertValues(double... expectedValues) { @Before public void setUp() { - currentTimeMillis.set(System.currentTimeMillis()); + startTime = System.currentTimeMillis(); + currentTimeMillis.set(startTime); ringBuffer = new SlidingWindow<>(Observer.class, Observer::new, Observer::observe, maxAgeSeconds, ageBuckets); ringBuffer.currentTimeMillis = currentTimeMillis::get; }