use of org.springframework.web.context.request.async.AsyncWebRequest 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.context.request.async.AsyncWebRequest in project spring-framework by spring-projects.
the class DeferredResultReturnValueHandlerTests method setup.
@BeforeEach
public void setup() throws Exception {
this.handler = new DeferredResultMethodReturnValueHandler();
this.request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
this.webRequest = new ServletWebRequest(this.request, response);
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, response);
WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
this.request.setAsyncSupported(true);
}
use of org.springframework.web.context.request.async.AsyncWebRequest in project spring-framework by spring-projects.
the class ResponseBodyEmitterReturnValueHandlerTests method responseBodyEmitterWithErrorValue.
@SuppressWarnings("unchecked")
@Test
public void responseBodyEmitterWithErrorValue() throws Exception {
AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
WebAsyncUtils.getAsyncManager(this.request).setAsyncWebRequest(asyncWebRequest);
ResponseBodyEmitter emitter = new ResponseBodyEmitter(19000L);
emitter.onError(mock(Consumer.class));
emitter.onCompletion(mock(Runnable.class));
MethodParameter type = on(TestController.class).resolveReturnType(ResponseBodyEmitter.class);
this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest);
verify(asyncWebRequest).addErrorHandler(any(Consumer.class));
verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
verify(asyncWebRequest).startAsync();
}
use of org.springframework.web.context.request.async.AsyncWebRequest in project spring-framework by spring-projects.
the class HandlerFunctionAdapter method getWebAsyncManager.
private WebAsyncManager getWebAsyncManager(HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(servletRequest, servletResponse);
asyncWebRequest.setTimeout(this.asyncRequestTimeout);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(servletRequest);
asyncManager.setAsyncWebRequest(asyncWebRequest);
return asyncManager;
}
use of org.springframework.web.context.request.async.AsyncWebRequest in project spring-framework by spring-projects.
the class DefaultAsyncServerResponse method writeAsync.
static void writeAsync(HttpServletRequest request, HttpServletResponse response, DeferredResult<?> deferredResult) throws ServletException, IOException {
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
AsyncWebRequest asyncWebRequest = WebAsyncUtils.createAsyncWebRequest(request, response);
asyncManager.setAsyncWebRequest(asyncWebRequest);
try {
asyncManager.startDeferredResultProcessing(deferredResult);
} catch (IOException | ServletException ex) {
throw ex;
} catch (Exception ex) {
throw new ServletException("Async processing failed", ex);
}
}
Aggregations