use of cn.taketoday.framework.web.server.GracefulShutdownResult in project today-infrastructure by TAKETODAY.
the class AbstractReactiveWebServerFactoryTests method givenAnInflightRequestWhenTheServerIsStoppedThenGracefulShutdownCallbackIsCalledWithRequestsActive.
@Test
void givenAnInflightRequestWhenTheServerIsStoppedThenGracefulShutdownCallbackIsCalledWithRequestsActive() throws Exception {
AbstractReactiveWebServerFactory factory = getFactory();
factory.setShutdown(Shutdown.GRACEFUL);
BlockingHandler blockingHandler = new BlockingHandler();
this.webServer = factory.getWebServer(blockingHandler);
this.webServer.start();
Mono<ResponseEntity<Void>> request = getWebClient(this.webServer.getPort()).build().get().retrieve().toBodilessEntity();
AtomicReference<ResponseEntity<Void>> responseReference = new AtomicReference<>();
CountDownLatch responseLatch = new CountDownLatch(1);
request.subscribe((response) -> {
responseReference.set(response);
responseLatch.countDown();
});
blockingHandler.awaitQueue();
AtomicReference<GracefulShutdownResult> result = new AtomicReference<>();
this.webServer.shutDownGracefully(result::set);
assertThat(responseReference.get()).isNull();
try {
this.webServer.stop();
} catch (Exception ex) {
// Continue
}
System.out.println("Stopped");
Awaitility.await().atMost(Duration.ofSeconds(5)).until(() -> GracefulShutdownResult.REQUESTS_ACTIVE == result.get());
blockingHandler.completeOne();
}
use of cn.taketoday.framework.web.server.GracefulShutdownResult in project today-infrastructure by TAKETODAY.
the class AbstractReactiveWebServerFactoryTests method whenARequestRemainsInFlightThenShutDownGracefullyDoesNotInvokeCallbackUntilTheRequestCompletes.
@Test
void whenARequestRemainsInFlightThenShutDownGracefullyDoesNotInvokeCallbackUntilTheRequestCompletes() throws Exception {
AbstractReactiveWebServerFactory factory = getFactory();
factory.setShutdown(Shutdown.GRACEFUL);
BlockingHandler blockingHandler = new BlockingHandler();
this.webServer = factory.getWebServer(blockingHandler);
this.webServer.start();
Mono<ResponseEntity<Void>> request = getWebClient(this.webServer.getPort()).build().get().retrieve().toBodilessEntity();
AtomicReference<ResponseEntity<Void>> responseReference = new AtomicReference<>();
CountDownLatch responseLatch = new CountDownLatch(1);
request.subscribe((response) -> {
responseReference.set(response);
responseLatch.countDown();
});
blockingHandler.awaitQueue();
AtomicReference<GracefulShutdownResult> result = new AtomicReference<>();
this.webServer.shutDownGracefully(result::set);
assertThat(responseReference.get()).isNull();
blockingHandler.completeOne();
assertThat(responseLatch.await(5, TimeUnit.SECONDS)).isTrue();
Awaitility.await().atMost(Duration.ofSeconds(30)).until(() -> GracefulShutdownResult.IDLE == result.get());
}
use of cn.taketoday.framework.web.server.GracefulShutdownResult in project today-infrastructure by TAKETODAY.
the class UndertowServletWebServerFactoryTests method whenServerIsShuttingDownGracefullyThenRequestsAreRejectedWithServiceUnavailable.
@Test
void whenServerIsShuttingDownGracefullyThenRequestsAreRejectedWithServiceUnavailable() throws Exception {
AbstractServletWebServerFactory factory = getFactory();
factory.setShutdown(Shutdown.GRACEFUL);
BlockingServlet blockingServlet = new BlockingServlet();
this.webServer = factory.getWebServer((context) -> {
Dynamic registration = context.addServlet("blockingServlet", blockingServlet);
registration.addMapping("/blocking");
registration.setAsyncSupported(true);
});
this.webServer.start();
int port = this.webServer.getPort();
Future<Object> request = initiateGetRequest(port, "/blocking");
blockingServlet.awaitQueue();
AtomicReference<GracefulShutdownResult> result = new AtomicReference<>();
this.webServer.shutDownGracefully(result::set);
assertThat(result.get()).isNull();
blockingServlet.admitOne();
assertThat(request.get()).isInstanceOf(HttpResponse.class);
Object rejectedResult = initiateGetRequest(port, "/").get();
assertThat(rejectedResult).isInstanceOf(HttpResponse.class);
assertThat(((HttpResponse) rejectedResult).getStatusLine().getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE.value());
this.webServer.stop();
}
use of cn.taketoday.framework.web.server.GracefulShutdownResult in project today-infrastructure by TAKETODAY.
the class JettyServletWebServerFactoryTests method whenARequestCompletesAfterGracefulShutdownHasBegunThenItHasAConnectionCloseHeader.
@Test
void whenARequestCompletesAfterGracefulShutdownHasBegunThenItHasAConnectionCloseHeader() throws InterruptedException, ExecutionException {
AbstractServletWebServerFactory factory = getFactory();
factory.setShutdown(Shutdown.GRACEFUL);
BlockingServlet blockingServlet = new BlockingServlet();
this.webServer = factory.getWebServer((context) -> {
Dynamic registration = context.addServlet("blockingServlet", blockingServlet);
registration.addMapping("/blocking");
registration.setAsyncSupported(true);
});
this.webServer.start();
int port = this.webServer.getPort();
Future<Object> request = initiateGetRequest(port, "/blocking");
blockingServlet.awaitQueue();
AtomicReference<GracefulShutdownResult> result = new AtomicReference<>();
this.webServer.shutDownGracefully(result::set);
blockingServlet.admitOne();
Awaitility.await().atMost(Duration.ofSeconds(5)).until(() -> GracefulShutdownResult.IDLE == result.get());
Object requestResult = request.get();
assertThat(requestResult).isInstanceOf(HttpResponse.class);
assertThat(((HttpResponse) requestResult).getFirstHeader("Connection").getValue()).isEqualTo("close");
}
use of cn.taketoday.framework.web.server.GracefulShutdownResult in project today-framework by TAKETODAY.
the class AbstractReactiveWebServerFactoryTests method givenAnInflightRequestWhenTheServerIsStoppedThenGracefulShutdownCallbackIsCalledWithRequestsActive.
@Test
void givenAnInflightRequestWhenTheServerIsStoppedThenGracefulShutdownCallbackIsCalledWithRequestsActive() throws Exception {
AbstractReactiveWebServerFactory factory = getFactory();
factory.setShutdown(Shutdown.GRACEFUL);
BlockingHandler blockingHandler = new BlockingHandler();
this.webServer = factory.getWebServer(blockingHandler);
this.webServer.start();
Mono<ResponseEntity<Void>> request = getWebClient(this.webServer.getPort()).build().get().retrieve().toBodilessEntity();
AtomicReference<ResponseEntity<Void>> responseReference = new AtomicReference<>();
CountDownLatch responseLatch = new CountDownLatch(1);
request.subscribe((response) -> {
responseReference.set(response);
responseLatch.countDown();
});
blockingHandler.awaitQueue();
AtomicReference<GracefulShutdownResult> result = new AtomicReference<>();
this.webServer.shutDownGracefully(result::set);
assertThat(responseReference.get()).isNull();
try {
this.webServer.stop();
} catch (Exception ex) {
// Continue
}
System.out.println("Stopped");
Awaitility.await().atMost(Duration.ofSeconds(5)).until(() -> GracefulShutdownResult.REQUESTS_ACTIVE == result.get());
blockingHandler.completeOne();
}
Aggregations