Skip to content

Commit 822e244

Browse files
committed
Polishing StandardServletAsyncWebRequestTests
See gh-32340
1 parent cfd0aee commit 822e244

File tree

1 file changed

+91
-100
lines changed

1 file changed

+91
-100
lines changed

spring-web/src/test/java/org/springframework/web/context/request/async/StandardServletAsyncWebRequestTests.java

Lines changed: 91 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import jakarta.servlet.AsyncEvent;
2222
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Nested;
2324
import org.junit.jupiter.api.Test;
2425

2526
import org.springframework.web.testfixture.servlet.MockAsyncContext;
@@ -32,7 +33,8 @@
3233
import static org.mockito.Mockito.verify;
3334

3435
/**
35-
* A test fixture with a {@link StandardServletAsyncWebRequest}.
36+
* Tests for {@link StandardServletAsyncWebRequest}.
37+
*
3638
* @author Rossen Stoyanchev
3739
*/
3840
class StandardServletAsyncWebRequestTests {
@@ -49,120 +51,109 @@ void setup() {
4951
this.request = new MockHttpServletRequest();
5052
this.request.setAsyncSupported(true);
5153
this.response = new MockHttpServletResponse();
52-
this.asyncRequest = new StandardServletAsyncWebRequest(this.request, this.response);
53-
this.asyncRequest.setTimeout(44*1000L);
54-
}
55-
56-
57-
@Test
58-
void isAsyncStarted() {
59-
assertThat(this.asyncRequest.isAsyncStarted()).isFalse();
60-
this.asyncRequest.startAsync();
61-
assertThat(this.asyncRequest.isAsyncStarted()).isTrue();
62-
}
6354

64-
@Test
65-
void startAsync() {
66-
this.asyncRequest.startAsync();
67-
68-
MockAsyncContext context = (MockAsyncContext) this.request.getAsyncContext();
69-
assertThat(context).isNotNull();
70-
assertThat(context.getTimeout()).as("Timeout value not set").isEqualTo((44 * 1000));
71-
assertThat(context.getListeners()).containsExactly(this.asyncRequest);
72-
}
73-
74-
@Test
75-
void startAsyncMultipleTimes() {
76-
this.asyncRequest.startAsync();
77-
this.asyncRequest.startAsync();
78-
this.asyncRequest.startAsync();
79-
this.asyncRequest.startAsync(); // idempotent
80-
81-
MockAsyncContext context = (MockAsyncContext) this.request.getAsyncContext();
82-
assertThat(context).isNotNull();
83-
assertThat(context.getListeners()).hasSize(1);
84-
}
85-
86-
@Test
87-
void startAsyncNotSupported() {
88-
this.request.setAsyncSupported(false);
89-
assertThatIllegalStateException().isThrownBy(
90-
this.asyncRequest::startAsync)
91-
.withMessageContaining("Async support must be enabled");
55+
this.asyncRequest = new StandardServletAsyncWebRequest(this.request, this.response);
56+
this.asyncRequest.setTimeout(44 * 1000L);
9257
}
9358

94-
@Test
95-
void startAsyncAfterCompleted() throws Exception {
96-
this.asyncRequest.onComplete(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
97-
assertThatIllegalStateException().isThrownBy(this.asyncRequest::startAsync)
98-
.withMessage("Cannot start async: [COMPLETED]");
59+
60+
@Nested
61+
class StartAsync {
62+
63+
@Test
64+
void isAsyncStarted() {
65+
assertThat(asyncRequest.isAsyncStarted()).isFalse();
66+
67+
asyncRequest.startAsync();
68+
69+
assertThat(asyncRequest.isAsyncStarted()).isTrue();
70+
}
71+
72+
@Test
73+
void startAsync() {
74+
asyncRequest.startAsync();
75+
76+
MockAsyncContext context = (MockAsyncContext) request.getAsyncContext();
77+
assertThat(context).isNotNull();
78+
assertThat(context.getTimeout()).as("Timeout value not set").isEqualTo((44 * 1000));
79+
assertThat(context.getListeners()).containsExactly(asyncRequest);
80+
}
81+
82+
@Test
83+
void startAsyncMultipleTimes() {
84+
asyncRequest.startAsync();
85+
asyncRequest.startAsync();
86+
asyncRequest.startAsync();
87+
asyncRequest.startAsync();
88+
89+
MockAsyncContext context = (MockAsyncContext) request.getAsyncContext();
90+
assertThat(context).isNotNull();
91+
assertThat(context.getListeners()).hasSize(1);
92+
}
93+
94+
@Test
95+
void startAsyncNotSupported() {
96+
request.setAsyncSupported(false);
97+
assertThatIllegalStateException()
98+
.isThrownBy(asyncRequest::startAsync)
99+
.withMessageContaining("Async support must be enabled");
100+
}
101+
102+
@Test
103+
void startAsyncAfterCompleted() throws Exception {
104+
asyncRequest.startAsync();
105+
asyncRequest.onComplete(new AsyncEvent(new MockAsyncContext(request, response)));
106+
107+
assertThatIllegalStateException()
108+
.isThrownBy(asyncRequest::startAsync)
109+
.withMessage("Cannot start async: [COMPLETED]");
110+
}
111+
112+
@Test
113+
void startAsyncAndSetTimeout() {
114+
asyncRequest.startAsync();
115+
assertThatIllegalStateException().isThrownBy(() -> asyncRequest.setTimeout(25L));
116+
}
99117
}
100118

101-
@Test
102-
void onTimeoutDefaultBehavior() throws Exception {
103-
this.asyncRequest.onTimeout(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
104-
assertThat(this.response.getStatus()).isEqualTo(200);
105-
}
106119

107-
@Test
108-
void onTimeoutHandler() throws Exception {
109-
Runnable timeoutHandler = mock();
110-
this.asyncRequest.addTimeoutHandler(timeoutHandler);
111-
this.asyncRequest.onTimeout(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
112-
verify(timeoutHandler).run();
113-
}
120+
@Nested
121+
class AsyncListenerHandling {
114122

115-
@Test
116-
void onErrorHandler() throws Exception {
117-
Consumer<Throwable> errorHandler = mock();
118-
this.asyncRequest.addErrorHandler(errorHandler);
119-
Exception e = new Exception();
120-
this.asyncRequest.onError(new AsyncEvent(new MockAsyncContext(this.request, this.response), e));
121-
verify(errorHandler).accept(e);
122-
}
123+
@Test
124+
void onTimeoutHandler() throws Exception {
125+
Runnable handler = mock();
126+
asyncRequest.addTimeoutHandler(handler);
123127

124-
@Test
125-
void setTimeoutDuringConcurrentHandling() {
126-
this.asyncRequest.startAsync();
127-
assertThatIllegalStateException().isThrownBy(() ->
128-
this.asyncRequest.setTimeout(25L));
129-
}
128+
asyncRequest.startAsync();
129+
asyncRequest.onTimeout(new AsyncEvent(new MockAsyncContext(request, response)));
130130

131-
@Test
132-
void onCompletionHandler() throws Exception {
133-
Runnable handler = mock();
134-
this.asyncRequest.addCompletionHandler(handler);
131+
verify(handler).run();
132+
}
135133

136-
this.asyncRequest.startAsync();
137-
this.asyncRequest.onComplete(new AsyncEvent(this.request.getAsyncContext()));
134+
@Test
135+
void onErrorHandler() throws Exception {
136+
Exception ex = new Exception();
137+
Consumer<Throwable> handler = mock();
138+
asyncRequest.addErrorHandler(handler);
138139

139-
verify(handler).run();
140-
assertThat(this.asyncRequest.isAsyncComplete()).isTrue();
141-
}
140+
asyncRequest.startAsync();
141+
asyncRequest.onError(new AsyncEvent(new MockAsyncContext(request, response), ex));
142142

143-
// SPR-13292
143+
verify(handler).accept(ex);
144+
}
144145

145-
@Test
146-
void onErrorHandlerAfterOnErrorEvent() throws Exception {
147-
Consumer<Throwable> handler = mock();
148-
this.asyncRequest.addErrorHandler(handler);
146+
@Test
147+
void onCompletionHandler() throws Exception {
148+
Runnable handler = mock();
149+
asyncRequest.addCompletionHandler(handler);
149150

150-
this.asyncRequest.startAsync();
151-
Exception e = new Exception();
152-
this.asyncRequest.onError(new AsyncEvent(this.request.getAsyncContext(), e));
151+
asyncRequest.startAsync();
152+
asyncRequest.onComplete(new AsyncEvent(request.getAsyncContext()));
153153

154-
verify(handler).accept(e);
154+
verify(handler).run();
155+
assertThat(asyncRequest.isAsyncComplete()).isTrue();
156+
}
155157
}
156158

157-
@Test
158-
void onCompletionHandlerAfterOnCompleteEvent() throws Exception {
159-
Runnable handler = mock();
160-
this.asyncRequest.addCompletionHandler(handler);
161-
162-
this.asyncRequest.startAsync();
163-
this.asyncRequest.onComplete(new AsyncEvent(this.request.getAsyncContext()));
164-
165-
verify(handler).run();
166-
assertThat(this.asyncRequest.isAsyncComplete()).isTrue();
167-
}
168159
}

0 commit comments

Comments
 (0)