Search in sources :

Example 11 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 12 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 13 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 14 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 15 with AsyncEvent

use of jakarta.servlet.AsyncEvent in project tomcat by apache.

the class TestAsyncContextImpl method testAsyncListenerSupplyRequestResponse.

@Test
public void testAsyncListenerSupplyRequestResponse() {
    final ServletRequest servletRequest = EasyMock.createMock(ServletRequest.class);
    final ServletResponse servletResponse = EasyMock.createMock(ServletResponse.class);
    final AsyncListener listener = new AsyncListener() {

        @Override
        public void onTimeout(AsyncEvent event) throws IOException {
            checkRequestResponse(event);
        }

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {
            checkRequestResponse(event);
        }

        @Override
        public void onError(AsyncEvent event) throws IOException {
            checkRequestResponse(event);
        }

        @Override
        public void onComplete(AsyncEvent event) throws IOException {
            checkRequestResponse(event);
        }

        private void checkRequestResponse(AsyncEvent event) {
            Assert.assertEquals(servletRequest, event.getSuppliedRequest());
            Assert.assertEquals(servletResponse, event.getSuppliedResponse());
        }
    };
    final Context context = new TesterContext();
    final Response response = new Response();
    final Request request = new Request(null);
    request.setCoyoteRequest(new org.apache.coyote.Request());
    request.getMappingData().context = context;
    final AsyncContextImpl ac = new AsyncContextImpl(request);
    ac.addListener(listener, servletRequest, servletResponse);
    ac.setStarted(context, request, response, true);
    ac.addListener(listener, servletRequest, servletResponse);
    ac.setErrorState(new Exception(), true);
    ac.fireOnComplete();
}
Also used : AsyncContext(jakarta.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) ServletRequest(jakarta.servlet.ServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Request(org.apache.catalina.connector.Request) ServletRequest(jakarta.servlet.ServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) AsyncEvent(jakarta.servlet.AsyncEvent) URISyntaxException(java.net.URISyntaxException) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) ServletResponse(jakarta.servlet.ServletResponse) Response(org.apache.catalina.connector.Response) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AsyncListener(jakarta.servlet.AsyncListener) TesterContext(org.apache.tomcat.unittest.TesterContext) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.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