DeferredResult/ResponseBodyEmitter adapter mechanism

The DeferredResult~ and the ResponseBodyEmitterReturnValueHandler now
each expose an adapter mechanism for plugging in other async return
value types. As a result the ListenableFutureReturnValueHandler and
CompletionStageReturnValueHandler are no longer needed and are now
deprecated.

Issue: SPR-14046
This commit is contained in:
Rossen Stoyanchev
2016-03-12 16:16:26 -05:00
parent 2152f436f9
commit 971ccab038
9 changed files with 479 additions and 59 deletions

View File

@@ -0,0 +1,197 @@
/*
* Copyright 2002-2016 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.web.servlet.mvc.method.annotation;
import java.lang.reflect.Method;
import java.util.concurrent.CompletableFuture;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.SettableListenableFuture;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.async.AsyncWebRequest;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.StandardServletAsyncWebRequest;
import org.springframework.web.context.request.async.WebAsyncUtils;
import org.springframework.web.method.support.ModelAndViewContainer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* Unit tests for {@link DeferredResultMethodReturnValueHandler}.
* @author Rossen Stoyanchev
*/
public class DeferredResultReturnValueHandlerTests {
private DeferredResultMethodReturnValueHandler handler;
private MockHttpServletRequest request;
private NativeWebRequest webRequest;
@Before
public void setUp() throws Exception {
this.handler = new DeferredResultMethodReturnValueHandler();
this.request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
this.webRequest = new ServletWebRequest(this.request, response);
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, response);
WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
this.request.setAsyncSupported(true);
}
@Test
public void supportsReturnType() throws Exception {
assertTrue(this.handler.supportsReturnType(returnType("handleDeferredResult")));
assertTrue(this.handler.supportsReturnType(returnType("handleListenableFuture")));
assertTrue(this.handler.supportsReturnType(returnType("handleCompletableFuture")));
assertFalse(this.handler.supportsReturnType(returnType("handleString")));
}
@Test
public void deferredResult() throws Exception {
MethodParameter returnType = returnType("handleDeferredResult");
DeferredResult<String> deferredResult = new DeferredResult<>();
handleReturnValue(deferredResult, returnType);
assertTrue(this.request.isAsyncStarted());
assertFalse(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
deferredResult.setResult("foo");
assertTrue(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
assertEquals("foo", WebAsyncUtils.getAsyncManager(this.webRequest).getConcurrentResult());
}
@Test
public void deferredResultWitError() throws Exception {
MethodParameter returnType = returnType("handleDeferredResult");
DeferredResult<String> deferredResult = new DeferredResult<>();
handleReturnValue(deferredResult, returnType);
assertTrue(this.request.isAsyncStarted());
assertFalse(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
IllegalStateException ex = new IllegalStateException();
deferredResult.setErrorResult(ex);
assertTrue(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
assertSame(ex, WebAsyncUtils.getAsyncManager(this.webRequest).getConcurrentResult());
}
@Test
public void listenableFuture() throws Exception {
MethodParameter returnType = returnType("handleListenableFuture");
SettableListenableFuture<String> future = new SettableListenableFuture<>();
handleReturnValue(future, returnType);
assertTrue(this.request.isAsyncStarted());
assertFalse(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
future.set("foo");
assertTrue(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
assertEquals("foo", WebAsyncUtils.getAsyncManager(this.webRequest).getConcurrentResult());
}
@Test
public void listenableFutureWithError() throws Exception {
MethodParameter returnType = returnType("handleListenableFuture");
SettableListenableFuture<String> future = new SettableListenableFuture<>();
handleReturnValue(future, returnType);
assertTrue(this.request.isAsyncStarted());
assertFalse(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
IllegalStateException ex = new IllegalStateException();
future.setException(ex);
assertTrue(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
assertSame(ex, WebAsyncUtils.getAsyncManager(this.webRequest).getConcurrentResult());
}
@Test
public void completableFuture() throws Exception {
MethodParameter returnType = returnType("handleCompletableFuture");
SettableListenableFuture<String> future = new SettableListenableFuture<>();
handleReturnValue(future, returnType);
assertTrue(this.request.isAsyncStarted());
assertFalse(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
future.set("foo");
assertTrue(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
assertEquals("foo", WebAsyncUtils.getAsyncManager(this.webRequest).getConcurrentResult());
}
@Test
public void completableFutureWithError() throws Exception {
MethodParameter returnType = returnType("handleCompletableFuture");
CompletableFuture<String> future = new CompletableFuture<>();
handleReturnValue(future, returnType);
assertTrue(this.request.isAsyncStarted());
assertFalse(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
IllegalStateException ex = new IllegalStateException();
future.completeExceptionally(ex);
assertTrue(WebAsyncUtils.getAsyncManager(this.webRequest).hasConcurrentResult());
assertSame(ex, WebAsyncUtils.getAsyncManager(this.webRequest).getConcurrentResult());
}
private void handleReturnValue(Object returnValue, MethodParameter returnType) throws Exception {
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
this.handler.handleReturnValue(returnValue, returnType, mavContainer, this.webRequest);
}
private MethodParameter returnType(String methodName) throws NoSuchMethodException {
Method method = TestController.class.getDeclaredMethod(methodName);
return new MethodParameter(method, -1);
}
@SuppressWarnings("unused")
private static class TestController {
private String handleString() {
return null;
}
private DeferredResult<String> handleDeferredResult() {
return null;
}
private ListenableFuture<String> handleListenableFuture() {
return null;
}
private CompletableFuture<String> handleCompletableFuture() {
return null;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -51,14 +51,12 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
private ResponseBodyEmitterReturnValueHandler handler;
private ModelAndViewContainer mavContainer;
private NativeWebRequest webRequest;
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private NativeWebRequest webRequest;
@Before
public void setUp() throws Exception {
@@ -67,8 +65,6 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
new StringHttpMessageConverter(), new MappingJackson2HttpMessageConverter());
this.handler = new ResponseBodyEmitterReturnValueHandler(converters);
this.mavContainer = new ModelAndViewContainer();
this.request = new MockHttpServletRequest();
this.response = new MockHttpServletResponse();
this.webRequest = new ServletWebRequest(this.request, this.response);
@@ -80,18 +76,18 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
@Test
public void supportsReturnType() throws Exception {
assertTrue(this.handler.supportsReturnType(returnType(TestController.class, "handle")));
assertTrue(this.handler.supportsReturnType(returnType(TestController.class, "handleSse")));
assertTrue(this.handler.supportsReturnType(returnType(TestController.class, "handleResponseEntity")));
assertFalse(this.handler.supportsReturnType(returnType(TestController.class, "handleResponseEntityString")));
assertFalse(this.handler.supportsReturnType(returnType(TestController.class, "handleResponseEntityParameterized")));
assertTrue(this.handler.supportsReturnType(returnType("handle")));
assertTrue(this.handler.supportsReturnType(returnType("handleSse")));
assertTrue(this.handler.supportsReturnType(returnType("handleResponseEntity")));
assertFalse(this.handler.supportsReturnType(returnType("handleResponseEntityString")));
assertFalse(this.handler.supportsReturnType(returnType("handleResponseEntityParameterized")));
}
@Test
public void responseBodyEmitter() throws Exception {
MethodParameter returnType = returnType(TestController.class, "handle");
MethodParameter returnType = returnType("handle");
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);
handleReturnValue(emitter, returnType);
assertTrue(this.request.isAsyncStarted());
assertEquals("", this.response.getContentAsString());
@@ -133,8 +129,8 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
emitter.onTimeout(mock(Runnable.class));
emitter.onCompletion(mock(Runnable.class));
MethodParameter returnType = returnType(TestController.class, "handle");
this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);
MethodParameter returnType = returnType("handle");
handleReturnValue(emitter, returnType);
verify(asyncWebRequest).setTimeout(19000L);
verify(asyncWebRequest).addTimeoutHandler(any(Runnable.class));
@@ -144,9 +140,9 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
@Test
public void sseEmitter() throws Exception {
MethodParameter returnType = returnType(TestController.class, "handleSse");
MethodParameter returnType = returnType("handleSse");
SseEmitter emitter = new SseEmitter();
this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);
handleReturnValue(emitter, returnType);
assertTrue(this.request.isAsyncStarted());
assertEquals(200, this.response.getStatus());
@@ -174,9 +170,9 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
@Test
public void responseEntitySse() throws Exception {
MethodParameter returnType = returnType(TestController.class, "handleResponseEntitySse");
ResponseEntity<SseEmitter> emitter = ResponseEntity.ok().header("foo", "bar").body(new SseEmitter());
this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);
MethodParameter returnType = returnType("handleResponseEntitySse");
ResponseEntity<SseEmitter> entity = ResponseEntity.ok().header("foo", "bar").body(new SseEmitter());
handleReturnValue(entity, returnType);
assertTrue(this.request.isAsyncStarted());
assertEquals(200, this.response.getStatus());
@@ -186,17 +182,21 @@ public class ResponseBodyEmitterReturnValueHandlerTests {
@Test
public void responseEntitySseNoContent() throws Exception {
MethodParameter returnType = returnType(TestController.class, "handleResponseEntitySse");
ResponseEntity<?> emitter = ResponseEntity.noContent().build();
this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);
MethodParameter returnType = returnType("handleResponseEntitySse");
ResponseEntity<?> entity = ResponseEntity.noContent().build();
handleReturnValue(entity, returnType);
assertFalse(this.request.isAsyncStarted());
assertEquals(204, this.response.getStatus());
}
private void handleReturnValue(Object returnValue, MethodParameter returnType) throws Exception {
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
this.handler.handleReturnValue(returnValue, returnType, mavContainer, this.webRequest);
}
private MethodParameter returnType(Class<?> clazz, String methodName) throws NoSuchMethodException {
Method method = clazz.getDeclaredMethod(methodName);
private MethodParameter returnType(String methodName) throws NoSuchMethodException {
Method method = TestController.class.getDeclaredMethod(methodName);
return new MethodParameter(method, -1);
}