Search in sources :

Example 11 with MockAsyncContext

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

the class WebAsyncManagerTimeoutTests method startDeferredResultProcessingAfterTimeoutException.

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

        @Override
        public <T> boolean handleTimeout(NativeWebRequest request, DeferredResult<T> result) throws Exception {
            throw exception;
        }
    };
    this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
    this.asyncManager.startDeferredResultProcessing(deferredResult);
    AsyncEvent event = null;
    this.asyncWebRequest.onTimeout(event);
    assertThat(this.asyncManager.hasConcurrentResult()).isTrue();
    assertThat(this.asyncManager.getConcurrentResult()).isEqualTo(exception);
    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 12 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 13 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 14 with MockAsyncContext

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

the class ResponseBodyEmitterReturnValueHandlerTests method responseBodyEmitter.

@Test
public void responseBodyEmitter() throws Exception {
    MethodParameter type = on(TestController.class).resolveReturnType(ResponseBodyEmitter.class);
    ResponseBodyEmitter emitter = new ResponseBodyEmitter();
    this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest);
    assertThat(this.request.isAsyncStarted()).isTrue();
    assertThat(this.response.getContentAsString()).isEqualTo("");
    SimpleBean bean = new SimpleBean();
    bean.setId(1L);
    bean.setName("Joe");
    emitter.send(bean);
    emitter.send("\n");
    bean.setId(2L);
    bean.setName("John");
    emitter.send(bean);
    emitter.send("\n");
    bean.setId(3L);
    bean.setName("Jason");
    emitter.send(bean);
    assertThat(this.response.getContentAsString()).isEqualTo(("{\"id\":1,\"name\":\"Joe\"}\n" + "{\"id\":2,\"name\":\"John\"}\n" + "{\"id\":3,\"name\":\"Jason\"}"));
    MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
    assertThat(asyncContext.getDispatchedPath()).isNull();
    emitter.complete();
    assertThat(asyncContext.getDispatchedPath()).isNotNull();
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 15 with MockAsyncContext

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

the class StandardServletAsyncWebRequestTests method startAsyncMultipleTimes.

@Test
public void startAsyncMultipleTimes() throws Exception {
    this.asyncRequest.startAsync();
    this.asyncRequest.startAsync();
    this.asyncRequest.startAsync();
    // idempotent
    this.asyncRequest.startAsync();
    MockAsyncContext context = (MockAsyncContext) this.request.getAsyncContext();
    assertThat(context).isNotNull();
    assertThat(context.getListeners().size()).isEqualTo(1);
}
Also used : MockAsyncContext(org.springframework.web.testfixture.servlet.MockAsyncContext) 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