Skip to content

Commit 6c4d9f0

Browse files
authored
Fix unstable TimerTest (#5957)
Closes #5937 (cherry picked from commit 854dd26)
1 parent 98fa0aa commit 6c4d9f0

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/test/java/io/vertx/core/TimerTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ public void testPeriodicWithInitialDelay2() {
6464
*/
6565
@Test
6666
public void testTimings() {
67-
final long start = System.currentTimeMillis();
67+
final long start = System.nanoTime();
6868
final long delay = 2000;
6969
vertx.setTimer(delay, timerID -> {
70-
long dur = System.currentTimeMillis() - start;
71-
assertTrue(dur >= delay);
70+
long dur = System.nanoTime() - start;
71+
assertTrue(dur >= TimeUnit.MILLISECONDS.toNanos(delay));
7272
long maxDelay = delay * 2;
73-
assertTrue("Timer accuracy: " + dur + " vs " + maxDelay, dur < maxDelay); // 100% margin of error (needed for CI)
73+
assertTrue("Timer accuracy: " + dur + " vs " + TimeUnit.MILLISECONDS.toNanos(maxDelay), dur < TimeUnit.MILLISECONDS.toNanos(maxDelay)); // 100% margin of error (needed for CI)
7474
vertx.cancelTimer(timerID);
7575
testComplete();
7676
});
@@ -121,11 +121,11 @@ static class PeriodicArg {
121121
private void periodic(PeriodicArg delay, BiFunction<PeriodicArg, Handler<Long>, Long> abc) {
122122
final int numFires = 10;
123123
final AtomicLong id = new AtomicLong(-1);
124-
long now = System.currentTimeMillis();
124+
long now = System.nanoTime();
125125
id.set(abc.apply(delay, new Handler<Long>() {
126126
int count;
127127
public void handle(Long timerID) {
128-
assertTrue( System.currentTimeMillis() - now >= delay.initialDelay + count * delay.delay);
128+
assertTrue( System.nanoTime() - now >= TimeUnit.MILLISECONDS.toNanos(delay.initialDelay + count * delay.delay));
129129
assertEquals(id.get(), timerID.longValue());
130130
count++;
131131
if (count == numFires) {
@@ -553,10 +553,10 @@ public void start() throws Exception {
553553

554554
@Test
555555
public void testTimerFire() {
556-
long now = System.currentTimeMillis();
556+
long now = System.nanoTime();
557557
Timer timer = vertx.timer(1, TimeUnit.SECONDS);
558558
timer.onComplete(onSuccess(v -> {
559-
assertTrue(System.currentTimeMillis() - now >= 800);
559+
assertTrue(System.nanoTime() - now >= TimeUnit.SECONDS.toNanos(1));
560560
testComplete();
561561
}));
562562
await();

0 commit comments

Comments
 (0)