ThreadAsyncExecutor implements AsyncExecutor { @Override public <T> AsyncResult<T> startProcess(Callable<T> task) { return startProcess(task, null); }  @Override AsyncResult<T> startProcess(Callable<T> task); @Override AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback); @Override T endProcess(AsyncResult<T> asyncResult);  }
@Test(timeout = 3000) public void testSuccessfulTaskWithoutCallback() throws Exception { final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); final Object result = new Object(); final Callable<Object> task = mock(Callable.class); when(task.call()).thenReturn(result); final AsyncResult<Object> asyncResult = executor.startProcess(task); assertNotNull(asyncResult); asyncResult.await(); assertTrue(asyncResult.isCompleted()); verify(task, times(1)).call(); assertSame(result, asyncResult.getValue()); }
@Test(timeout = 3000) public void testSuccessfulTaskWithCallback() throws Exception { final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); final Object result = new Object(); final Callable<Object> task = mock(Callable.class); when(task.call()).thenReturn(result); final AsyncCallback callback = mock(AsyncCallback.class); final AsyncResult<Object> asyncResult = executor.startProcess(task, callback); assertNotNull(asyncResult); asyncResult.await(); assertTrue(asyncResult.isCompleted()); verify(task, times(1)).call(); final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class); verify(callback, times(1)).onComplete(eq(result), optionalCaptor.capture()); final Optional<Exception> optionalException = optionalCaptor.getValue(); assertNotNull(optionalException); assertFalse(optionalException.isPresent()); assertSame(result, asyncResult.getValue()); }
@Test(timeout = 5000) public void testLongRunningTaskWithoutCallback() throws Exception { final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); final Object result = new Object(); final Callable<Object> task = mock(Callable.class); when(task.call()).thenAnswer(i -> { Thread.sleep(1500); return result; }); final AsyncResult<Object> asyncResult = executor.startProcess(task); assertNotNull(asyncResult); assertFalse(asyncResult.isCompleted()); try { asyncResult.getValue(); fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task"); } catch (IllegalStateException e) { assertNotNull(e.getMessage()); } verify(task, timeout(3000).times(1)).call(); asyncResult.await(); assertTrue(asyncResult.isCompleted()); verifyNoMoreInteractions(task); assertSame(result, asyncResult.getValue()); }
@Test(timeout = 5000) public void testLongRunningTaskWithCallback() throws Exception { final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); final Object result = new Object(); final Callable<Object> task = mock(Callable.class); when(task.call()).thenAnswer(i -> { Thread.sleep(1500); return result; }); final AsyncCallback<Object> callback = mock(AsyncCallback.class); final AsyncResult<Object> asyncResult = executor.startProcess(task, callback); assertNotNull(asyncResult); assertFalse(asyncResult.isCompleted()); verifyZeroInteractions(callback); try { asyncResult.getValue(); fail("Expected IllegalStateException when calling AsyncResult#getValue on a non-completed task"); } catch (IllegalStateException e) { assertNotNull(e.getMessage()); } verify(task, timeout(3000).times(1)).call(); final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class); verify(callback, timeout(3000).times(1)).onComplete(eq(result), optionalCaptor.capture()); final Optional<Exception> optionalException = optionalCaptor.getValue(); assertNotNull(optionalException); assertFalse(optionalException.isPresent()); asyncResult.await(); assertTrue(asyncResult.isCompleted()); verifyNoMoreInteractions(task, callback); assertSame(result, asyncResult.getValue()); }
@Test(timeout = 3000) public void testNullTask() throws Exception { final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); final AsyncResult<Object> asyncResult = executor.startProcess(null); assertNotNull("The AsyncResult should not be 'null', even though the task was 'null'.", asyncResult); asyncResult.await(); assertTrue(asyncResult.isCompleted()); try { asyncResult.getValue(); fail("Expected ExecutionException with NPE as cause"); } catch (final ExecutionException e) { assertNotNull(e.getMessage()); assertNotNull(e.getCause()); assertEquals(NullPointerException.class, e.getCause().getClass()); } }
@Test(timeout = 3000) public void testNullTaskWithCallback() throws Exception { final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); final AsyncCallback<Object> callback = mock(AsyncCallback.class); final AsyncResult<Object> asyncResult = executor.startProcess(null, callback); assertNotNull("The AsyncResult should not be 'null', even though the task was 'null'.", asyncResult); asyncResult.await(); assertTrue(asyncResult.isCompleted()); final ArgumentCaptor<Optional<Exception>> optionalCaptor = ArgumentCaptor.forClass((Class) Optional.class); verify(callback, times(1)).onComplete(Matchers.isNull(), optionalCaptor.capture()); final Optional<Exception> optionalException = optionalCaptor.getValue(); assertNotNull(optionalException); assertTrue(optionalException.isPresent()); final Exception exception = optionalException.get(); assertNotNull(exception); assertEquals(NullPointerException.class, exception.getClass()); try { asyncResult.getValue(); fail("Expected ExecutionException with NPE as cause"); } catch (final ExecutionException e) { assertNotNull(e.getMessage()); assertNotNull(e.getCause()); assertEquals(NullPointerException.class, e.getCause().getClass()); } }
@Test(timeout = 3000) public void testNullTaskWithNullCallback() throws Exception { final ThreadAsyncExecutor executor = new ThreadAsyncExecutor(); final AsyncResult<Object> asyncResult = executor.startProcess(null, null); assertNotNull("The AsyncResult should not be 'null', even though the task and callback were 'null'.", asyncResult); asyncResult.await(); assertTrue(asyncResult.isCompleted()); try { asyncResult.getValue(); fail("Expected ExecutionException with NPE as cause"); } catch (final ExecutionException e) { assertNotNull(e.getMessage()); assertNotNull(e.getCause()); assertEquals(NullPointerException.class, e.getCause().getClass()); } }