@author Tim Fox
| 35 | * @author <a href="http://tfox.org">Tim Fox</a> |
| 36 | */ |
| 37 | public class TimerTest extends VertxTestBase2 { |
| 38 | |
| 39 | @Test |
| 40 | public void testTimer(Checkpoint checkpoint) { |
| 41 | timer(checkpoint, 1); |
| 42 | } |
| 43 | |
| 44 | @Test |
| 45 | public void testPeriodic1(Checkpoint checkpoint) { |
| 46 | periodic(checkpoint, new PeriodicArg(100, 100), (delay, handler) -> vertx.setPeriodic(delay.delay, handler)); |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void testPeriodic2(Checkpoint checkpoint) { |
| 51 | periodic(checkpoint, new PeriodicArg(100, 100), (delay, handler) -> vertx.setPeriodic(delay.delay, delay.delay, handler)); |
| 52 | } |
| 53 | |
| 54 | @Test |
| 55 | public void testPeriodicWithInitialDelay1(Checkpoint checkpoint) { |
| 56 | periodic(checkpoint, new PeriodicArg(0, 100), (delay, handler) -> vertx.setPeriodic(delay.initialDelay, delay.delay, handler)); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testPeriodicWithInitialDelay2(Checkpoint checkpoint) { |
| 61 | periodic(checkpoint, new PeriodicArg(100, 200), (delay, handler) -> vertx.setPeriodic(delay.initialDelay, delay.delay, handler)); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Test the timers fire with approximately the correct delay |
| 66 | */ |
| 67 | @Test |
| 68 | public void testTimings(Checkpoint checkpoint) { |
| 69 | final long start = System.nanoTime(); |
| 70 | final long delay = 2000; |
| 71 | vertx.setTimer(delay, timerID -> { |
| 72 | long dur = System.nanoTime() - start; |
| 73 | Assert.assertTrue(dur >= TimeUnit.MILLISECONDS.toNanos(delay)); |
| 74 | long maxDelay = delay * 2; |
| 75 | Assert.assertTrue("Timer accuracy: " + dur + " vs " + TimeUnit.MILLISECONDS.toNanos(maxDelay), dur < TimeUnit.MILLISECONDS.toNanos(maxDelay)); // 100% margin of error (needed for CI) |
| 76 | vertx.cancelTimer(timerID); |
| 77 | checkpoint.succeed(); |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testInVerticle(Checkpoint checkpoint1) { |
| 83 | class MyVerticle extends AbstractVerticle { |
| 84 | @Override |
| 85 | public void start() { |
| 86 | Thread thr = Thread.currentThread(); |
| 87 | vertx.setTimer(1, id -> { |
| 88 | Assert.assertSame(thr, Thread.currentThread()); |
| 89 | checkpoint1.succeed(); |
| 90 | }); |
| 91 | } |
| 92 | } |
| 93 | MyVerticle verticle = new MyVerticle(); |
| 94 | vertx.deployVerticle(verticle); |
nothing calls this directly
no outgoing calls
no test coverage detected