Search in sources :

Example 16 with MockAsyncContext

use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.

the class StandardServletAsyncWebRequestTests method startAsyncAfterCompleted.

@Test
public void startAsyncAfterCompleted() throws Exception {
    this.asyncRequest.onComplete(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
    assertThatIllegalStateException().isThrownBy(this.asyncRequest::startAsync).withMessage("Async processing has already completed");
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 17 with MockAsyncContext

use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.

the class WebAsyncManagerErrorTests method startCallableProcessingAfterException.

@Test
public void startCallableProcessingAfterException() throws Exception {
    StubCallable callable = new StubCallable();
    Exception exception = new Exception();
    CallableProcessingInterceptor interceptor = mock(CallableProcessingInterceptor.class);
    Exception e = new Exception();
    given(interceptor.handleError(this.asyncWebRequest, callable, e)).willThrow(exception);
    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(exception);
    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 18 with MockAsyncContext

use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.

the class WebAsyncManagerErrorTests method startDeferredResultProcessingErrorAndComplete.

@Test
public void startDeferredResultProcessingErrorAndComplete() throws Exception {
    DeferredResult<Integer> deferredResult = new DeferredResult<>();
    DeferredResultProcessingInterceptor interceptor = mock(DeferredResultProcessingInterceptor.class);
    Exception e = new Exception();
    given(interceptor.handleError(this.asyncWebRequest, deferredResult, e)).willReturn(true);
    this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
    this.asyncManager.startDeferredResultProcessing(deferredResult);
    AsyncEvent event = new AsyncEvent(new MockAsyncContext(this.servletRequest, this.servletResponse), e);
    this.asyncWebRequest.onError(event);
    this.asyncWebRequest.onComplete(event);
    assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
    assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(e);
    verify(interceptor).beforeConcurrentHandling(this.asyncWebRequest, deferredResult);
    verify(interceptor).preProcess(this.asyncWebRequest, deferredResult);
    verify(interceptor).afterCompletion(this.asyncWebRequest, deferredResult);
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 19 with MockAsyncContext

use of org.springframework.web.testfixture.servlet.MockAsyncContext 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 20 with MockAsyncContext

use of org.springframework.web.testfixture.servlet.MockAsyncContext in project spring-framework by spring-projects.

the class WebAsyncManagerErrorTests method startCallableProcessingErrorAndComplete.

@Test
public void startCallableProcessingErrorAndComplete() throws Exception {
    StubCallable callable = new StubCallable();
    CallableProcessingInterceptor interceptor = mock(CallableProcessingInterceptor.class);
    Exception e = new Exception();
    given(interceptor.handleError(this.asyncWebRequest, callable, e)).willReturn(RESULT_NONE);
    this.asyncManager.registerCallableInterceptor("interceptor", interceptor);
    this.asyncManager.startCallableProcessing(callable);
    AsyncEvent event = new AsyncEvent(new MockAsyncContext(this.servletRequest, this.servletResponse), e);
    this.asyncWebRequest.onError(event);
    this.asyncWebRequest.onComplete(event);
    assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
    assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(e);
    verify(interceptor).beforeConcurrentHandling(this.asyncWebRequest, callable);
    verify(interceptor).afterCompletion(this.asyncWebRequest, callable);
}
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