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 StandardServletAsyncWebRequestTests method startAsync.
@Test
public void startAsync() throws Exception {
this.asyncRequest.startAsync();
MockAsyncContext context = (MockAsyncContext) this.request.getAsyncContext();
assertThat(context).isNotNull();
assertThat(context.getTimeout()).as("Timeout value not set").isEqualTo((44 * 1000));
assertThat(context.getListeners().size()).isEqualTo(1);
assertThat(context.getListeners().get(0)).isSameAs(this.asyncRequest);
}
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 WebAsyncManagerErrorTests method startDeferredResultProcessingErrorAndResumeWithDefaultResult.
@Test
public void startDeferredResultProcessingErrorAndResumeWithDefaultResult() throws Exception {
Exception e = new Exception();
DeferredResult<Throwable> deferredResult = new DeferredResult<>(null, e);
this.asyncManager.startDeferredResultProcessing(deferredResult);
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(e);
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 startDeferredResultProcessingErrorAndResumeThroughCallback.
@Test
public void startDeferredResultProcessingErrorAndResumeThroughCallback() throws Exception {
final DeferredResult<Throwable> deferredResult = new DeferredResult<>();
deferredResult.onError(t -> deferredResult.setResult(t));
this.asyncManager.startDeferredResultProcessing(deferredResult);
Exception e = new Exception();
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(e);
assertThat(((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()).isEqualTo("/test");
}
Aggregations