diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockAsyncContext.java b/spring-test/src/main/java/org/springframework/mock/web/MockAsyncContext.java index ed63a6902df..1ca1fec66ae 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockAsyncContext.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockAsyncContext.java @@ -154,6 +154,17 @@ public class MockAsyncContext implements AsyncContext { return BeanUtils.instantiateClass(clazz); } + /** + * By default this is set to 10000 (10 seconds) even though the Servlet API + * specifies a default async request timeout of 30 seconds. Keep in mind the + * timeout could further be impacted by global configuration through the MVC + * Java config or the XML namespace, as well as be overridden per request on + * {@link org.springframework.web.context.request.async.DeferredResult DeferredResult} + * or on + * {@link org.springframework.web.servlet.mvc.method.annotation.SseEmitter SseEmitter}. + * @param timeout the timeout value to use. + * @see AsyncContext#setTimeout(long) + */ @Override public void setTimeout(long timeout) { this.timeout = timeout; diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java b/spring-test/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java index 3157b191441..7239c3d8f29 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java @@ -138,8 +138,9 @@ class DefaultMvcResult implements MvcResult { @Override public Object getAsyncResult(long timeToWait) { - if (this.mockRequest.getAsyncContext() != null) { - timeToWait = (timeToWait == -1 ? this.mockRequest.getAsyncContext().getTimeout() : timeToWait); + if (this.mockRequest.getAsyncContext() != null && timeToWait == -1) { + long requestTimeout = this.mockRequest.getAsyncContext().getTimeout(); + timeToWait = requestTimeout == -1 ? Long.MAX_VALUE : requestTimeout; } if (!awaitAsyncDispatch(timeToWait)) { throw new IllegalStateException("Async result for handler [" + this.handler + "]" + diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/MvcResult.java b/spring-test/src/main/java/org/springframework/test/web/servlet/MvcResult.java index 05892882f93..ad4788aeb9d 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/MvcResult.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/MvcResult.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License; Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -80,19 +80,23 @@ public interface MvcResult { FlashMap getFlashMap(); /** - * Get the result of async execution. This method will wait for the async result - * to be set for up to the amount of time configured on the async request, - * i.e. {@link org.springframework.mock.web.MockAsyncContext#getTimeout()}. + * Get the result of async execution. + *
This method will wait for the async result to be set within the
+ * timeout value associated with the async request, see
+ * {@link org.springframework.mock.web.MockAsyncContext#setTimeout
+ * MockAsyncContext#setTimeout}. Alternatively, use
+ * {@link #getAsyncResult(long)} to specify the amount of time to wait.
* @throws IllegalStateException if the async result was not set
*/
Object getAsyncResult();
/**
- * Get the result of async execution. This method will wait for the async result
- * to be set for up to the specified amount of time.
+ * Get the result of async execution and wait if necessary.
* @param timeToWait how long to wait for the async result to be set, in
- * milliseconds; if -1, then the async request timeout value is used,
- * i.e.{@link org.springframework.mock.web.MockAsyncContext#getTimeout()}.
+ * milliseconds; if -1, then fall back on the timeout value associated with
+ * the async request, see
+ * {@link org.springframework.mock.web.MockAsyncContext#setTimeout
+ * MockAsyncContext#setTimeout} for more details.
* @throws IllegalStateException if the async result was not set
*/
Object getAsyncResult(long timeToWait);
diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ReactiveReturnTypeTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ReactiveReturnTypeTests.java
new file mode 100644
index 00000000000..c19bf4a26c0
--- /dev/null
+++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ReactiveReturnTypeTests.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2002-2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.test.web.servlet.samples.standalone;
+
+import java.time.Duration;
+
+import org.junit.Test;
+import reactor.core.publisher.Flux;
+
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
+
+/**
+ * Tests with reactive return value types.
+ *
+ * @author Rossen Stoyanchev
+ */
+public class ReactiveReturnTypeTests {
+
+
+ @Test // SPR-16869
+ public void sseWithFlux() throws Exception {
+
+ MockMvc mockMvc = MockMvcBuilders.standaloneSetup(ReactiveController.class).build();
+
+ MvcResult mvcResult = mockMvc.perform(get("/spr16869"))
+ .andExpect(request().asyncStarted())
+ .andExpect(status().isOk())
+ .andReturn();
+
+ mockMvc.perform(asyncDispatch(mvcResult))
+ .andExpect(content().string("data:event0\n\ndata:event1\n\ndata:event2\n\n"));
+ }
+
+
+
+ @RestController
+ static class ReactiveController {
+
+ @GetMapping(path = "/spr16869", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
+ Flux