use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.
the class StandardServletAsyncWebRequestTests method startAsyncAfterCompleted.
@Test
public void startAsyncAfterCompleted() throws Exception {
this.asyncRequest.onComplete(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
assertThatIllegalStateException().isThrownBy(this.asyncRequest::startAsync).withMessage("Async processing has already completed");
}
use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.
the class WebAsyncManagerErrorTests method startCallableProcessingAfterException.
@Test
public void startCallableProcessingAfterException() throws Exception {
StubCallable callable = new StubCallable();
Exception exception = new Exception();
CallableProcessingInterceptor interceptor = mock(CallableProcessingInterceptor.class);
Exception e = new Exception();
given(interceptor.handleError(this.asyncWebRequest, callable, e)).willThrow(exception);
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(exception);
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 WebAsyncManagerErrorTests method startDeferredResultProcessingErrorAndComplete.
@Test
public void startDeferredResultProcessingErrorAndComplete() throws Exception {
DeferredResult<Integer> deferredResult = new DeferredResult<>();
DeferredResultProcessingInterceptor interceptor = mock(DeferredResultProcessingInterceptor.class);
Exception e = new Exception();
given(interceptor.handleError(this.asyncWebRequest, deferredResult, e)).willReturn(true);
this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
this.asyncManager.startDeferredResultProcessing(deferredResult);
AsyncEvent event = new AsyncEvent(new MockAsyncContext(this.servletRequest, this.servletResponse), e);
this.asyncWebRequest.onError(event);
this.asyncWebRequest.onComplete(event);
assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(e);
verify(interceptor).beforeConcurrentHandling(this.asyncWebRequest, deferredResult);
verify(interceptor).preProcess(this.asyncWebRequest, deferredResult);
verify(interceptor).afterCompletion(this.asyncWebRequest, deferredResult);
}
use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.
the class OpenEntityManagerInViewTests method testOpenEntityManagerInViewInterceptorAsyncErrorScenario.
@Test
public void testOpenEntityManagerInViewInterceptorAsyncErrorScenario() 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 error
given(this.manager.isOpen()).willReturn(true);
MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
for (AsyncListener listener : asyncContext.getListeners()) {
listener.onError(new AsyncEvent(asyncContext, new Exception()));
}
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 WebAsyncManagerErrorTests method startCallableProcessingErrorAndComplete.
@Test
public void startCallableProcessingErrorAndComplete() throws Exception {
StubCallable callable = new StubCallable();
CallableProcessingInterceptor interceptor = mock(CallableProcessingInterceptor.class);
Exception e = new Exception();
given(interceptor.handleError(this.asyncWebRequest, callable, e)).willReturn(RESULT_NONE);
this.asyncManager.registerCallableInterceptor("interceptor", interceptor);
this.asyncManager.startCallableProcessing(callable);
AsyncEvent event = new AsyncEvent(new MockAsyncContext(this.servletRequest, this.servletResponse), e);
this.asyncWebRequest.onError(event);
this.asyncWebRequest.onComplete(event);
assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(e);
verify(interceptor).beforeConcurrentHandling(this.asyncWebRequest, callable);
verify(interceptor).afterCompletion(this.asyncWebRequest, callable);
}
Aggregations