Search in sources :

Example 1 with MockAsyncContext

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();
}
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 2 with MockAsyncContext

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);
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) Test(org.junit.jupiter.api.Test)

Example 3 with MockAsyncContext

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);
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 4 with MockAsyncContext

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");
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 5 with MockAsyncContext

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");
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Aggregations

MockAsyncContext (org.springframework.web.testfixture.servlet.MockAsyncContext)23 Test (org.junit.jupiter.api.Test)22 AsyncEvent (jakarta.servlet.AsyncEvent)19 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)4 AsyncListener (jakarta.servlet.AsyncListener)2 AsyncWebRequest (org.springframework.web.context.request.async.AsyncWebRequest)2 StandardServletAsyncWebRequest (org.springframework.web.context.request.async.StandardServletAsyncWebRequest)2 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)2 AsyncContext (jakarta.servlet.AsyncContext)1 URI (java.net.URI)1 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)1 MethodParameter (org.springframework.core.MethodParameter)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1