Search in sources :

Example 1 with AsyncEvent

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

the class StandardServletAsyncWebRequestTests method onErrorHandler.

@SuppressWarnings("unchecked")
@Test
public void onErrorHandler() throws Exception {
    Consumer<Throwable> errorHandler = mock(Consumer.class);
    this.asyncRequest.addErrorHandler(errorHandler);
    Exception e = new Exception();
    this.asyncRequest.onError(new AsyncEvent(new MockAsyncContext(this.request, this.response), e));
    verify(errorHandler).accept(e);
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncEvent(jakarta.servlet.AsyncEvent) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Test(org.junit.jupiter.api.Test)

Example 2 with AsyncEvent

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

the class StandardServletAsyncWebRequestTests method onTimeoutDefaultBehavior.

@Test
public void onTimeoutDefaultBehavior() throws Exception {
    this.asyncRequest.onTimeout(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
    assertThat(this.response.getStatus()).isEqualTo(200);
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 3 with AsyncEvent

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

the class StandardServletAsyncWebRequestTests method onTimeoutHandler.

@Test
public void onTimeoutHandler() throws Exception {
    Runnable timeoutHandler = mock(Runnable.class);
    this.asyncRequest.addTimeoutHandler(timeoutHandler);
    this.asyncRequest.onTimeout(new AsyncEvent(new MockAsyncContext(this.request, this.response)));
    verify(timeoutHandler).run();
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) AsyncEvent(jakarta.servlet.AsyncEvent) Test(org.junit.jupiter.api.Test)

Example 4 with AsyncEvent

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

the class StandardServletAsyncWebRequestTests method onCompletionHandler.

@Test
public void onCompletionHandler() 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 5 with AsyncEvent

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

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