Add onTimeout/onCompletion callbacks to DeferredResult
Issue: SPR-9914
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.web.context.request.async;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -80,27 +81,42 @@ public class DeferredResultTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setExpired() {
|
||||
DeferredResult<String> result = new DeferredResult<String>();
|
||||
assertFalse(result.isSetOrExpired());
|
||||
public void onCompletion() throws Exception {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
||||
DeferredResult<String> result = new DeferredResult<String>();
|
||||
result.onCompletion(new Runnable() {
|
||||
public void run() {
|
||||
sb.append("completion event");
|
||||
}
|
||||
});
|
||||
|
||||
result.getInterceptor().afterCompletion(null, null);
|
||||
|
||||
result.expire();
|
||||
assertTrue(result.isSetOrExpired());
|
||||
assertFalse(result.setResult("hello"));
|
||||
assertEquals("completion event", sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void applyTimeoutResult() {
|
||||
public void onTimeout() throws Exception {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
||||
DeferredResultHandler handler = createMock(DeferredResultHandler.class);
|
||||
handler.handleResult("timed out");
|
||||
handler.handleResult("timeout result");
|
||||
replay(handler);
|
||||
|
||||
DeferredResult<String> result = new DeferredResult<String>(null, "timed out");
|
||||
DeferredResult<String> result = new DeferredResult<String>(null, "timeout result");
|
||||
result.setResultHandler(handler);
|
||||
result.onTimeout(new Runnable() {
|
||||
public void run() {
|
||||
sb.append("timeout event");
|
||||
}
|
||||
});
|
||||
|
||||
assertTrue(result.applyTimeoutResult());
|
||||
assertFalse("Shouldn't be able to set result after timeout", result.setResult("hello"));
|
||||
result.getInterceptor().afterTimeout(null, null);
|
||||
|
||||
assertEquals("timeout event", sb.toString());
|
||||
assertFalse("Should not be able to set result a second time", result.setResult("hello"));
|
||||
verify(handler);
|
||||
}
|
||||
|
||||
|
||||
@@ -128,7 +128,7 @@ public class StandardServletAsyncWebRequestTests {
|
||||
timeoutHandler.run();
|
||||
replay(timeoutHandler);
|
||||
|
||||
this.asyncRequest.setTimeoutHandler(timeoutHandler);
|
||||
this.asyncRequest.addTimeoutHandler(timeoutHandler);
|
||||
this.asyncRequest.onTimeout(new AsyncEvent(null));
|
||||
|
||||
verify(timeoutHandler);
|
||||
|
||||
@@ -208,13 +208,13 @@ public class WebAsyncManagerTests {
|
||||
replay(executor);
|
||||
|
||||
this.asyncWebRequest.setTimeout(1000L);
|
||||
this.asyncWebRequest.setTimeoutHandler(EasyMock.<Runnable>anyObject());
|
||||
this.asyncWebRequest.addTimeoutHandler(EasyMock.<Runnable>anyObject());
|
||||
this.asyncWebRequest.addCompletionHandler(EasyMock.<Runnable>anyObject());
|
||||
this.asyncWebRequest.startAsync();
|
||||
replay(this.asyncWebRequest);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
AsyncTask<Object> asyncTask = new AsyncTask<Object>(1000L, executor, createMock(Callable.class));
|
||||
MvcAsyncTask<Object> asyncTask = new MvcAsyncTask<Object>(1000L, executor, createMock(Callable.class));
|
||||
this.asyncManager.startCallableProcessing(asyncTask);
|
||||
|
||||
verify(executor, this.asyncWebRequest);
|
||||
@@ -311,7 +311,7 @@ public class WebAsyncManagerTests {
|
||||
}
|
||||
|
||||
private void setupDefaultAsyncScenario() {
|
||||
this.asyncWebRequest.setTimeoutHandler((Runnable) notNull());
|
||||
this.asyncWebRequest.addTimeoutHandler((Runnable) notNull());
|
||||
this.asyncWebRequest.addCompletionHandler((Runnable) notNull());
|
||||
this.asyncWebRequest.startAsync();
|
||||
expect(this.asyncWebRequest.isAsyncComplete()).andReturn(false);
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.web.context.request.async;
|
||||
|
||||
import static org.springframework.web.context.request.async.CallableProcessingInterceptor.RESULT_NONE;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.createStrictMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
@@ -25,6 +24,8 @@ import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.web.context.request.async.CallableProcessingInterceptor.RESULT_NONE;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
@@ -94,7 +95,27 @@ public class WebAsyncManagerTimeoutTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startCallableProcessingTimeoutAndResume() throws Exception {
|
||||
public void startCallableProcessingTimeoutAndResumeThroughCallback() throws Exception {
|
||||
|
||||
StubCallable callable = new StubCallable();
|
||||
MvcAsyncTask<Object> mvcAsyncTask = new MvcAsyncTask<Object>(callable);
|
||||
mvcAsyncTask.onTimeout(new Callable<Object>() {
|
||||
public Object call() throws Exception {
|
||||
return 7;
|
||||
}
|
||||
});
|
||||
|
||||
this.asyncManager.startCallableProcessing(mvcAsyncTask);
|
||||
|
||||
this.asyncWebRequest.onTimeout(ASYNC_EVENT);
|
||||
|
||||
assertTrue(this.asyncManager.hasConcurrentResult());
|
||||
assertEquals(7, this.asyncManager.getConcurrentResult());
|
||||
assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startCallableProcessingTimeoutAndResumeThroughInterceptor() throws Exception {
|
||||
|
||||
StubCallable callable = new StubCallable();
|
||||
|
||||
@@ -107,6 +128,7 @@ public class WebAsyncManagerTimeoutTests {
|
||||
|
||||
this.asyncWebRequest.onTimeout(ASYNC_EVENT);
|
||||
|
||||
assertTrue(this.asyncManager.hasConcurrentResult());
|
||||
assertEquals(22, this.asyncManager.getConcurrentResult());
|
||||
assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
|
||||
|
||||
@@ -128,6 +150,7 @@ public class WebAsyncManagerTimeoutTests {
|
||||
|
||||
this.asyncWebRequest.onTimeout(ASYNC_EVENT);
|
||||
|
||||
assertTrue(this.asyncManager.hasConcurrentResult());
|
||||
assertEquals(exception, this.asyncManager.getConcurrentResult());
|
||||
assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
|
||||
|
||||
@@ -161,25 +184,38 @@ public class WebAsyncManagerTimeoutTests {
|
||||
public void startDeferredResultProcessingTimeoutAndResumeWithDefaultResult() throws Exception {
|
||||
|
||||
DeferredResult<Integer> deferredResult = new DeferredResult<Integer>(null, 23);
|
||||
|
||||
DeferredResultProcessingInterceptor interceptor = new DeferredResultProcessingInterceptorAdapter() {
|
||||
public <T> void afterTimeout(NativeWebRequest request, DeferredResult<T> result) throws Exception {
|
||||
result.setErrorResult("should not get here");
|
||||
}
|
||||
};
|
||||
|
||||
this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
|
||||
this.asyncManager.startDeferredResultProcessing(deferredResult);
|
||||
|
||||
AsyncEvent event = null;
|
||||
this.asyncWebRequest.onTimeout(event);
|
||||
|
||||
assertTrue(this.asyncManager.hasConcurrentResult());
|
||||
assertEquals(23, this.asyncManager.getConcurrentResult());
|
||||
assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startDeferredResultProcessingTimeoutAndResumeWithInterceptor() throws Exception {
|
||||
public void startDeferredResultProcessingTimeoutAndResumeThroughCallback() throws Exception {
|
||||
|
||||
final DeferredResult<Integer> deferredResult = new DeferredResult<Integer>();
|
||||
deferredResult.onTimeout(new Runnable() {
|
||||
public void run() {
|
||||
deferredResult.setResult(23);
|
||||
}
|
||||
});
|
||||
|
||||
this.asyncManager.startDeferredResultProcessing(deferredResult);
|
||||
|
||||
AsyncEvent event = null;
|
||||
this.asyncWebRequest.onTimeout(event);
|
||||
|
||||
assertTrue(this.asyncManager.hasConcurrentResult());
|
||||
assertEquals(23, this.asyncManager.getConcurrentResult());
|
||||
assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startDeferredResultProcessingTimeoutAndResumeThroughInterceptor() throws Exception {
|
||||
|
||||
DeferredResult<Integer> deferredResult = new DeferredResult<Integer>();
|
||||
|
||||
@@ -195,6 +231,7 @@ public class WebAsyncManagerTimeoutTests {
|
||||
AsyncEvent event = null;
|
||||
this.asyncWebRequest.onTimeout(event);
|
||||
|
||||
assertTrue(this.asyncManager.hasConcurrentResult());
|
||||
assertEquals(23, this.asyncManager.getConcurrentResult());
|
||||
assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
|
||||
}
|
||||
@@ -217,6 +254,7 @@ public class WebAsyncManagerTimeoutTests {
|
||||
AsyncEvent event = null;
|
||||
this.asyncWebRequest.onTimeout(event);
|
||||
|
||||
assertTrue(this.asyncManager.hasConcurrentResult());
|
||||
assertEquals(exception, this.asyncManager.getConcurrentResult());
|
||||
assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user