Skip to content

Commit

Permalink
Fix flaky SlidingWindowTest
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Stäber <fabian@fstab.de>
  • Loading branch information
fstab committed Apr 7, 2024
1 parent 5ae9e24 commit 96e4d5a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public SlidingWindow(Class<T> clazz, Supplier<T> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

public class SlidingWindowTest {

static class Observer {
class Observer {

List<Double> values = new ArrayList<>();
final List<Double> values = new ArrayList<>();

public void observe(double value) {
values.add(value);
Expand All @@ -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<Observer> ringBuffer;
private final long maxAgeSeconds = 30;
Expand All @@ -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;
}
Expand Down

0 comments on commit 96e4d5a

Please sign in to comment.