Search in sources :

Example 1 with MockAsyncContext

use of org.springframework.mock.web.test.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);
    assertTrue(TransactionSynchronizationManager.hasResource(this.factory));
    AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
    asyncManager.setTaskExecutor(new SyncTaskExecutor());
    asyncManager.setAsyncWebRequest(asyncWebRequest);
    asyncManager.startCallableProcessing(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return "anything";
        }
    });
    interceptor.afterConcurrentHandlingStarted(this.webRequest);
    assertFalse(TransactionSynchronizationManager.hasResource(this.factory));
    // 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(javax.servlet.AsyncListener) MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) AsyncEvent(javax.servlet.AsyncEvent) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with MockAsyncContext

use of org.springframework.mock.web.test.MockAsyncContext in project spring-framework by spring-projects.

the class ServerHttpRequestTests method createHttpRequest.

private ServerHttpRequest createHttpRequest(String path) throws Exception {
    HttpServletRequest request = new MockHttpServletRequest("GET", path) {

        @Override
        public ServletInputStream getInputStream() {
            return new TestServletInputStream();
        }
    };
    AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse());
    return new ServletServerHttpRequest(request, asyncContext, new DefaultDataBufferFactory(), 1024);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) AsyncContext(javax.servlet.AsyncContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Example 3 with MockAsyncContext

use of org.springframework.mock.web.test.MockAsyncContext in project spring-framework by spring-projects.

the class StandardServletAsyncWebRequestTests method startAsync.

@Test
public void startAsync() throws Exception {
    this.asyncRequest.startAsync();
    MockAsyncContext context = (MockAsyncContext) this.request.getAsyncContext();
    assertNotNull(context);
    assertEquals("Timeout value not set", 44 * 1000, context.getTimeout());
    assertEquals(1, context.getListeners().size());
    assertSame(this.asyncRequest, context.getListeners().get(0));
}
Also used : MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) Test(org.junit.Test)

Example 4 with MockAsyncContext

use of org.springframework.mock.web.test.MockAsyncContext 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.mock.web.test.MockAsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 5 with MockAsyncContext

use of org.springframework.mock.web.test.MockAsyncContext 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)));
    assertEquals(200, this.response.getStatus());
}
Also used : MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Aggregations

MockAsyncContext (org.springframework.mock.web.test.MockAsyncContext)8 Test (org.junit.Test)7 AsyncEvent (javax.servlet.AsyncEvent)4 IOException (java.io.IOException)1 AsyncContext (javax.servlet.AsyncContext)1 AsyncListener (javax.servlet.AsyncListener)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 MethodParameter (org.springframework.core.MethodParameter)1 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)1 AsyncWebRequest (org.springframework.web.context.request.async.AsyncWebRequest)1 StandardServletAsyncWebRequest (org.springframework.web.context.request.async.StandardServletAsyncWebRequest)1 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)1