@Override public void start(Future<Void> startFuture) throws Exception { HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestHandler(req -> req.response().end()); server.listen(res -> { if (res.succeeded()) { startFuture.complete(); } else { startFuture.fail(res.cause()); } }); } }
@Override public void start(Future<Void> startFuture) throws Exception { vertx.deployVerticle("java:" + ChildVerticle.class.getName(), ar -> { if (ar.succeeded()) { startFuture.complete(null); } else { ar.cause().printStackTrace(); } }); } }
@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); // Send a message every second vertx.setPeriodic(1000, v -> eb.publish("news-feed", "Some news!")); } }
@Override public void start() throws Exception { vertx.setPeriodic(3000, x -> { System.out.println("Sending data to 'news'"); vertx.eventBus().send("news", "hello vert.x"); }); }
@Override public void put(K k, V v, long timeout, Handler<AsyncResult<Void>> completionHandler) { long timestamp = System.nanoTime(); long timerId = vertx.setTimer(timeout, l -> removeIfExpired(k)); Holder<V> previous = map.put(k, new Holder<>(v, timerId, timeout, timestamp)); if (previous != null && previous.expires()) { vertx.cancelTimer(previous.timerId); } completionHandler.handle(Future.succeededFuture()); }
@Test public void testAsyncUndeployFailureCalledSynchronously() throws Exception { MyAsyncVerticle verticle = new MyAsyncVerticle(f -> f.complete(null), f -> f.fail(new Exception("foobar"))); vertx.deployVerticle(verticle, ar -> { assertTrue(ar.succeeded()); vertx.undeploy(ar.result(), ar2 -> { assertFalse(ar2.succeeded()); assertEquals("foobar", ar2.cause().getMessage()); assertFalse(vertx.deploymentIDs().contains(ar.result())); testComplete(); }); }); await(); }
private void testServerWebsocketPingPongCheck(int maxFrameSize, Consumer<ServerWebSocket> check) { server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebsocketFrameSize(maxFrameSize)); server.websocketHandler(ws -> { ws.pongHandler(buff -> fail()); check.accept(ws); }).listen(onSuccess(v -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> {}); })); await(); }
@Test public void testSendFileFromClasspath() { vertx.createHttpServer(new HttpServerOptions().setPort(8080)).requestHandler(res -> { res.response().sendFile(webRoot + "/somefile.html"); }).listen(onSuccess(res -> { vertx.createHttpClient(new HttpClientOptions()).request(HttpMethod.GET, 8080, "localhost", "/", onSuccess(resp -> { resp.bodyHandler(buff -> { assertTrue(buff.toString().startsWith("<html><body>blah</body></html>")); testComplete(); }); })).end(); })); await(); }
@Test public void testUnknownContentLengthIsSetToZeroWithHTTP_1_0() throws Exception { server.requestHandler(req -> { req.response().write("Some-String").end(); }); startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_1_0)); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertNull(resp.getHeader("Content-Length")); testComplete(); })); await(); }
@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(); }
FakeWaiter() { context = (ContextInternal) vertx.getOrCreateContext(); handler = ar -> { if (ar.succeeded()) { handleConnection(ar.result()); } else { handleFailure(ar.cause()); } }; }
@Test public void testAsyncUndeployCalledSynchronously() throws Exception { MyAsyncVerticle verticle = new MyAsyncVerticle(f -> f.complete(null), f -> f.complete(null)); vertx.deployVerticle(verticle, ar -> { assertTrue(ar.succeeded()); vertx.undeploy(ar.result(), ar2 -> { assertTrue(ar2.succeeded()); assertFalse(vertx.deploymentIDs().contains(ar.result())); testComplete(); }); }); await(); }
private void drainingServer(Consumer<Future<Void>> consumer) { Future<Void> resumeFuture = Future.future(); server.requestHandler(req -> { req.response().setChunked(true); assertFalse(req.response().writeQueueFull()); req.response().setWriteQueueMaxSize(1000); Buffer buff = TestUtils.randomBuffer(10000); //Send data until the buffer is full vertx.setPeriodic(1, id -> { req.response().write(buff); if (req.response().writeQueueFull()) { vertx.cancelTimer(id); req.response().drainHandler(v -> { assertFalse(req.response().writeQueueFull()); testComplete(); }); // Tell the client to resume resumeFuture.complete(); } }); }); server.listen(onSuccess(s -> consumer.accept(resumeFuture))); }
@Test public void testAsyncDeployFailureCalledSynchronously() throws Exception { MyAsyncVerticle verticle = new MyAsyncVerticle(f -> f.fail(new Exception("foobar")), null); vertx.deployVerticle(verticle, ar -> { assertFalse(ar.succeeded()); assertEquals("foobar", ar.cause().getMessage()); testComplete(); }); await(); }
@Test public void testRegisterRemote2() { startNodes(2); String str = TestUtils.randomUnicodeString(100); vertices[0].eventBus().consumer(ADDRESS1, (Message<String> msg) -> { assertEquals(str, msg.body()); testComplete(); }).completionHandler(ar -> { assertTrue(ar.succeeded()); vertices[1].eventBus().send(ADDRESS1, str); }); await(); }
@Test public void testRequestTimeoutExtendedWhenResponseChunksReceived() { long timeout = 2000; int numChunks = 100; AtomicInteger count = new AtomicInteger(0); long interval = timeout * 2 / numChunks; server.requestHandler(req -> { req.response().setChunked(true); vertx.setPeriodic(interval, timerID -> { req.response().write("foo"); if (count.incrementAndGet() == numChunks) { req.response().end(); vertx.cancelTimer(timerID); } }); }); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(200, resp.statusCode()); resp.endHandler(v -> testComplete()); })); req.exceptionHandler(t -> fail("Should not be called")); req.setTimeout(timeout); req.end(); })); await(); }
@Test public void testFallbackOnHttp1() throws Exception { server.close(); server = vertx.createHttpServer(serverOptions.setUseAlpn(false)); server.requestHandler(req -> { assertEquals(HttpVersion.HTTP_1_1, req.version()); req.response().end(); }); startServer(); client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> { testComplete(); }).exceptionHandler(this::fail).end(); await(); }
@Test public void testExecuteOrderedBlocking() throws Exception { Context context = vertx.getOrCreateContext(); context.executeBlocking(f -> { assertTrue(Context.isOnWorkerThread()); f.complete(1 + 2); }, r -> { assertTrue(Context.isOnEventLoopThread()); assertEquals(r.result(), 3); testComplete(); }); await(); }
@Test public void testDeployUsingClassName() throws Exception { vertx.deployVerticle("java:" + TestVerticle.class.getCanonicalName(), ar -> { assertTrue(ar.succeeded()); testComplete(); }); await(); }