MvcResult returns asyncResult after asyncDispatch

Issue: SPR-16648
This commit is contained in:
Rossen Stoyanchev
2018-03-28 22:13:34 -04:00
parent e6020ed377
commit f9e6ea5482
5 changed files with 93 additions and 49 deletions

View File

@@ -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.
@@ -30,6 +30,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.util.WebUtils;
@@ -43,10 +44,12 @@ public class MockAsyncContext implements AsyncContext {
private final HttpServletRequest request;
@Nullable
private final HttpServletResponse response;
private final List<AsyncListener> listeners = new ArrayList<>();
@Nullable
private String dispatchedPath;
private long timeout = 10 * 1000L; // 10 seconds is Tomcat's default
@@ -54,7 +57,7 @@ public class MockAsyncContext implements AsyncContext {
private final List<Runnable> dispatchHandlers = new ArrayList<>();
public MockAsyncContext(ServletRequest request, ServletResponse response) {
public MockAsyncContext(ServletRequest request, @Nullable ServletResponse response) {
this.request = (HttpServletRequest) request;
this.response = (HttpServletResponse) response;
}
@@ -62,7 +65,14 @@ public class MockAsyncContext implements AsyncContext {
public void addDispatchHandler(Runnable handler) {
Assert.notNull(handler, "Dispatch handler must not be null");
this.dispatchHandlers.add(handler);
synchronized (this) {
if (this.dispatchedPath == null) {
this.dispatchHandlers.add(handler);
}
else {
handler.run();
}
}
}
@Override
@@ -71,6 +81,7 @@ public class MockAsyncContext implements AsyncContext {
}
@Override
@Nullable
public ServletResponse getResponse() {
return this.response;
}
@@ -91,13 +102,14 @@ public class MockAsyncContext implements AsyncContext {
}
@Override
public void dispatch(ServletContext context, String path) {
this.dispatchedPath = path;
for (Runnable r : this.dispatchHandlers) {
r.run();
public void dispatch(@Nullable ServletContext context, String path) {
synchronized (this) {
this.dispatchedPath = path;
this.dispatchHandlers.forEach(Runnable::run);
}
}
@Nullable
public String getDispatchedPath() {
return this.dispatchedPath;
}