|
17 | 17 | import io.vertx.core.internal.VertxInternal; |
18 | 18 | import io.vertx.test.core.Repeat; |
19 | 19 | import io.vertx.test.core.VertxTestBase; |
| 20 | +import org.hamcrest.Matchers; |
20 | 21 | import org.junit.Test; |
21 | 22 |
|
22 | 23 | import java.util.concurrent.CancellationException; |
@@ -62,13 +63,13 @@ public void testPeriodicWithInitialDelay2() { |
62 | 63 | */ |
63 | 64 | @Test |
64 | 65 | public void testTimings() { |
65 | | - final long start = System.currentTimeMillis(); |
| 66 | + final long start = System.nanoTime(); |
66 | 67 | final long delay = 2000; |
67 | 68 | vertx.setTimer(delay, timerID -> { |
68 | | - long dur = System.currentTimeMillis() - start; |
69 | | - assertTrue(dur >= delay); |
| 69 | + long dur = System.nanoTime() - start; |
| 70 | + assertTrue(dur >= TimeUnit.MILLISECONDS.toNanos(delay)); |
70 | 71 | long maxDelay = delay * 2; |
71 | | - assertTrue("Timer accuracy: " + dur + " vs " + maxDelay, dur < maxDelay); // 100% margin of error (needed for CI) |
| 72 | + assertTrue("Timer accuracy: " + dur + " vs " + TimeUnit.MILLISECONDS.toNanos(maxDelay), dur < TimeUnit.MILLISECONDS.toNanos(maxDelay)); // 100% margin of error (needed for CI) |
72 | 73 | vertx.cancelTimer(timerID); |
73 | 74 | testComplete(); |
74 | 75 | }); |
@@ -119,11 +120,11 @@ static class PeriodicArg { |
119 | 120 | private void periodic(PeriodicArg delay, BiFunction<PeriodicArg, Handler<Long>, Long> abc) { |
120 | 121 | final int numFires = 10; |
121 | 122 | final AtomicLong id = new AtomicLong(-1); |
122 | | - long now = System.currentTimeMillis(); |
| 123 | + long now = System.nanoTime(); |
123 | 124 | id.set(abc.apply(delay, new Handler<Long>() { |
124 | 125 | int count; |
125 | 126 | public void handle(Long timerID) { |
126 | | - assertTrue( System.currentTimeMillis() - now >= delay.initialDelay + count * delay.delay); |
| 127 | + assertThat(System.nanoTime() - now, Matchers.greaterThanOrEqualTo(TimeUnit.MILLISECONDS.toNanos(delay.initialDelay + count * delay.delay))); |
127 | 128 | assertEquals(id.get(), timerID.longValue()); |
128 | 129 | count++; |
129 | 130 | if (count == numFires) { |
@@ -363,10 +364,10 @@ public void start() throws Exception { |
363 | 364 |
|
364 | 365 | @Test |
365 | 366 | public void testTimerFire() { |
366 | | - long now = System.currentTimeMillis(); |
| 367 | + long now = System.nanoTime(); |
367 | 368 | Timer timer = vertx.timer(1, TimeUnit.SECONDS); |
368 | 369 | timer.onComplete(onSuccess(v -> { |
369 | | - assertTrue(System.currentTimeMillis() - now >= 800); |
| 370 | + assertTrue(System.nanoTime() - now >= TimeUnit.SECONDS.toNanos(1)); |
370 | 371 | testComplete(); |
371 | 372 | })); |
372 | 373 | await(); |
|
0 commit comments