@SuppressWarnings("unchecked") @Override public void handle(AsyncResult event) { if (event.failed()) { handler.handle(event); } else { List<String> txts = new ArrayList<>(); List<List<String>> records = (List<List<String>>) event.result(); for (List<String> txt: records) { txts.addAll(txt); } handler.handle(Future.succeededFuture(txts)); } } }, DnsRecordType.TXT);
@Test public void testRaceConditionWithWebsocketClientWorker() throws Exception { CompletableFuture<Context> fut = new CompletableFuture<>(); vertx.deployVerticle(new AbstractVerticle() { @Override public void start() throws Exception { fut.complete(context); } }, new DeploymentOptions().setWorker(true), ar -> { if (ar.failed()) { fut.completeExceptionally(ar.cause()); } }); testRaceConditionWithWebsocketClient(fut.get()); }
@Test public void testReadFile() throws Exception { byte[] content = TestUtils.randomByteArray(1000); String fileName = "some-file.dat"; createFile(fileName, content); vertx.fileSystem().readFile(testDir + pathSep + fileName, ar -> { if (ar.failed()) { fail(ar.cause().getMessage()); } else { assertEquals(Buffer.buffer(content), ar.result()); testComplete(); } }); await(); }
@Test public void testReplyFailureNoHandlers() throws Exception { CountDownLatch latch = new CountDownLatch(1); EventBus eb = vertx.eventBus(); eb.send(ADDRESS1, "bar", new DeliveryOptions().setSendTimeout(10), ar -> { assertTrue(ar.failed()); latch.countDown(); }); awaitLatch(latch); FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(eb); assertEquals(Collections.singletonList(ADDRESS1), metrics.getReplyFailureAddresses()); assertEquals(Collections.singletonList(ReplyFailure.NO_HANDLERS), metrics.getReplyFailures()); }
@Test public void testMultipleFailedDeploys() throws InterruptedException { int instances = 10; DeploymentOptions options = new DeploymentOptions(); options.setInstances(instances); AtomicBoolean called = new AtomicBoolean(false); vertx.deployVerticle(() -> { Verticle v = new AbstractVerticle() { @Override public void start(final Future<Void> startFuture) throws Exception { startFuture.fail("Fail to deploy."); } }; return v; }, options, asyncResult -> { assertTrue(asyncResult.failed()); assertNull(asyncResult.result()); if (!called.compareAndSet(false, true)) { fail("Completion handler called more than once"); } vertx.setTimer(30, id -> { testComplete(); }); }); await(); }
@Test public void testFailedDeployRunsContextShutdownHook() throws Exception { AtomicBoolean closeHookCalledBeforeDeployFailure = new AtomicBoolean(false); Closeable closeable = completionHandler -> { closeHookCalledBeforeDeployFailure.set(true); completionHandler.handle(Future.succeededFuture()); }; Verticle v = new AbstractVerticle() { @Override public void start(Future<Void> startFuture) throws Exception { this.context.addCloseHook(closeable); startFuture.fail("Fail to deploy."); } }; vertx.deployVerticle(v, asyncResult -> { assertTrue(closeHookCalledBeforeDeployFailure.get()); assertTrue(asyncResult.failed()); assertNull(asyncResult.result()); testComplete(); }); await(); }
private void handleListProducts(RoutingContext routingContext) { HttpServerResponse response = routingContext.response(); SQLConnection conn = routingContext.get("conn"); conn.query("SELECT id, name, price, weight FROM products", query -> { if (query.failed()) { sendError(500, response); } else { JsonArray arr = new JsonArray(); query.result().getRows().forEach(arr::add); routingContext.response().putHeader("content-type", "application/json").end(arr.encode()); } }); }
public synchronized void setResult(AsyncResult<Void> result) { this.result = result; if (completionHandler != null) { if (metrics != null && result.succeeded()) { metric = metrics.handlerRegistered(address, repliedAddress); } Handler<AsyncResult<Void>> callback = completionHandler; vertx.runOnContext(v -> callback.handle(result)); } else if (result.failed()) { log.error("Failed to propagate registration for handler " + handler + " and address " + address); } else if (metrics != null) { metric = metrics.handlerRegistered(address, repliedAddress); } }
@Test public void testCallbackInvokedOnFailure() throws Exception { // will trigger java.net.UnknownHostException String hostName = "zoom.zoom.zen.tld"; VertxOptions options = new VertxOptions() .setClusterManager(new FakeClusterManager()) .setClusterHost(hostName); AtomicReference<AsyncResult<Vertx>> resultRef = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); Vertx.clusteredVertx(options, ar -> { resultRef.set(ar); latch.countDown(); }); awaitLatch(latch); assertFalse(resultRef.get() == null); assertTrue(resultRef.get().failed()); assertTrue("Was expecting failure to be an instance of UnknownHostException", resultRef.get().cause() instanceof UnknownHostException); } }
@Test public void testCallSetHandlerAfterCompletion() { AtomicBoolean called = new AtomicBoolean(); future.setHandler(result -> { assertTrue(result.succeeded()); assertFalse(result.failed()); assertEquals(null, result.result()); assertEquals(null, result.cause()); called.set(true); }); future.setHandler(result -> { assertTrue(result.succeeded()); assertFalse(result.failed()); assertEquals(foo, result.result()); assertEquals(null, result.cause()); called.set(true); }); future.setHandler(result -> { assertFalse(result.succeeded()); assertTrue(result.failed()); assertEquals(null, result.result()); assertEquals(cause, result.cause()); called.set(true); });
@Test public void testSendFileFailsWhenClientClosesConnection() throws Exception { // 10 megs final File f = setupFile("file.pdf", TestUtils.randomUnicodeString(10 * 1024 * 1024)); server.requestHandler(req -> { try { req.response().sendFile(f.getAbsolutePath(), ar -> { if (ar.failed()) { // Broken pipe testComplete(); } else { fail(new Exception("It should not reach this point")); } }); } catch (Exception e) { // this was the bug reported with issues/issue-80 fail(e); } }); startServer(); vertx.createNetClient().connect(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, socket -> { socket.result().write("GET / HTTP/1.1\r\n\r\n").close(); }); await(); }
private void setClusterViewChangedHandler(HAManager haManager) { haManager.setClusterViewChangedHandler(members -> { ownSubs.forEach(address -> { subs.add(address, nodeInfo, addResult -> { if (addResult.failed()) { log.warn("Failed to update subs map with self", addResult.cause()); } }); }); subs.removeAllMatching((Serializable & Predicate<ClusterNodeInfo>) ci -> !members.contains(ci.nodeId), removeResult -> { if (removeResult.failed()) { log.warn("Error removing subs", removeResult.cause()); } }); }); }