void close() { if (timeoutID != -1) { vertx.cancelTimer(timeoutID); } if (pingTimeoutID != -1) { vertx.cancelTimer(pingTimeoutID); } try { client.close(); } catch (Exception ignore) { } // The holder can be null or different if the target server is restarted with same serverid // before the cleanup for the previous one has been processed if (eventBus.connections().remove(serverID, this)) { if (log.isDebugEnabled()) { log.debug("Cluster connection closed for server " + serverID); } } }
protected void tearDown() throws Exception { if (tmp != null) { tmp.delete(); } if (client != null) { client.close(); } if (server != null) { awaitClose(server); } if (proxy != null) { proxy.stop(); } super.tearDown(); }
@Test public void testAttemptConnectAfterClose() { client.close(); try { client.connect(testAddress, ar -> { }); fail("Should throw exception"); } catch (IllegalStateException e) { //OK } }
@Override protected void tearDown() throws Exception { if (netClient != null) { netClient.close(); } if (client != null) { client.close(); } if (server != null) { CountDownLatch latch = new CountDownLatch(1); server.close((asyncResult) -> { assertTrue(asyncResult.succeeded()); latch.countDown(); }); awaitLatch(latch); } super.tearDown(); }
return result.get(20, TimeUnit.SECONDS); } finally { client.close();
client.close(); client = vertx.createNetClient(new NetClientOptions()); CountDownLatch latchClient = new CountDownLatch(numConnections);
@Test public void testReconnectAttemptsNotEnough() { client.close(); client = vertx.createNetClient(new NetClientOptions().setReconnectAttempts(100).setReconnectInterval(10)); client.connect(testAddress, (res) -> { assertFalse(res.succeeded()); assertTrue(res.failed()); testComplete(); }); await(); }
@Test public void testListenSocketAddress() { NetClient netClient = vertx.createNetClient(); server = vertx.createHttpServer().requestHandler(req -> req.response().end()); SocketAddress sockAddress = SocketAddress.inetSocketAddress(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST); server.listen(sockAddress, onSuccess(server -> { netClient.connect(sockAddress, onSuccess(sock -> { sock.handler(buf -> { assertTrue("Response is not an http 200", buf.toString("UTF-8").startsWith("HTTP/1.1 200 OK")); testComplete(); }); sock.write("GET / HTTP/1.1\r\n\r\n"); })); })); try { await(); } finally { netClient.close(); } }
private void testInVerticle(boolean worker) throws Exception { client.close(); server.close(); class MyVerticle extends AbstractVerticle {
client.close(); testComplete(); });
@Test public void testClientLogging() throws Exception { client.close(); client = vertx.createNetClient(new NetClientOptions().setLogActivity(true)); TestLoggerFactory factory = testLogging(); assertTrue(factory.hasName("io.netty.handler.logging.LoggingHandler")); }
@Test public void testClientIdleTimeout() { client.close(); NetClientOptions netClientOptions = new NetClientOptions(); netClientOptions.setIdleTimeout(1000); netClientOptions.setIdleTimeoutUnit(TimeUnit.MILLISECONDS); client = vertx.createNetClient(netClientOptions); server.connectHandler(s -> { }).listen(testAddress, ar -> { assertTrue(ar.succeeded()); client.connect(testAddress, res -> { assertTrue(res.succeeded()); NetSocket socket = res.result(); socket.closeHandler(v -> testComplete()); }); }); await(); }
@Test public void testClientOptionsCopiedBeforeUse() { client.close(); NetClientOptions options = new NetClientOptions(); client = vertx.createNetClient(options); options.setSsl(true); // Now change something - but server should ignore this server.connectHandler(sock -> { testComplete(); }); server.listen(1234, "localhost", ar -> { assertTrue(ar.succeeded()); client.connect(1234, "localhost", ar2 -> { assertTrue(ar2.succeeded()); }); }); await(); }
@Test public void testTooLongContentInHttpServerRequest() throws Exception { server.requestHandler(req -> { req.response().end(); }); server.connectionHandler(conn -> { conn.exceptionHandler(error -> { assertEquals(IllegalArgumentException.class, error.getClass()); testComplete(); }); }); startServer(); NetClient client = vertx.createNetClient(); try { client.connect(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(so -> { so.write("POST / HTTP/1.1\r\nContent-Length: 4\r\n\r\ntoolong\r\n"); })); await(); } finally { client.close(); } } @Test
private void testNet(String hostname) throws Exception { NetClient client = vertx.createNetClient(); NetServer server = vertx.createNetServer().connectHandler(so -> { so.handler(buff -> { so.write(buff); so.close(); }); }); try { CountDownLatch listenLatch = new CountDownLatch(1); server.listen(1234, hostname, onSuccess(s -> { listenLatch.countDown(); })); awaitLatch(listenLatch); client.connect(1234, hostname, onSuccess(so -> { Buffer buffer = Buffer.buffer(); so.handler(buffer::appendBuffer); so.closeHandler(v -> { assertEquals(Buffer.buffer("foo"), buffer); testComplete(); }); so.write(Buffer.buffer("foo")); })); await(); } finally { client.close(); server.close(); } }
private void reconnectAttempts(int attempts) { client.close(); client = vertx.createNetClient(new NetClientOptions().setReconnectAttempts(attempts).setReconnectInterval(10)); //The server delays starting for a a few seconds, but it should still connect client.connect(testAddress, onSuccess(so -> testComplete())); // Start the server after a delay vertx.setTimer(2000, id -> startEchoServer(testAddress, s -> {})); await(); }
@Test public void testNetClientInternalTLS() throws Exception { client.close(); client = vertx.createNetClient(new NetClientOptions().setSsl(true).setTrustStoreOptions(Trust.SERVER_JKS.get())); testNetClientInternal_(new HttpServerOptions() .setHost("localhost") .setPort(1234) .setSsl(true) .setKeyStoreOptions(Cert.SERVER_JKS.get()), true); }
.connectHandler((idleOnServer ? sender : receiver)::accept); startServer(); client.close(); client = vertx.createNetClient(new NetClientOptions().setIdleTimeout(200).setIdleTimeoutUnit(TimeUnit.MILLISECONDS)); client.connect(testAddress, onSuccess(idleOnServer ? receiver : sender));
@Test public void testClientLocalAddress() { String expectedAddress = TestUtils.loopbackAddress(); NetClientOptions clientOptions = new NetClientOptions().setLocalAddress(expectedAddress); client.close(); client = vertx.createNetClient(clientOptions); server.connectHandler(sock -> { assertEquals(expectedAddress, sock.remoteAddress().host()); sock.close(); }); server.listen(1234, "localhost", onSuccess(v -> { client.connect(1234, "localhost", onSuccess(socket -> { socket.closeHandler(v2 -> { testComplete(); }); })); })); await(); }