@SuppressWarnings("unchecked") @Test public void startCallableProcessingTimeoutAndCheckThreadInterrupted() throws Exception { StubCallable callable = new StubCallable(); Future future = mock(Future.class); AsyncTaskExecutor executor = mock(AsyncTaskExecutor.class); when(executor.submit(any(Runnable.class))).thenReturn(future); this.asyncManager.setTaskExecutor(executor); this.asyncManager.startCallableProcessing(callable); this.asyncWebRequest.onTimeout(ASYNC_EVENT); assertTrue(this.asyncManager.hasConcurrentResult()); verify(future).cancel(true); verifyNoMoreInteractions(future); }
@Test public void startCallableProcessingTimeoutAndResumeThroughCallback() throws Exception { StubCallable callable = new StubCallable(); WebAsyncTask<Object> webAsyncTask = new WebAsyncTask<>(callable); webAsyncTask.onTimeout(new Callable<Object>() { @Override public Object call() throws Exception { return 7; } }); this.asyncManager.startCallableProcessing(webAsyncTask); this.asyncWebRequest.onTimeout(ASYNC_EVENT); assertTrue(this.asyncManager.hasConcurrentResult()); assertEquals(7, this.asyncManager.getConcurrentResult()); assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()); }
@Test public void startCallableProcessingTimeoutAndComplete() throws Exception { StubCallable callable = new StubCallable(); CallableProcessingInterceptor interceptor = mock(CallableProcessingInterceptor.class); given(interceptor.handleTimeout(this.asyncWebRequest, callable)).willReturn(RESULT_NONE); this.asyncManager.registerCallableInterceptor("interceptor", interceptor); this.asyncManager.startCallableProcessing(callable); this.asyncWebRequest.onTimeout(ASYNC_EVENT); this.asyncWebRequest.onComplete(ASYNC_EVENT); assertTrue(this.asyncManager.hasConcurrentResult()); assertEquals(AsyncRequestTimeoutException.class, this.asyncManager.getConcurrentResult().getClass()); verify(interceptor).beforeConcurrentHandling(this.asyncWebRequest, callable); verify(interceptor).afterCompletion(this.asyncWebRequest, callable); }
@Test public void startCallableProcessingAfterTimeoutException() throws Exception { StubCallable callable = new StubCallable(); Exception exception = new Exception(); CallableProcessingInterceptor interceptor = mock(CallableProcessingInterceptor.class); given(interceptor.handleTimeout(this.asyncWebRequest, callable)).willThrow(exception); this.asyncManager.registerCallableInterceptor("timeoutInterceptor", interceptor); this.asyncManager.startCallableProcessing(callable); this.asyncWebRequest.onTimeout(ASYNC_EVENT); assertTrue(this.asyncManager.hasConcurrentResult()); assertEquals(exception, this.asyncManager.getConcurrentResult()); assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()); verify(interceptor).beforeConcurrentHandling(this.asyncWebRequest, callable); }
@Test public void startCallableProcessingTimeoutAndResumeThroughInterceptor() throws Exception { StubCallable callable = new StubCallable(); CallableProcessingInterceptor interceptor = mock(CallableProcessingInterceptor.class); given(interceptor.handleTimeout(this.asyncWebRequest, callable)).willReturn(22); this.asyncManager.registerCallableInterceptor("timeoutInterceptor", interceptor); this.asyncManager.startCallableProcessing(callable); this.asyncWebRequest.onTimeout(ASYNC_EVENT); assertTrue(this.asyncManager.hasConcurrentResult()); assertEquals(22, this.asyncManager.getConcurrentResult()); assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath()); verify(interceptor).beforeConcurrentHandling(this.asyncWebRequest, callable); }