Search in sources :

Example 11 with AsyncEvent

use of javax.servlet.AsyncEvent 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 12 with AsyncEvent

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

the class WebAsyncManagerTimeoutTests method startDeferredResultProcessingTimeoutAndResumeThroughCallback.

@Test
public void startDeferredResultProcessingTimeoutAndResumeThroughCallback() throws Exception {
    final DeferredResult<Integer> deferredResult = new DeferredResult<>();
    deferredResult.onTimeout(new Runnable() {

        @Override
        public void run() {
            deferredResult.setResult(23);
        }
    });
    this.asyncManager.startDeferredResultProcessing(deferredResult);
    AsyncEvent event = null;
    this.asyncWebRequest.onTimeout(event);
    assertTrue(this.asyncManager.hasConcurrentResult());
    assertEquals(23, this.asyncManager.getConcurrentResult());
    assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
}
Also used : AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 13 with AsyncEvent

use of javax.servlet.AsyncEvent 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 DeferredResultProcessingInterceptorAdapter() {

        @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);
    assertTrue(this.asyncManager.hasConcurrentResult());
    assertEquals(exception, this.asyncManager.getConcurrentResult());
    assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
}
Also used : NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 14 with AsyncEvent

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

the class WebAsyncManagerTimeoutTests method startDeferredResultProcessingTimeoutAndResumeThroughInterceptor.

@Test
public void startDeferredResultProcessingTimeoutAndResumeThroughInterceptor() throws Exception {
    DeferredResult<Integer> deferredResult = new DeferredResult<>();
    DeferredResultProcessingInterceptor interceptor = new DeferredResultProcessingInterceptorAdapter() {

        @Override
        public <T> boolean handleTimeout(NativeWebRequest request, DeferredResult<T> result) throws Exception {
            result.setErrorResult(23);
            return true;
        }
    };
    this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
    this.asyncManager.startDeferredResultProcessing(deferredResult);
    AsyncEvent event = null;
    this.asyncWebRequest.onTimeout(event);
    assertTrue(this.asyncManager.hasConcurrentResult());
    assertEquals(23, this.asyncManager.getConcurrentResult());
    assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
}
Also used : NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 15 with AsyncEvent

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

the class WebAsyncManagerTimeoutTests method startDeferredResultProcessingTimeoutAndResumeWithDefaultResult.

@Test
public void startDeferredResultProcessingTimeoutAndResumeWithDefaultResult() throws Exception {
    DeferredResult<Integer> deferredResult = new DeferredResult<>(null, 23);
    this.asyncManager.startDeferredResultProcessing(deferredResult);
    AsyncEvent event = null;
    this.asyncWebRequest.onTimeout(event);
    assertTrue(this.asyncManager.hasConcurrentResult());
    assertEquals(23, this.asyncManager.getConcurrentResult());
    assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
}
Also used : AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Aggregations

AsyncEvent (javax.servlet.AsyncEvent)39 IOException (java.io.IOException)27 Test (org.junit.Test)21 AsyncListener (javax.servlet.AsyncListener)20 AsyncContext (javax.servlet.AsyncContext)19 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 HttpServletRequest (javax.servlet.http.HttpServletRequest)16 ServletException (javax.servlet.ServletException)12 HttpServlet (javax.servlet.http.HttpServlet)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 OutputStream (java.io.OutputStream)4 Request (org.eclipse.jetty.server.Request)4 Socket (java.net.Socket)3 URISyntaxException (java.net.URISyntaxException)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 ServletResponse (javax.servlet.ServletResponse)3 HttpTester (org.eclipse.jetty.http.HttpTester)3 MockAsyncContext (org.springframework.mock.web.test.MockAsyncContext)3 RequestContext (com.linkedin.r2.message.RequestContext)2