use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.
the class WebAsyncManagerTimeoutTests method startDeferredResultProcessingAfterTimeoutException.
@Test
public void startDeferredResultProcessingAfterTimeoutException() throws Exception {
DeferredResult<Integer> deferredResult = new DeferredResult<>();
final Exception exception = new Exception();
DeferredResultProcessingInterceptor interceptor = new DeferredResultProcessingInterceptor() {
@Override
public <T> boolean handleTimeout(NativeWebRequest request, DeferredResult<T> result) throws Exception {
throw exception;
}
};
this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
this.asyncManager.startDeferredResultProcessing(deferredResult);
AsyncEvent event = null;
this.asyncWebRequest.onTimeout(event);
assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(exception);
assertThat(((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()).isEqualTo("/test");
}
use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.
the class WebAsyncManagerErrorTests method startCallableProcessingErrorAndResumeThroughInterceptor.
@Test
public void startCallableProcessingErrorAndResumeThroughInterceptor() throws Exception {
StubCallable callable = new StubCallable();
CallableProcessingInterceptor interceptor = mock(CallableProcessingInterceptor.class);
Exception e = new Exception();
given(interceptor.handleError(this.asyncWebRequest, callable, e)).willReturn(22);
this.asyncManager.registerCallableInterceptor("errorInterceptor", interceptor);
this.asyncManager.startCallableProcessing(callable);
AsyncEvent event = new AsyncEvent(new MockAsyncContext(this.servletRequest, this.servletResponse), e);
this.asyncWebRequest.onError(event);
assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(22);
assertThat(((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()).isEqualTo("/test");
verify(interceptor).beforeConcurrentHandling(this.asyncWebRequest, callable);
}
use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.
the class OpenEntityManagerInViewTests method testOpenEntityManagerInViewInterceptorAsyncTimeoutScenario.
@Test
public void testOpenEntityManagerInViewInterceptorAsyncTimeoutScenario() throws Exception {
// Initial request thread
OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
interceptor.setEntityManagerFactory(factory);
given(this.factory.createEntityManager()).willReturn(this.manager);
interceptor.preHandle(this.webRequest);
assertThat(TransactionSynchronizationManager.hasResource(this.factory)).isTrue();
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
asyncManager.setTaskExecutor(this.taskExecutor);
asyncManager.setAsyncWebRequest(asyncWebRequest);
asyncManager.startCallableProcessing((Callable<String>) () -> "anything");
this.taskExecutor.await();
assertThat(asyncManager.getConcurrentResult()).as("Concurrent result ").isEqualTo("anything");
interceptor.afterConcurrentHandlingStarted(this.webRequest);
assertThat(TransactionSynchronizationManager.hasResource(this.factory)).isFalse();
// Async request timeout
given(this.manager.isOpen()).willReturn(true);
MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
for (AsyncListener listener : asyncContext.getListeners()) {
listener.onTimeout(new AsyncEvent(asyncContext));
}
for (AsyncListener listener : asyncContext.getListeners()) {
listener.onComplete(new AsyncEvent(asyncContext));
}
verify(this.manager).close();
}
use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.
the class ResponseBodyEmitterReturnValueHandlerTests method responseBodyEmitter.
@Test
public void responseBodyEmitter() throws Exception {
MethodParameter type = on(TestController.class).resolveReturnType(ResponseBodyEmitter.class);
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest);
assertThat(this.request.isAsyncStarted()).isTrue();
assertThat(this.response.getContentAsString()).isEqualTo("");
SimpleBean bean = new SimpleBean();
bean.setId(1L);
bean.setName("Joe");
emitter.send(bean);
emitter.send("\n");
bean.setId(2L);
bean.setName("John");
emitter.send(bean);
emitter.send("\n");
bean.setId(3L);
bean.setName("Jason");
emitter.send(bean);
assertThat(this.response.getContentAsString()).isEqualTo(("{\"id\":1,\"name\":\"Joe\"}\n" + "{\"id\":2,\"name\":\"John\"}\n" + "{\"id\":3,\"name\":\"Jason\"}"));
MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
assertThat(asyncContext.getDispatchedPath()).isNull();
emitter.complete();
assertThat(asyncContext.getDispatchedPath()).isNotNull();
}
use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.
the class StandardServletAsyncWebRequestTests method startAsyncMultipleTimes.
@Test
public void startAsyncMultipleTimes() throws Exception {
this.asyncRequest.startAsync();
this.asyncRequest.startAsync();
this.asyncRequest.startAsync();
// idempotent
this.asyncRequest.startAsync();
MockAsyncContext context = (MockAsyncContext) this.request.getAsyncContext();
assertThat(context).isNotNull();
assertThat(context.getListeners().size()).isEqualTo(1);
}
Aggregations