Respect async request timeout of -1 in MockMvc
When falling back on the timeout associated with the async request, a value of -1 must be treated as: never time out. Issue: SPR-16869
This commit is contained in:
parent
82480a7908
commit
2a993bf9ff
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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 + "]" +
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
* <p>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);
|
||||
|
|
|
|||
|
|
@ -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<String> sseFlux() {
|
||||
return Flux.interval(Duration.ofSeconds(1)).take(3)
|
||||
.map(aLong -> String.format("event%d", aLong));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue