private void setEndTimer() { // Set another timer to trigger test complete - this is so if the first timer is called more than once we will // catch it vertx.setTimer(10, id -> testComplete()); }
@Test public void testTimerStreamExceptionDuringHandle() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.timerStream(200); AtomicBoolean handled = new AtomicBoolean(); timer.handler(l -> { assertFalse(handled.get()); handled.set(true); throw new RuntimeException(); }); timer.endHandler(v2 -> { assertTrue(handled.get()); testComplete(); }); }); await(); }
@Test public void testTimerStreamSetHandlerSchedulesTheTimer() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.timerStream(200); AtomicBoolean handled = new AtomicBoolean(); timer.handler(l -> { assertFalse(handled.get()); handled.set(true); }); timer.endHandler(v2 -> { assertTrue(handled.get()); testComplete(); }); }); await(); }
@Test /** * Test the timers fire with approximately the correct delay */ public void testTimings() throws Exception { final long start = System.currentTimeMillis(); final long delay = 2000; vertx.setTimer(delay, timerID -> { long dur = System.currentTimeMillis() - start; assertTrue(dur >= delay); long maxDelay = delay * 2; assertTrue("Timer accuracy: " + dur + " vs " + maxDelay, dur < maxDelay); // 100% margin of error (needed for CI) vertx.cancelTimer(timerID); testComplete(); }); await(); }
@Test public void testPeriodicPauseResume() throws Exception { ReadStream<Long> timer = vertx.periodicStream(200); AtomicInteger count = new AtomicInteger(); timer.handler(id -> { int cnt = count.incrementAndGet(); if (cnt == 2) { timer.pause(); vertx.setTimer(500, id2 -> { assertEquals(2, count.get()); timer.resume(); }); } else if (cnt == 3) { testComplete(); } }); await(); }
@Test public void testTimeoutStreamEndCallbackAsynchronously() { TimeoutStream stream = vertx.timerStream(200); ThreadLocal<Object> stack = new ThreadLocal<>(); stack.set(true); stream.endHandler(v2 -> { assertTrue(Vertx.currentContext().isEventLoopContext()); assertNull(stack.get()); testComplete(); }); stream.handler(id -> { }); await(); }
@Test public void testTimerStreamCancellation() throws Exception { vertx.runOnContext(v -> { TimeoutStream timer = vertx.timerStream(200); AtomicBoolean called = new AtomicBoolean(); timer.handler(l -> { called.set(true); }); timer.cancel(); vertx.setTimer(500, id -> { assertFalse(called.get()); testComplete(); }); }); await(); }
@Test public void testPeriodicStreamHandler() throws Exception { TimeoutStream timer = vertx.periodicStream(10); AtomicInteger count = new AtomicInteger(); timer.handler(l -> { int value = count.incrementAndGet(); switch (value) { case 0: break; case 1: throw new RuntimeException(); case 2: timer.cancel(); testComplete(); break; default: fail(); } }); timer.endHandler(v -> { fail(); }); await(); }
@Test public void testTimerStreamCallingWithNullHandlerCancelsTheTimer() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.timerStream(200); AtomicInteger count = new AtomicInteger(); timer.handler(l -> { if (count.incrementAndGet() == 1) { timer.handler(null); vertx.setTimer(200, id -> { assertEquals(1, count.get()); testComplete(); }); } else { fail(); } }); }); await(); }
@Test public void testTimerPauseResume() throws Exception { ReadStream<Long> timer = vertx.timerStream(10); timer.handler(l -> testComplete()); timer.pause(); timer.resume(); await(); }
@Test public void testTimerPause() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.timerStream(10); timer.handler(l -> fail()); timer.endHandler(l -> testComplete()); timer.pause(); }); await(); }
@Test public void testTimerSetHandlerTwice() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.timerStream(200); timer.handler(l -> testComplete()); try { timer.handler(l -> fail()); fail(); } catch (IllegalStateException ignore) { } }); await(); }
@Test public void testPeriodicSetHandlerTwice() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.periodicStream(200); timer.handler(l -> testComplete()); try { timer.handler(l -> fail()); fail(); } catch (IllegalStateException ignore) { } }); await(); }
private void setEndTimer() { // Set another timer to trigger test complete - this is so if the first timer is called more than once we will // catch it vertx.setTimer(10, id -> testComplete()); }
@Test public void testTimerStreamCancellation() throws Exception { vertx.runOnContext(v -> { TimeoutStream timer = vertx.timerStream(200); AtomicBoolean called = new AtomicBoolean(); timer.handler(l -> { called.set(true); }); timer.cancel(); vertx.setTimer(500, id -> { assertFalse(called.get()); testComplete(); }); }); await(); }
@Test public void testTimeoutStreamEndCallbackAsynchronously() { TimeoutStream stream = vertx.timerStream(200); ThreadLocal<Object> stack = new ThreadLocal<>(); stack.set(true); stream.endHandler(v2 -> { assertTrue(Vertx.currentContext().isEventLoopContext()); assertNull(stack.get()); testComplete(); }); stream.handler(id -> { }); await(); }
@Test public void testTimerPauseResume() throws Exception { ReadStream<Long> timer = vertx.timerStream(10); timer.handler(l -> testComplete()); timer.pause(); timer.resume(); await(); }
@Test public void testTimerPause() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.timerStream(10); timer.handler(l -> fail()); timer.endHandler(l -> testComplete()); timer.pause(); }); await(); }
@Test public void testTimerSetHandlerTwice() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.timerStream(200); timer.handler(l -> testComplete()); try { timer.handler(l -> fail()); fail(); } catch (IllegalStateException ignore) { } }); await(); }
@Test public void testPeriodicSetHandlerTwice() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.periodicStream(200); timer.handler(l -> testComplete()); try { timer.handler(l -> fail()); fail(); } catch (IllegalStateException ignore) { } }); await(); }