public void run() { CountDownLatch latch = new CountDownLatch(1); vertx.close(ar -> { if (!ar.succeeded()) { log.error("Failure in stopping Vert.x", ar.cause()); } latch.countDown(); }); try { if (!latch.await(2, TimeUnit.MINUTES)) { log.error("Timed out waiting to undeploy all"); } } catch (InterruptedException e) { throw new IllegalStateException(e); } } });
void getConnectionForRequest(ContextInternal ctx, String peerHost, boolean ssl, int port, String host, Handler<AsyncResult<HttpClientStream>> handler) { httpCM.getConnection(ctx, peerHost, ssl, port, host, ar -> { if (ar.succeeded()) { ar.result().createStream(handler); } else { handler.handle(Future.failedFuture(ar.cause())); } }); }
protected void closeClustered(List<Vertx> clustered) throws Exception { CountDownLatch latch = new CountDownLatch(clustered.size()); for (Vertx clusteredVertx : clustered) { clusteredVertx.close(ar -> { if (ar.failed()) { log.error("Failed to shutdown vert.x", ar.cause()); } latch.countDown(); }); } assertTrue(latch.await(180, TimeUnit.SECONDS)); }
@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()); }
private void closeVertx() throws Exception { if (running) { CountDownLatch latch = new CountDownLatch(1); vertx.close(ar -> { assertTrue(ar.succeeded()); latch.countDown(); }); assertTrue(latch.await(30, TimeUnit.SECONDS)); running = false; } }
@Test public void testSharedServersRoundRobinButFirstStartAndStopServer() throws Exception { // Start and stop a server on the same port/host before hand to make sure it doesn't interact CountDownLatch latch = new CountDownLatch(1); HttpServer theServer = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)); theServer.requestHandler(req -> { fail("Should not process request"); }).listen(onSuccess(s -> latch.countDown())); awaitLatch(latch); CountDownLatch closeLatch = new CountDownLatch(1); theServer.close(ar -> { assertTrue(ar.succeeded()); closeLatch.countDown(); }); assertTrue(closeLatch.await(10, TimeUnit.SECONDS)); testSharedServersRoundRobin(); }
@Test public void testQuorumLost() throws Exception { vertx1 = startVertx(3); vertx2 = startVertx(3); vertx3 = startVertx(3); DeploymentOptions options = new DeploymentOptions().setHa(true); JsonObject config = new JsonObject().put("foo", "bar"); options.setConfig(config); vertx1.deployVerticle("java:" + HAVerticle1.class.getName(), options, ar -> { assertTrue(ar.succeeded()); assertTrue(vertx1.deploymentIDs().contains(ar.result())); ; }); vertx2.deployVerticle("java:" + HAVerticle2.class.getName(), options, ar -> { assertTrue(ar.succeeded()); assertTrue(vertx2.deploymentIDs().contains(ar.result())); ; }); assertWaitUntil(() -> vertx1.deploymentIDs().size() == 1 && vertx2.deploymentIDs().size() == 1); // Now close vertx3 - quorum should then be lost and verticles undeployed CountDownLatch latch = new CountDownLatch(1); vertx3.close(ar -> { latch.countDown(); }); awaitLatch(latch); assertWaitUntil(() -> vertx1.deploymentIDs().isEmpty() && vertx2.deploymentIDs().isEmpty()); // Now re-instate the quorum vertx4 = startVertx(3); assertWaitUntil(() -> vertx1.deploymentIDs().size() == 1 && vertx2.deploymentIDs().size() == 1); }
private void testIsolationGroup(String group1, String group2, int count1, int count2, List<String> isolatedClasses, String verticleID) throws Exception { Map<String, Integer> countMap = new ConcurrentHashMap<>(); vertx.eventBus().<JsonObject>consumer("testcounts").handler((Message<JsonObject> msg) -> { countMap.put(msg.body().getString("deploymentID"), msg.body().getInteger("count")); }); CountDownLatch latch = new CountDownLatch(1); AtomicReference<String> deploymentID1 = new AtomicReference<>(); AtomicReference<String> deploymentID2 = new AtomicReference<>(); boolean expectedSuccess = Thread.currentThread().getContextClassLoader() instanceof URLClassLoader; try { vertx.deployVerticle(verticleID, new DeploymentOptions(). setIsolationGroup(group1).setIsolatedClasses(isolatedClasses), ar -> { assertTrue(ar.succeeded()); deploymentID1.set(ar.result()); assertEquals(0, TestVerticle.instanceCount.get()); vertx.deployVerticle(verticleID, new DeploymentOptions().setIsolationGroup(group2).setIsolatedClasses(isolatedClasses), ar2 -> { assertTrue(ar2.succeeded()); deploymentID2.set(ar2.result()); assertEquals(0, TestVerticle.instanceCount.get()); latch.countDown(); }); });
@Test public void testCleanCloseNoFailover() throws Exception { vertx1 = startVertx(); vertx2 = startVertx(); DeploymentOptions options = new DeploymentOptions().setHa(true); JsonObject config = new JsonObject().put("foo", "bar"); options.setConfig(config); CountDownLatch deployLatch = new CountDownLatch(1); vertx2.deployVerticle("java:" + HAVerticle1.class.getName(), options, ar -> { assertTrue(ar.succeeded()); deployLatch.countDown(); }); awaitLatch(deployLatch); ((VertxInternal)vertx1).failoverCompleteHandler((nodeID, haInfo, succeeded) -> { fail("Should not be called"); }); vertx2.close(ar -> { vertx.setTimer(500, tid -> { // Wait a bit in case failover happens testComplete(); }); }); await(); }
@Test public void testSimpleFailover() throws Exception { startNodes(2, new VertxOptions().setHAEnabled(true)); DeploymentOptions options = new DeploymentOptions().setHa(true); JsonObject config = new JsonObject().put("foo", "bar"); options.setConfig(config); CountDownLatch latch = new CountDownLatch(1); vertices[0].deployVerticle("java:" + HAVerticle1.class.getName(), options, ar -> { assertTrue(ar.succeeded()); assertEquals(1, vertices[0].deploymentIDs().size()); assertEquals(0, vertices[1].deploymentIDs().size()); latch.countDown(); }); awaitLatch(latch); kill(0); assertWaitUntil(() -> vertices[1].deploymentIDs().size() == 1); checkDeploymentExists(1, "java:" + HAVerticle1.class.getName(), options); }
@Test public void testExecuteBlockingTTCL() throws Exception { ClassLoader cl = Thread.currentThread().getContextClassLoader(); assertNotNull(cl); CountDownLatch latch = new CountDownLatch(1); AtomicReference<ClassLoader> blockingTCCL = new AtomicReference<>(); vertx.<String>executeBlocking(future -> { future.complete("whatever"); blockingTCCL.set(Thread.currentThread().getContextClassLoader()); }, ar -> { assertTrue(ar.succeeded()); assertEquals("whatever", ar.result()); latch.countDown(); }); assertSame(cl, Thread.currentThread().getContextClassLoader()); awaitLatch(latch); assertSame(cl, blockingTCCL.get()); }
@Test public void testDeployWhenClosedShouldFail() throws Exception { CountDownLatch closed = new CountDownLatch(1); vertx.close(ar -> { assertTrue(ar.succeeded()); closed.countDown(); }); awaitLatch(closed); vertx.deployVerticle(new AbstractVerticle() { }, ar -> { assertFalse(ar.succeeded()); assertEquals("Vert.x closed", ar.cause().getMessage()); testComplete(); }); await(); }
@Test public void testGetInstanceCountMultipleVerticles() throws Exception { AtomicInteger messageCount = new AtomicInteger(0); AtomicInteger totalReportedInstances = new AtomicInteger(0); vertx.eventBus().consumer("instanceCount", event -> { messageCount.incrementAndGet(); totalReportedInstances.addAndGet((int)event.body()); if(messageCount.intValue() == 3) { assertEquals(9, totalReportedInstances.get()); testComplete(); } }); vertx.deployVerticle(TestVerticle3.class.getCanonicalName(), new DeploymentOptions().setInstances(3), ar -> { assertTrue(ar.succeeded()); }); await(); Deployment deployment = ((VertxInternal) vertx).getDeployment(vertx.deploymentIDs().iterator().next()); CountDownLatch latch = new CountDownLatch(1); vertx.undeploy(deployment.deploymentID(), ar -> latch.countDown()); awaitLatch(latch); }
@Test public void testSharedServersRoundRobinWithOtherServerRunningOnDifferentPort() throws Exception { // Have a server running on a different port to make sure it doesn't interact CountDownLatch latch = new CountDownLatch(1); HttpServer theServer = vertx.createHttpServer(new HttpServerOptions().setPort(4321)); theServer.websocketHandler(ws -> { fail("Should not connect"); }).listen(ar -> { if (ar.succeeded()) { latch.countDown(); } else { fail("Failed to bind server"); } }); awaitLatch(latch); testSharedServersRoundRobin(); }
@Test public void testCloseCallsEndHandlerWithRegistrationContext() throws Exception { Context ctx = vertx.getOrCreateContext(); CountDownLatch registered = new CountDownLatch(1); ctx.runOnContext(v1 -> { MessageConsumer<String> consumer = eb.consumer(ADDRESS1); consumer.endHandler(v2 -> { assertSame(Vertx.currentContext(), ctx); testComplete(); }); consumer.handler(msg -> {}); consumer.completionHandler(ar -> { assertTrue(ar.succeeded()); registered.countDown(); }); }); awaitLatch(registered); closeVertx(); await(); }
@Test public void testSharedServersRoundRobinWithOtherServerRunningOnDifferentPort() throws Exception { CountDownLatch latch = new CountDownLatch(1); // Have a server running on a different port to make sure it doesn't interact server.close(); server = vertx.createNetServer(new NetServerOptions().setPort(4321)); server.connectHandler(sock -> { fail("Should not connect"); }).listen(ar2 -> { if (ar2.succeeded()) { latch.countDown(); } else { fail("Failed to bind server"); } }); awaitLatch(latch); testSharedServersRoundRobin(); }