Search in sources :

Example 1 with GracefulShutdownResult

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();
}
Also used : ResponseEntity(cn.taketoday.http.ResponseEntity) GracefulShutdownResult(cn.taketoday.framework.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) WebClientRequestException(cn.taketoday.web.reactive.function.client.WebClientRequestException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 2 with GracefulShutdownResult

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());
}
Also used : ResponseEntity(cn.taketoday.http.ResponseEntity) GracefulShutdownResult(cn.taketoday.framework.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 3 with GracefulShutdownResult

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();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) AbstractServletWebServerFactoryTests(cn.taketoday.framework.web.servlet.server.AbstractServletWebServerFactoryTests) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Builder(io.undertow.Undertow.Builder) Dynamic(jakarta.servlet.ServletRegistration.Dynamic) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Undertow(io.undertow.Undertow) PortInUseException(cn.taketoday.framework.web.server.PortInUseException) SocketException(java.net.SocketException) Future(java.util.concurrent.Future) Charset(java.nio.charset.Charset) Locale(java.util.Locale) Duration(java.time.Duration) Map(java.util.Map) Assertions(org.assertj.core.api.Assertions) ReflectionTestUtils(cn.taketoday.test.util.ReflectionTestUtils) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet) JspServlet(org.apache.jasper.servlet.JspServlet) InOrder(org.mockito.InOrder) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) ServletContainer(io.undertow.servlet.api.ServletContainer) File(java.io.File) Shutdown(cn.taketoday.framework.web.server.Shutdown) ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) Test(org.junit.jupiter.api.Test) ErrorPage(cn.taketoday.framework.web.server.ErrorPage) GracefulShutdownResult(cn.taketoday.framework.web.server.GracefulShutdownResult) AfterEach(org.junit.jupiter.api.AfterEach) SSLException(javax.net.ssl.SSLException) Mockito.inOrder(org.mockito.Mockito.inOrder) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpResponse(org.apache.http.HttpResponse) HttpStatus(cn.taketoday.http.HttpStatus) AbstractServletWebServerFactory(cn.taketoday.framework.web.servlet.server.AbstractServletWebServerFactory) Matchers.is(org.hamcrest.Matchers.is) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) Awaitility(org.awaitility.Awaitility) Mockito.mock(org.mockito.Mockito.mock) Dynamic(jakarta.servlet.ServletRegistration.Dynamic) AbstractServletWebServerFactory(cn.taketoday.framework.web.servlet.server.AbstractServletWebServerFactory) GracefulShutdownResult(cn.taketoday.framework.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 4 with GracefulShutdownResult

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");
}
Also used : SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Arrays(java.util.Arrays) AbstractServletWebServerFactoryTests(cn.taketoday.framework.web.servlet.server.AbstractServletWebServerFactoryTests) ClassMatcher(org.eclipse.jetty.webapp.ClassMatcher) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Disabled(org.junit.jupiter.api.Disabled) Header(org.apache.http.Header) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) InetAddress(java.net.InetAddress) AbstractConfiguration(org.eclipse.jetty.webapp.AbstractConfiguration) Future(java.util.concurrent.Future) Configuration(org.eclipse.jetty.webapp.Configuration) BDDMockito.given(org.mockito.BDDMockito.given) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Locale(java.util.Locale) Duration(java.time.Duration) Map(java.util.Map) Assertions(org.assertj.core.api.Assertions) Server(org.eclipse.jetty.server.Server) Method(java.lang.reflect.Method) JspServlet(org.apache.jasper.servlet.JspServlet) Collection(java.util.Collection) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) WebServerException(cn.taketoday.framework.web.server.WebServerException) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) Test(org.junit.jupiter.api.Test) GracefulShutdownResult(cn.taketoday.framework.web.server.GracefulShutdownResult) EventListener(java.util.EventListener) Mockito.inOrder(org.mockito.Mockito.inOrder) ServletContextEvent(jakarta.servlet.ServletContextEvent) CapturedOutput(cn.taketoday.framework.test.system.CapturedOutput) Awaitility(org.awaitility.Awaitility) HttpClients(org.apache.http.impl.client.HttpClients) ServletContextListener(jakarta.servlet.ServletContextListener) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Handler(org.eclipse.jetty.server.Handler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Dynamic(jakarta.servlet.ServletRegistration.Dynamic) AtomicReference(java.util.concurrent.atomic.AtomicReference) Compression(cn.taketoday.framework.web.server.Compression) PortInUseException(cn.taketoday.framework.web.server.PortInUseException) Charset(java.nio.charset.Charset) HttpClient(org.apache.http.client.HttpClient) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ErrorPageErrorHandler(org.eclipse.jetty.servlet.ErrorPageErrorHandler) InOrder(org.mockito.InOrder) Ssl(cn.taketoday.framework.web.server.Ssl) ReflectionUtils(cn.taketoday.util.ReflectionUtils) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) Shutdown(cn.taketoday.framework.web.server.Shutdown) ExecutionException(java.util.concurrent.ExecutionException) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpResponse(org.apache.http.HttpResponse) AbstractServletWebServerFactory(cn.taketoday.framework.web.servlet.server.AbstractServletWebServerFactory) Connector(org.eclipse.jetty.server.Connector) Collections(java.util.Collections) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) Dynamic(jakarta.servlet.ServletRegistration.Dynamic) AbstractServletWebServerFactory(cn.taketoday.framework.web.servlet.server.AbstractServletWebServerFactory) GracefulShutdownResult(cn.taketoday.framework.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 5 with GracefulShutdownResult

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();
}
Also used : ResponseEntity(cn.taketoday.http.ResponseEntity) GracefulShutdownResult(cn.taketoday.framework.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) WebClientRequestException(cn.taketoday.web.reactive.function.client.WebClientRequestException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

GracefulShutdownResult (cn.taketoday.framework.web.server.GracefulShutdownResult)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 Test (org.junit.jupiter.api.Test)10 ResponseEntity (cn.taketoday.http.ResponseEntity)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 PortInUseException (cn.taketoday.framework.web.server.PortInUseException)4 Shutdown (cn.taketoday.framework.web.server.Shutdown)4 AbstractServletWebServerFactory (cn.taketoday.framework.web.servlet.server.AbstractServletWebServerFactory)4 AbstractServletWebServerFactoryTests (cn.taketoday.framework.web.servlet.server.AbstractServletWebServerFactoryTests)4 Dynamic (jakarta.servlet.ServletRegistration.Dynamic)4 Charset (java.nio.charset.Charset)4 Duration (java.time.Duration)4 Arrays (java.util.Arrays)4 Locale (java.util.Locale)4 Map (java.util.Map)4 Future (java.util.concurrent.Future)4 HttpResponse (org.apache.http.HttpResponse)4 JspServlet (org.apache.jasper.servlet.JspServlet)4 Assertions (org.assertj.core.api.Assertions)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4