Search in sources :

Example 21 with AsyncEvent

use of jakarta.servlet.AsyncEvent in project spring-framework by spring-projects.

the class WebAsyncManagerErrorTests method startCallableProcessingErrorAndResumeThroughCallback.

@Test
public void startCallableProcessingErrorAndResumeThroughCallback() throws Exception {
    StubCallable callable = new StubCallable();
    WebAsyncTask<Object> webAsyncTask = new WebAsyncTask<>(callable);
    webAsyncTask.onError(() -> 7);
    this.asyncManager.startCallableProcessing(webAsyncTask);
    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(7);
    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 22 with AsyncEvent

use of jakarta.servlet.AsyncEvent in project spring-framework by spring-projects.

the class WebAsyncManagerErrorTests method startDeferredResultProcessingAfterException.

@Test
public void startDeferredResultProcessingAfterException() throws Exception {
    DeferredResult<Integer> deferredResult = new DeferredResult<>();
    final Exception exception = new Exception();
    DeferredResultProcessingInterceptor interceptor = new DeferredResultProcessingInterceptor() {

        @Override
        public <T> boolean handleError(NativeWebRequest request, DeferredResult<T> result, Throwable t) throws Exception {
            throw exception;
        }
    };
    this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
    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) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 23 with AsyncEvent

use of jakarta.servlet.AsyncEvent in project spring-framework by spring-projects.

the class StandardServletAsyncWebRequestTests method onCompletionHandlerAfterOnCompleteEvent.

@Test
public void onCompletionHandlerAfterOnCompleteEvent() throws Exception {
    Runnable handler = mock(Runnable.class);
    this.asyncRequest.addCompletionHandler(handler);
    this.asyncRequest.startAsync();
    this.asyncRequest.onComplete(new AsyncEvent(this.request.getAsyncContext()));
    verify(handler).run();
    assertThat(this.asyncRequest.isAsyncComplete()).isTrue();
}
Also used : AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 24 with AsyncEvent

use of jakarta.servlet.AsyncEvent 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 25 with AsyncEvent

use of jakarta.servlet.AsyncEvent 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)

Aggregations

AsyncEvent (jakarta.servlet.AsyncEvent)25 Test (org.junit.jupiter.api.Test)22 MockAsyncContext (org.springframework.web.testfixture.servlet.MockAsyncContext)19 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)4 AsyncListener (jakarta.servlet.AsyncListener)3 ServletResponse (jakarta.servlet.ServletResponse)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 ArrayList (java.util.ArrayList)2 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)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 ServletException (jakarta.servlet.ServletException)1 ServletRequest (jakarta.servlet.ServletRequest)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Context (org.apache.catalina.Context)1