Search in sources :

Example 16 with WebAsyncManager

use of org.springframework.web.context.request.async.WebAsyncManager in project spring-framework by spring-projects.

the class OpenEntityManagerInViewTests method testOpenEntityManagerInViewInterceptorAsyncScenario.

@Test
public void testOpenEntityManagerInViewInterceptorAsyncScenario() throws Exception {
    // Initial request thread
    OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
    interceptor.setEntityManagerFactory(factory);
    given(factory.createEntityManager()).willReturn(this.manager);
    interceptor.preHandle(this.webRequest);
    assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
    AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.webRequest);
    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(factory)).isFalse();
    // Async dispatch thread
    interceptor.preHandle(this.webRequest);
    assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
    asyncManager.clearConcurrentResult();
    // check that further invocations simply participate
    interceptor.preHandle(new ServletWebRequest(request));
    interceptor.preHandle(new ServletWebRequest(request));
    interceptor.postHandle(new ServletWebRequest(request), null);
    interceptor.afterCompletion(new ServletWebRequest(request), null);
    interceptor.postHandle(new ServletWebRequest(request), null);
    interceptor.afterCompletion(new ServletWebRequest(request), null);
    interceptor.preHandle(new ServletWebRequest(request));
    interceptor.postHandle(new ServletWebRequest(request), null);
    interceptor.afterCompletion(new ServletWebRequest(request), null);
    interceptor.postHandle(this.webRequest, null);
    assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
    given(this.manager.isOpen()).willReturn(true);
    interceptor.afterCompletion(this.webRequest, null);
    assertThat(TransactionSynchronizationManager.hasResource(factory)).isFalse();
    verify(this.manager).close();
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 17 with WebAsyncManager

use of org.springframework.web.context.request.async.WebAsyncManager 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();
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) AsyncListener(jakarta.servlet.AsyncListener) MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 18 with WebAsyncManager

use of org.springframework.web.context.request.async.WebAsyncManager in project spring-boot by spring-projects.

the class ErrorPageFilterTests method setUpAsyncDispatch.

private void setUpAsyncDispatch() throws Exception {
    this.request.setAsyncSupported(true);
    this.request.setAsyncStarted(true);
    DeferredResult<String> result = new DeferredResult<>();
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
    asyncManager.setAsyncWebRequest(new StandardServletAsyncWebRequest(this.request, this.response));
    asyncManager.startDeferredResultProcessing(result);
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) DeferredResult(org.springframework.web.context.request.async.DeferredResult)

Aggregations

WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)18 AsyncWebRequest (org.springframework.web.context.request.async.AsyncWebRequest)7 Test (org.junit.jupiter.api.Test)6 StandardServletAsyncWebRequest (org.springframework.web.context.request.async.StandardServletAsyncWebRequest)5 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)4 EntityManager (jakarta.persistence.EntityManager)3 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)3 AsyncEvent (jakarta.servlet.AsyncEvent)3 AsyncListener (jakarta.servlet.AsyncListener)3 IOException (java.io.IOException)3 MockAsyncContext (org.springframework.web.testfixture.servlet.MockAsyncContext)3 PersistenceException (jakarta.persistence.PersistenceException)2 ServletException (jakarta.servlet.ServletException)2 Session (org.hibernate.Session)2 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)2 Nullable (org.springframework.lang.Nullable)2 EntityManagerHolder (org.springframework.orm.jpa.EntityManagerHolder)2 NestedServletException (org.springframework.web.util.NestedServletException)2 FilterChain (jakarta.servlet.FilterChain)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1