Skip to content

Commit d0dd8c8

Browse files
committed
Prevent pausing of the web server
1 parent 764f69c commit d0dd8c8

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.web.server.test.client.reactive;
18+
19+
import java.util.Collections;
20+
import java.util.Map;
21+
22+
import org.junit.jupiter.api.Nested;
23+
import org.junit.jupiter.api.Test;
24+
import reactor.core.publisher.Mono;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.SpringBootConfiguration;
28+
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.boot.tomcat.reactive.TomcatReactiveWebServerFactory;
30+
import org.springframework.context.annotation.Bean;
31+
import org.springframework.context.annotation.Import;
32+
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
33+
import org.springframework.http.HttpStatus;
34+
import org.springframework.http.server.reactive.ContextPathCompositeHandler;
35+
import org.springframework.http.server.reactive.HttpHandler;
36+
import org.springframework.http.server.reactive.ServerHttpRequest;
37+
import org.springframework.http.server.reactive.ServerHttpResponse;
38+
import org.springframework.test.context.TestPropertySource;
39+
import org.springframework.test.web.reactive.server.WebTestClient;
40+
41+
/**
42+
* Tests for {@link WebTestClientContextCustomizer} when the test context framework pauses
43+
* a context while it's in the cache.
44+
*
45+
* @author Andy Wilkinson
46+
*/
47+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
48+
properties = "spring.main.web-application-type=none")
49+
class WebTestClientContextCustomizerTcfCacheContextPausingTests {
50+
51+
@Nested
52+
@Import(TestConfig.class)
53+
@TestPropertySource(properties = { "context=one", "spring.main.web-application-type=reactive" })
54+
class ContextOne {
55+
56+
@Autowired
57+
private WebTestClient webTestClient;
58+
59+
@Test
60+
void test() {
61+
this.webTestClient.get().uri("/test").exchange().expectBody(String.class).isEqualTo("hello world");
62+
}
63+
64+
}
65+
66+
@Nested
67+
@Import(TestConfig.class)
68+
@TestPropertySource(properties = { "context=two", "spring.main.web-application-type=reactive" })
69+
class ContextTwo {
70+
71+
@Autowired
72+
private WebTestClient webTestClient;
73+
74+
@Test
75+
void test() {
76+
this.webTestClient.get().uri("/test").exchange().expectBody(String.class).isEqualTo("hello world");
77+
}
78+
79+
}
80+
81+
@Nested
82+
@Import(TestConfig.class)
83+
@TestPropertySource(properties = { "context=one", "spring.main.web-application-type=reactive" })
84+
class ReuseContextOne {
85+
86+
@Autowired
87+
private WebTestClient webTestClient;
88+
89+
@Test
90+
void test() {
91+
this.webTestClient.get().uri("/test").exchange().expectBody(String.class).isEqualTo("hello world");
92+
}
93+
94+
}
95+
96+
@SpringBootConfiguration(proxyBeanMethods = false)
97+
static class TestConfig {
98+
99+
@Bean
100+
TomcatReactiveWebServerFactory webServerFactory() {
101+
return new TomcatReactiveWebServerFactory(0);
102+
}
103+
104+
@Bean
105+
HttpHandler httpHandler() {
106+
TestHandler httpHandler = new TestHandler();
107+
Map<String, HttpHandler> handlersMap = Collections.singletonMap("/test", httpHandler);
108+
return new ContextPathCompositeHandler(handlersMap);
109+
}
110+
111+
}
112+
113+
static class TestHandler implements HttpHandler {
114+
115+
private static final DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
116+
117+
@Override
118+
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
119+
response.setStatusCode(HttpStatus.OK);
120+
return response.writeWith(Mono.just(factory.wrap("hello world".getBytes())));
121+
}
122+
123+
}
124+
125+
}

module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/context/WebServerGracefulShutdownLifecycle.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,9 @@ public int getPhase() {
7575
return WebServerApplicationContext.GRACEFUL_SHUTDOWN_PHASE;
7676
}
7777

78+
@Override
79+
public boolean isPauseable() {
80+
return false;
81+
}
82+
7883
}

module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/reactive/context/WebServerStartStopLifecycle.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ public int getPhase() {
5858
return WebServerApplicationContext.START_STOP_LIFECYCLE_PHASE;
5959
}
6060

61+
@Override
62+
public boolean isPauseable() {
63+
return false;
64+
}
65+
6166
}

module/spring-boot-web-server/src/main/java/org/springframework/boot/web/server/servlet/context/WebServerStartStopLifecycle.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,9 @@ public int getPhase() {
6363
return WebServerApplicationContext.START_STOP_LIFECYCLE_PHASE;
6464
}
6565

66+
@Override
67+
public boolean isPauseable() {
68+
return false;
69+
}
70+
6671
}

0 commit comments

Comments
 (0)