Fix intermittent test failure in AsyncTests
This commit is contained in:
@@ -22,8 +22,6 @@ import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.web.context.request.async.WebAsyncManager;
|
||||
import org.springframework.web.context.request.async.WebAsyncUtils;
|
||||
import org.springframework.web.servlet.FlashMap;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
@@ -50,6 +48,8 @@ class DefaultMvcResult implements MvcResult {
|
||||
|
||||
private Exception resolvedException;
|
||||
|
||||
private Object asyncResult;
|
||||
|
||||
private CountDownLatch asyncResultLatch;
|
||||
|
||||
|
||||
@@ -105,29 +105,23 @@ class DefaultMvcResult implements MvcResult {
|
||||
return RequestContextUtils.getOutputFlashMap(mockRequest);
|
||||
}
|
||||
|
||||
public void setAsyncResultLatch(CountDownLatch asyncResultLatch) {
|
||||
this.asyncResultLatch = asyncResultLatch;
|
||||
public void setAsyncResult(Object asyncResult) {
|
||||
this.asyncResult = asyncResult;
|
||||
}
|
||||
|
||||
public Object getAsyncResult() {
|
||||
HttpServletRequest request = this.mockRequest;
|
||||
if (request.isAsyncStarted()) {
|
||||
|
||||
long timeout = request.getAsyncContext().getTimeout();
|
||||
if (!awaitAsyncResult(timeout)) {
|
||||
if (!awaitAsyncResult(request)) {
|
||||
throw new IllegalStateException(
|
||||
"Gave up waiting on async result from [" + this.handler + "] to complete");
|
||||
}
|
||||
|
||||
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.mockRequest);
|
||||
if (asyncManager.hasConcurrentResult()) {
|
||||
return asyncManager.getConcurrentResult();
|
||||
"Gave up waiting on async result from handler [" + this.handler + "] to complete");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return this.asyncResult;
|
||||
}
|
||||
|
||||
private boolean awaitAsyncResult(long timeout) {
|
||||
private boolean awaitAsyncResult(HttpServletRequest request) {
|
||||
long timeout = request.getAsyncContext().getTimeout();
|
||||
if (this.asyncResultLatch != null) {
|
||||
try {
|
||||
return this.asyncResultLatch.await(timeout, TimeUnit.MILLISECONDS);
|
||||
@@ -139,4 +133,8 @@ class DefaultMvcResult implements MvcResult {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setAsyncResultLatch(CountDownLatch asyncResultLatch) {
|
||||
this.asyncResultLatch = asyncResultLatch;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -66,19 +66,21 @@ final class TestDispatcherServlet extends DispatcherServlet {
|
||||
super.service(request, response);
|
||||
}
|
||||
|
||||
private CountDownLatch registerAsyncInterceptors(HttpServletRequest request) {
|
||||
private CountDownLatch registerAsyncInterceptors(final HttpServletRequest servletRequest) {
|
||||
|
||||
final CountDownLatch asyncResultLatch = new CountDownLatch(1);
|
||||
|
||||
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
|
||||
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(servletRequest);
|
||||
|
||||
asyncManager.registerCallableInterceptor(KEY, new CallableProcessingInterceptorAdapter() {
|
||||
public <T> void postProcess(NativeWebRequest request, Callable<T> task, Object value) throws Exception {
|
||||
getMvcResult(servletRequest).setAsyncResult(value);
|
||||
asyncResultLatch.countDown();
|
||||
}
|
||||
});
|
||||
asyncManager.registerDeferredResultInterceptor(KEY, new DeferredResultProcessingInterceptorAdapter() {
|
||||
public <T> void postProcess(NativeWebRequest request, DeferredResult<T> result, Object value) throws Exception {
|
||||
getMvcResult(servletRequest).setAsyncResult(value);
|
||||
asyncResultLatch.countDown();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user