/** * Stop the server. * <p> * Doesn't wait for the close operation to finish */ @Override public void stop() { if (server != null) { server.close(); server = null; } }
@Override public void tearDown() throws Exception { if (httpServer != null) { CountDownLatch closeLatch = new CountDownLatch(1); httpServer.close(event -> closeLatch.countDown()); awaitLatch(closeLatch); } super.tearDown(); } }
protected void tearDown() throws Exception { if (client != null) { try { client.close(); } catch (IllegalStateException ignore) { // Client was already closed by the test } } if (server != null) { CountDownLatch latch = new CountDownLatch(1); server.close((asyncResult) -> { assertTrue(asyncResult.succeeded()); latch.countDown(); }); awaitLatch(latch); } super.tearDown(); }
@Test public void testListenInvalidPort() throws Exception { /* Port 7 is free for use by any application in Windows, so this test fails. */ Assume.assumeFalse(System.getProperty("os.name").startsWith("Windows")); server.close(); server = vertx.createHttpServer(new HttpServerOptions().setPort(7)); server.requestHandler(noOpHandler()).listen(onFailure(server -> testComplete())); await(); }
@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(); }
@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(); }
protected void tearDown() throws Exception { client.close(); if (server != null) { CountDownLatch latch = new CountDownLatch(1); server.close(ar -> { assertTrue(ar.succeeded()); latch.countDown(); }); awaitLatch(latch); } if (netServer != null) { CountDownLatch latch = new CountDownLatch(1); netServer.close(ar -> { assertTrue(ar.succeeded()); latch.countDown(); }); awaitLatch(latch); } super.tearDown(); }
@Test public void testListenInvalidHost() { server.close(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setHost("iqwjdoqiwjdoiqwdiojwd")); server.requestHandler(noOpHandler()); server.listen(onFailure(s -> testComplete())); }
@Test public void testClosingServerClosesRequestStreamEndHandler() { this.server = vertx.createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)); ReadStream<HttpServerRequest> stream = server.requestStream(); AtomicBoolean closed = new AtomicBoolean(); stream.endHandler(v -> closed.set(true)); stream.handler(req -> {}); server.listen(ar -> { assertTrue(ar.succeeded()); assertFalse(closed.get()); server.close(v -> { assertTrue(ar.succeeded()); assertTrue(closed.get()); testComplete(); }); }); await(); }
@Test public void testClosingServerClosesWebSocketStreamEndHandler() { this.server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)); ReadStream<ServerWebSocket> stream = server.websocketStream(); AtomicBoolean closed = new AtomicBoolean(); stream.endHandler(v -> closed.set(true)); stream.handler(ws -> { }); server.listen(ar -> { assertTrue(ar.succeeded()); assertFalse(closed.get()); server.close(v -> { assertTrue(ar.succeeded()); assertTrue(closed.get()); testComplete(); }); }); await(); }
@Test public void testServerWebsocketIdleTimeout() { server.close(); server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST)); server.websocketHandler(ws -> {}).listen(ar -> { assertTrue(ar.succeeded()); client.websocket(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", ws -> { ws.closeHandler(v -> testComplete()); }); }); 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 testServerLogging() throws Exception { server.close(); server = vertx.createHttpServer(createBaseServerOptions().setLogActivity(true)); TestLoggerFactory factory = testLogging(); if (this instanceof Http1xTest) { assertTrue(factory.hasName("io.netty.handler.logging.LoggingHandler")); } else { assertTrue(factory.hasName("io.netty.handler.codec.http2.Http2FrameLogger")); } }
@Test public void testServerOptionsCopiedBeforeUse() { server.close(); HttpServerOptions options = new HttpServerOptions().setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT); HttpServer server = vertx.createHttpServer(options); // Now change something - but server should still listen at previous port options.setPort(DEFAULT_HTTP_PORT + 1); server.requestHandler(req -> { req.response().end(); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/uri", onSuccess(res -> { assertEquals(200, res.statusCode()); testComplete(); })).end(); }); await(); }
@Test public void testAbsoluteURIServer() { server.close(); // Listen on all addresses server = vertx.createHttpServer(createBaseServerOptions().setHost("0.0.0.0")); server.requestHandler(req -> { String absURI = req.absoluteURI(); assertEquals(req.scheme() + "://localhost:8080/path", absURI); req.response().end(); }); server.listen(onSuccess(s -> { String host = "localhost"; String path = "/path"; int port = 8080; client.getNow(port, host, path, onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); })); })); await(); }
@Test public void testServerConnectionClosed() throws Exception { server.close(); server = vertx.createHttpServer(createBaseServerOptions().setIdleTimeout(2)); server.requestHandler(req -> { FakeHttpServerMetrics metrics = FakeMetricsBase.getMetrics(server); HttpServerMetric metric = metrics.getMetric(req); assertNotNull(metric); assertFalse(metric.failed.get()); req.response().closeHandler(v -> { assertNull(metrics.getMetric(req)); assertTrue(metric.failed.get()); testComplete(); }); }); startServer(); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {}); await(); } }
@Test public void testContentDecompression() throws Exception { server.close(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setDecompressionSupported(true)); String expected = TestUtils.randomAlphaString(1000); byte[] dataGzipped = TestUtils.compressGzip(expected); server.requestHandler(req -> { assertEquals("localhost:" + DEFAULT_HTTP_PORT, req.headers().get("host")); req.bodyHandler(buffer -> { assertEquals(expected, buffer.toString()); req.response().end(); }); }); server.listen(onSuccess(server -> { client .request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "some-uri", onSuccess(resp -> testComplete())) .putHeader("Content-Encoding", "gzip") .end(Buffer.buffer(dataGzipped)); })); await(); }
@Test public void testIdleTimeoutWithPartialH2CRequest() throws Exception { server.close(); server = vertx.createHttpServer(new HttpServerOptions() .setPort(DEFAULT_HTTP_PORT) .setHost(DEFAULT_HTTP_HOST) .setIdleTimeout(1)); server.requestHandler(req -> { testComplete(); }); startServer(); NetClient client = vertx.createNetClient(); client.connect(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(so -> { so.closeHandler(v -> { testComplete(); }); })); await(); }
@Test public void testServerDoesNotSupportAlpn() throws Exception { waitFor(2); server.close(); server = vertx.createHttpServer(createBaseServerOptions().setUseAlpn(false)); server.requestHandler(req -> { assertEquals(HttpVersion.HTTP_1_1, req.version()); req.response().end(); complete(); }); startServer(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(HttpVersion.HTTP_1_1, resp.version()); complete(); })).exceptionHandler(this::fail).end(); await(); }
@Test public void testInitialMaxConcurrentStreamZero() throws Exception { server.close(); server = vertx.createHttpServer(createBaseServerOptions().setInitialSettings(new Http2Settings().setMaxConcurrentStreams(0))); server.requestHandler(req -> { req.response().end(); }); server.connectionHandler(conn -> { vertx.setTimer(500, id -> { conn.updateSettings(new Http2Settings().setMaxConcurrentStreams(10)); }); }); startServer(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> { testComplete(); }).connectionHandler(conn -> { assertEquals(10, conn.remoteSettings().getMaxConcurrentStreams()); }).setTimeout(10000).exceptionHandler(this::fail).end(); await(); }