Search in sources :

Example 1 with GracefulShutdownResult

use of org.springframework.boot.web.server.GracefulShutdownResult in project spring-boot by spring-projects.

the class AbstractReactiveWebServerFactoryTests method whenARequestIsActiveAfterGracefulShutdownEndsThenStopWillComplete.

@Test
void whenARequestIsActiveAfterGracefulShutdownEndsThenStopWillComplete() throws InterruptedException {
    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);
    this.webServer.stop();
    Awaitility.await().atMost(Duration.ofSeconds(30)).until(() -> GracefulShutdownResult.REQUESTS_ACTIVE == result.get());
    blockingHandler.completeOne();
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) GracefulShutdownResult(org.springframework.boot.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 2 with GracefulShutdownResult

use of org.springframework.boot.web.server.GracefulShutdownResult in project spring-boot by spring-projects.

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(org.springframework.http.ResponseEntity) GracefulShutdownResult(org.springframework.boot.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException) WebClientRequestException(org.springframework.web.reactive.function.client.WebClientRequestException) Test(org.junit.jupiter.api.Test)

Example 3 with GracefulShutdownResult

use of org.springframework.boot.web.server.GracefulShutdownResult in project spring-boot by spring-projects.

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(org.springframework.http.ResponseEntity) GracefulShutdownResult(org.springframework.boot.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with GracefulShutdownResult

use of org.springframework.boot.web.server.GracefulShutdownResult in project spring-boot by spring-projects.

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) 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) ExampleServlet(org.springframework.boot.testsupport.web.servlet.ExampleServlet) Undertow(io.undertow.Undertow) SocketException(java.net.SocketException) Future(java.util.concurrent.Future) Charset(java.nio.charset.Charset) Shutdown(org.springframework.boot.web.server.Shutdown) Locale(java.util.Locale) Duration(java.time.Duration) Map(java.util.Map) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) PortInUseException(org.springframework.boot.web.server.PortInUseException) JspServlet(org.apache.jasper.servlet.JspServlet) InOrder(org.mockito.InOrder) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ErrorPage(org.springframework.boot.web.server.ErrorPage) AbstractServletWebServerFactoryTests(org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) IOException(java.io.IOException) ServletContainer(io.undertow.servlet.api.ServletContainer) GracefulShutdownResult(org.springframework.boot.web.server.GracefulShutdownResult) File(java.io.File) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) SSLException(javax.net.ssl.SSLException) AfterEach(org.junit.jupiter.api.AfterEach) Mockito.inOrder(org.mockito.Mockito.inOrder) AbstractServletWebServerFactory(org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpResponse(org.apache.http.HttpResponse) 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(org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory) GracefulShutdownResult(org.springframework.boot.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 5 with GracefulShutdownResult

use of org.springframework.boot.web.server.GracefulShutdownResult in project spring-boot by spring-projects.

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) 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) Future(java.util.concurrent.Future) AbstractConfiguration(org.eclipse.jetty.webapp.AbstractConfiguration) Configuration(org.eclipse.jetty.webapp.Configuration) BDDMockito.given(org.mockito.BDDMockito.given) Locale(java.util.Locale) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Duration(java.time.Duration) Map(java.util.Map) PortInUseException(org.springframework.boot.web.server.PortInUseException) Method(java.lang.reflect.Method) Server(org.eclipse.jetty.server.Server) JspServlet(org.apache.jasper.servlet.JspServlet) Collection(java.util.Collection) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) AbstractServletWebServerFactoryTests(org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) Test(org.junit.jupiter.api.Test) EventListener(java.util.EventListener) Mockito.inOrder(org.mockito.Mockito.inOrder) ServletContextEvent(jakarta.servlet.ServletContextEvent) 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) WebServerException(org.springframework.boot.web.server.WebServerException) Dynamic(jakarta.servlet.ServletRegistration.Dynamic) AtomicReference(java.util.concurrent.atomic.AtomicReference) Charset(java.nio.charset.Charset) Compression(org.springframework.boot.web.server.Compression) Shutdown(org.springframework.boot.web.server.Shutdown) HttpClient(org.apache.http.client.HttpClient) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ErrorPageErrorHandler(org.eclipse.jetty.servlet.ErrorPageErrorHandler) InOrder(org.mockito.InOrder) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) GracefulShutdownResult(org.springframework.boot.web.server.GracefulShutdownResult) Ssl(org.springframework.boot.web.server.Ssl) ExecutionException(java.util.concurrent.ExecutionException) ServerConnector(org.eclipse.jetty.server.ServerConnector) ReflectionUtils(org.springframework.util.ReflectionUtils) AbstractServletWebServerFactory(org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory) HttpResponse(org.apache.http.HttpResponse) Connector(org.eclipse.jetty.server.Connector) Collections(java.util.Collections) CapturedOutput(org.springframework.boot.testsupport.system.CapturedOutput) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) Dynamic(jakarta.servlet.ServletRegistration.Dynamic) AbstractServletWebServerFactory(org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory) GracefulShutdownResult(org.springframework.boot.web.server.GracefulShutdownResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Test (org.junit.jupiter.api.Test)5 GracefulShutdownResult (org.springframework.boot.web.server.GracefulShutdownResult)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 ResponseEntity (org.springframework.http.ResponseEntity)3 Dynamic (jakarta.servlet.ServletRegistration.Dynamic)2 IOException (java.io.IOException)2 Charset (java.nio.charset.Charset)2 Duration (java.time.Duration)2 Arrays (java.util.Arrays)2 Locale (java.util.Locale)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 HttpResponse (org.apache.http.HttpResponse)2 JspServlet (org.apache.jasper.servlet.JspServlet)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Awaitility (org.awaitility.Awaitility)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 InOrder (org.mockito.InOrder)2