Source:
public void testGracefulShutdown() throws Exception { CountDownLatch requestReceived = new CountDownLatch(1); CountDownLatch shutdownInvoked = new CountDownLatch(1); environment.jersey().register(new AbstractBinder() { @Override protected void configure() { bind(new TestResource(requestReceived, shutdownInvoked)).to(TestResource.class); } }); environment.jersey().register(TestResource.class); final ScheduledExecutorService executor = Executors.newScheduledThreadPool(3); http.configure(environment); final Server server = http.build(environment); ((AbstractNetworkConnector) server.getConnectors()[0]).setPort(0); ((AbstractNetworkConnector) server.getConnectors()[1]).setPort(0); ScheduledFuture<Void> cleanup = executor.schedule(() -> { if (!server.isStopped()) { server.stop(); } executor.shutdownNow(); return null; }, 5, TimeUnit.SECONDS); server.start(); final int port = ((AbstractNetworkConnector) server.getConnectors()[0]).getLocalPort(); Future<String> futureResult = executor.submit(() -> { URL url = new URL("http://localhost:" + port + "/app/test"); URLConnection connection = url.openConnection(); connection.connect(); return CharStreams.toString(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); }); requestReceived.await(10, TimeUnit.SECONDS); Future<Void> serverStopped = executor.submit(() -> { server.stop(); return null; }); Connector[] connectors = server.getConnectors(); assertThat(connectors).isNotEmpty(); assertThat(connectors[0]).isInstanceOf(NetworkConnector.class); NetworkConnector connector = (NetworkConnector) connectors[0]; while (true) { if (!connector.isOpen()) { shutdownInvoked.countDown(); break; } Thread.sleep(5); } String result = futureResult.get(); assertThat(result).isEqualTo("test"); serverStopped.get(); cleanup.cancel(false); executor.shutdownNow(); }

Target:
public void testGracefulShutdown() throws Exception { CountDownLatch requestReceived = new CountDownLatch(1); CountDownLatch shutdownInvoked = new CountDownLatch(1); environment.jersey().register(new TestResource(requestReceived, shutdownInvoked)); final ScheduledExecutorService executor = Executors.newScheduledThreadPool(3); http.configure(environment); final Server server = http.build(environment); ((AbstractNetworkConnector) server.getConnectors()[0]).setPort(0); ((AbstractNetworkConnector) server.getConnectors()[1]).setPort(0); ScheduledFuture<Void> cleanup = executor.schedule(() -> { if (!server.isStopped()) { server.stop(); } executor.shutdownNow(); return null; }, 5, TimeUnit.SECONDS); server.start(); final int port = ((AbstractNetworkConnector) server.getConnectors()[0]).getLocalPort(); Future<String> futureResult = executor.submit(() -> { URL url = new URL("http://localhost:" + port + "/app/test"); URLConnection connection = url.openConnection(); connection.connect(); return CharStreams.toString(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); }); requestReceived.await(10, TimeUnit.SECONDS); Future<Void> serverStopped = executor.submit(() -> { server.stop(); return null; }); Connector[] connectors = server.getConnectors(); assertThat(connectors).isNotEmpty(); assertThat(connectors[0]).isInstanceOf(NetworkConnector.class); NetworkConnector connector = (NetworkConnector) connectors[0]; while (true) { if (!connector.isOpen()) { shutdownInvoked.countDown(); break; } Thread.sleep(5); } String result = futureResult.get(); assertThat(result).isEqualTo("test"); serverStopped.get(); cleanup.cancel(false); executor.shutdownNow(); }