@Async public Future<String> returnSomething(int i) { assertTrue(!Thread.currentThread().getName().equals(originalThreadName)); if (i == 0) { throw new IllegalArgumentException(); } else if (i < 0) { return AsyncResult.forExecutionException(new IOException()); } return AsyncResult.forValue(Integer.toString(i)); }
@Async public ListenableFuture<String> returnSomethingListenable(int i) { assertTrue(!Thread.currentThread().getName().equals(originalThreadName)); if (i == 0) { throw new IllegalArgumentException(); } else if (i < 0) { return AsyncResult.forExecutionException(new IOException()); } return new AsyncResult<>(Integer.toString(i)); }
@Test public void asyncResultWithSeparateCallbacksAndException() throws Exception { IOException ex = new IOException(); final Set<Throwable> values = new HashSet<>(1); ListenableFuture<String> future = AsyncResult.forExecutionException(ex); future.addCallback((result) -> fail("Success callback not expected: " + result), values::add); assertSame(ex, values.iterator().next()); try { future.get(); fail("Should have thrown ExecutionException"); } catch (ExecutionException ex2) { assertSame(ex, ex2.getCause()); } try { future.completable().get(); fail("Should have thrown ExecutionException"); } catch (ExecutionException ex2) { assertSame(ex, ex2.getCause()); } }
IOException ex = new IOException(); final Set<Throwable> values = new HashSet<>(1); ListenableFuture<String> future = AsyncResult.forExecutionException(ex); future.addCallback(new ListenableFutureCallback<String>() { @Override