Refine search
@Test public void testAsyncDeployFailure() throws Exception { long start = System.currentTimeMillis(); long delay = 1000; MyAsyncVerticle verticle = new MyAsyncVerticle(f -> vertx.setTimer(delay, id -> f.fail(new Exception("foobar"))), null); vertx.deployVerticle(verticle, ar -> { assertFalse(ar.succeeded()); assertEquals("foobar", ar.cause().getMessage()); long now = System.currentTimeMillis(); assertTrue(now - start >= delay); assertTrue(vertx.deploymentIDs().isEmpty()); testComplete(); }); await(); }
@Test public void testAsyncUndeployFailure() throws Exception { long delay = 1000; MyAsyncVerticle verticle = new MyAsyncVerticle(f-> f.complete(null), f -> vertx.setTimer(delay, id -> f.fail(new Exception("foobar")))); vertx.deployVerticle(verticle, ar -> { assertTrue(ar.succeeded()); long start = System.currentTimeMillis(); vertx.undeploy(ar.result(), ar2 -> { assertFalse(ar2.succeeded()); long now = System.currentTimeMillis(); assertTrue(now - start >= delay); assertFalse(vertx.deploymentIDs().contains(ar.result())); testComplete(); }); }); await(); }
@Test public void testAsyncUndeploy() throws Exception { long delay = 1000; MyAsyncVerticle verticle = new MyAsyncVerticle(f-> f.complete(null), f -> vertx.setTimer(delay, id -> f.complete(null))); vertx.deployVerticle(verticle, ar -> { assertTrue(ar.succeeded()); long start = System.currentTimeMillis(); vertx.undeploy(ar.result(), ar2 -> { assertTrue(ar2.succeeded()); long now = System.currentTimeMillis(); assertTrue(now - start >= delay); assertFalse(vertx.deploymentIDs().contains(ar.result())); testComplete(); }); vertx.setTimer(delay / 2, id -> assertFalse(vertx.deploymentIDs().isEmpty())); }); await(); }
@Test public void testAsyncDeploy() throws Exception { long start = System.currentTimeMillis(); long delay = 1000; MyAsyncVerticle verticle = new MyAsyncVerticle(f -> { vertx.setTimer(delay, id -> { f.complete(null); }); }, f -> f.complete(null)); vertx.deployVerticle(verticle, ar -> { assertTrue(ar.succeeded()); long now = System.currentTimeMillis(); assertTrue(now - start >= delay); assertTrue(vertx.deploymentIDs().contains(ar.result())); testComplete(); }); Thread.sleep(delay / 2); assertTrue(vertx.deploymentIDs().isEmpty()); 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 testRequestTimeoutCanceledWhenRequestHasAnOtherError() { AtomicReference<Throwable> exception = new AtomicReference<>(); // There is no server running, should fail to connect client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(exception::set)) .setTimeout(800) .end(); vertx.setTimer(1500, id -> { assertNotNull("Expected an exception to be set", exception.get()); assertFalse("Expected to not end with timeout exception, but did: " + exception.get(), exception.get() instanceof TimeoutException); testComplete(); }); await(); }
@Test public void testChangesNotVisibleObject3() { Map<String, Object> map = new HashMap<>(); final JsonObject obj = new JsonObject(map); eb.<JsonObject>consumer("foo").handler((Message<JsonObject> msg) -> { vertx.setTimer(1000, id -> { assertFalse(msg.body().containsKey("b")); testComplete(); }); }); eb.send("foo", obj); map.put("b", "uhqdihuqwd"); 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 testChangesNotVisibleArray3() { List<Object> list = new ArrayList<>(); final JsonArray obj = new JsonArray(list); eb.<JsonArray>consumer("foo").handler((Message<JsonArray> msg) -> { vertx.setTimer(1000, id -> { assertEquals(0, msg.body().size()); testComplete(); }); }); eb.send("foo", obj); list.add("uhwqdiuh"); 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 testExceptionCaught() throws Exception { vertx.createNetClient(new NetClientOptions().setSoLinger(0)).connect(8080, "localhost", onSuccess(socket -> { vertx.setTimer(2000, id -> { socket.close(); }); })); awaitLatch(resetLatch); assertThat(caught.get(), instanceOf(IOException.class)); }
@Test public void testRequestTimeoutCanceledWhenRequestEndsNormally() { server.requestHandler(req -> req.response().end()); server.listen(onSuccess(s -> { AtomicReference<Throwable> exception = new AtomicReference<>(); // There is no server running, should fail to connect HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler()); req.exceptionHandler(exception::set); req.setTimeout(500); req.end(); vertx.setTimer(1000, id -> { assertNull("Did not expect any exception", exception.get()); testComplete(); }); })); await(); }
@Test public void testChangesNotVisibleObject2() { final JsonObject obj = new JsonObject(); eb.<JsonObject>consumer("foo").handler((Message<JsonObject> msg) -> { msg.body().put("b", "uqwduihwqd"); }); eb.send("foo", obj); vertx.setTimer(1000, id -> { assertFalse(obj.containsKey("b")); testComplete(); }); await(); }
@Test public void testMessageConsumerCloseHookIsClosedCorrectly() { Vertx vertx = Vertx.vertx(); EventBus eb = vertx.eventBus(); vertx.deployVerticle(new AbstractVerticle() { MessageConsumer consumer; @Override public void start() throws Exception { context.exceptionHandler(err -> { fail("Unexpected exception"); }); consumer = eb.<String>consumer(ADDRESS1).handler(msg -> {}); } }, onSuccess(deploymentID -> { vertx.undeploy(deploymentID, onSuccess(v -> { vertx.setTimer(10, id -> { testComplete(); }); })); })); 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 testSendFileNotFound() throws Exception { server.requestHandler(req -> { req.response().putHeader("Content-Type", "wibble"); req.response().sendFile("nosuchfile.html"); }); server.listen(onSuccess(s -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> {})).end(); vertx.setTimer(100, tid -> testComplete()); })); await(); }
@Test public void testAcquire() { getVertx().sharedData().getLock("foo", ar -> { assertTrue(ar.succeeded()); long start = System.currentTimeMillis(); Lock lock = ar.result(); vertx.setTimer(1000, tid -> { lock.release(); }); getVertx().sharedData().getLock("foo", ar2 -> { assertTrue(ar2.succeeded()); // Should be delayed assertTrue(System.currentTimeMillis() - start >= 1000); testComplete(); }); }); await(); }