use of org.springframework.mock.web.test.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);
assertTrue(TransactionSynchronizationManager.hasResource(this.factory));
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
asyncManager.setTaskExecutor(new SyncTaskExecutor());
asyncManager.setAsyncWebRequest(asyncWebRequest);
asyncManager.startCallableProcessing(new Callable<String>() {
@Override
public String call() throws Exception {
return "anything";
}
});
interceptor.afterConcurrentHandlingStarted(this.webRequest);
assertFalse(TransactionSynchronizationManager.hasResource(this.factory));
// 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.mock.web.test.MockAsyncContext in project spring-framework by spring-projects.
the class ServerHttpRequestTests method createHttpRequest.
private ServerHttpRequest createHttpRequest(String path) throws Exception {
HttpServletRequest request = new MockHttpServletRequest("GET", path) {
@Override
public ServletInputStream getInputStream() {
return new TestServletInputStream();
}
};
AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse());
return new ServletServerHttpRequest(request, asyncContext, new DefaultDataBufferFactory(), 1024);
}
use of org.springframework.mock.web.test.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();
assertNotNull(context);
assertEquals("Timeout value not set", 44 * 1000, context.getTimeout());
assertEquals(1, context.getListeners().size());
assertSame(this.asyncRequest, context.getListeners().get(0));
}
use of org.springframework.mock.web.test.MockAsyncContext in project spring-framework by spring-projects.
the class StandardServletAsyncWebRequestTests method onTimeoutHandler.
@Test
public void onTimeoutHandler() throws Exception {
Runnable timeoutHandler = mock(Runnable.class);
this.asyncRequest.addTimeoutHandler(timeoutHandler);
this.asyncRequest.onTimeout(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
verify(timeoutHandler).run();
}
use of org.springframework.mock.web.test.MockAsyncContext in project spring-framework by spring-projects.
the class StandardServletAsyncWebRequestTests method onTimeoutDefaultBehavior.
@Test
public void onTimeoutDefaultBehavior() throws Exception {
this.asyncRequest.onTimeout(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
assertEquals(200, this.response.getStatus());
}
Aggregations