public void testAllAsList_error() throws Exception { Error error = new Error("deliberate"); SettableFuture<String> future1 = SettableFuture.create(); ListenableFuture<String> future2 = immediateFuture("results"); ListenableFuture<List<String>> compound = allAsList(ImmutableList.of(future1, future2)); future1.setException(error); try { getDone(compound); fail(); } catch (ExecutionException expected) { assertSame(error, expected.getCause()); } }
public void testAllAsList_immediateFailure() throws Exception { Throwable exception = new Throwable("failed"); ListenableFuture<String> future1 = immediateFailedFuture(exception); ListenableFuture<String> future2 = immediateFuture("results"); ListenableFuture<List<String>> compound = allAsList(ImmutableList.of(future1, future2)); try { getDone(compound); fail(); } catch (ExecutionException expected) { assertSame(exception, expected.getCause()); } }
@GwtIncompatible // reflection private static void runGetIdempotencyTest( Future<Integer> transformedFuture, Class<? extends Throwable> expectedExceptionClass) throws Throwable { for (int i = 0; i < 5; i++) { try { getDone(transformedFuture); fail(); } catch (ExecutionException expected) { if (!expectedExceptionClass.isInstance(expected.getCause())) { throw expected.getCause(); } } } }
@GwtIncompatible // get() timeout public void testTransformAsync_asyncFunction_timeout() throws InterruptedException, ExecutionException { AsyncFunction<String, Integer> function = constantAsyncFunction(immediateFuture(1)); ListenableFuture<Integer> future = transformAsync(SettableFuture.<String>create(), function, directExecutor()); try { future.get(1, MILLISECONDS); fail(); } catch (TimeoutException expected) { } }
public void testAllAsList_singleFailure() throws Exception { Throwable exception = new Throwable("failed"); ListenableFuture<String> future = immediateFailedFuture(exception); ListenableFuture<List<String>> compound = allAsList(ImmutableList.of(future)); try { getDone(compound); fail(); } catch (ExecutionException expected) { assertSame(exception, expected.getCause()); } }
public void testTransform_rejectionPropagatesToOutput() throws Exception { SettableFuture<Foo> input = SettableFuture.create(); Function<Foo, Foo> identity = identity(); ListenableFuture<Foo> transformed = transform(input, identity, REJECTING_EXECUTOR); input.set(new Foo()); try { getDone(transformed); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); } }
public void testCatching_rejectionPropagatesToOutput() throws Exception { SettableFuture<String> input = SettableFuture.create(); ListenableFuture<String> transformed = catching(input, Throwable.class, constant("foo"), REJECTING_EXECUTOR); input.setException(new Exception()); try { getDone(transformed); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); } }
public void testTransform_getThrowsError() throws Exception { ListenableFuture<Object> input = UncheckedThrowingFuture.throwingError(new MyError()); ListenableFuture<Object> output = transform(input, identity(), directExecutor()); try { getDone(output); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyError.class); } }
public void testTransform_getThrowsRuntimeException() throws Exception { ListenableFuture<Object> input = UncheckedThrowingFuture.throwingRuntimeException(new MyRuntimeException()); ListenableFuture<Object> output = transform(input, identity(), directExecutor()); try { getDone(output); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyRuntimeException.class); } }
public void testTransformAsync_rejectionPropagatesToOutput() throws Exception { SettableFuture<Foo> input = SettableFuture.create(); AsyncFunction<Foo, Foo> asyncIdentity = asyncIdentity(); ListenableFuture<Foo> transformed = transformAsync(input, asyncIdentity, REJECTING_EXECUTOR); input.set(new Foo()); try { getDone(transformed); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); } }
public void testNonCancellationPropagating_failure() throws Exception { SettableFuture<Foo> input = SettableFuture.create(); ListenableFuture<Foo> wrapper = nonCancellationPropagating(input); Throwable failure = new Throwable("thrown"); assertFalse(wrapper.isDone()); input.setException(failure); try { getDone(wrapper); fail(); } catch (ExecutionException expected) { assertSame(failure, expected.getCause()); } }
/** Ensure that errors are always logged. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_error() throws Exception { try { getDone(allAsList(immediateFailedFuture(new MyError()))); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyError.class); List<LogRecord> logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // errors are always logged assertThat(logged.get(0).getThrown()).isInstanceOf(MyError.class); } }
/** A single non-error failure is not logged because it is reported via the output future. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_exception() throws Exception { try { getDone(allAsList(immediateFailedFuture(new MyException()))); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyException.class); assertEquals( "Nothing should be logged", 0, aggregateFutureLogHandler.getStoredLogRecords().size()); } }
@GwtIncompatible // non-Throwable exceptionType public void testCatching_customTypeNoMatch() throws Exception { Function<IOException, Integer> fallback = functionReturningOne(); ListenableFuture<Integer> originalFuture = immediateFailedFuture(new RuntimeException()); ListenableFuture<Integer> faultTolerantFuture = catching(originalFuture, IOException.class, fallback, directExecutor()); try { getDone(faultTolerantFuture); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(RuntimeException.class); } }
@GwtIncompatible // non-Throwable exceptionType public void testCatchingAsync_customTypeNoMatch() throws Exception { AsyncFunction<IOException, Integer> fallback = asyncFunctionReturningOne(); ListenableFuture<Integer> originalFuture = immediateFailedFuture(new RuntimeException()); ListenableFuture<Integer> faultTolerantFuture = catchingAsync(originalFuture, IOException.class, fallback, directExecutor()); try { getDone(faultTolerantFuture); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(RuntimeException.class); } }
public void testSubmitAsync_asyncCallable_nullInsteadOfFuture() throws Exception { ListenableFuture<?> chainedFuture = submitAsync(constantAsyncCallable(null), directExecutor()); try { getDone(chainedFuture); fail(); } catch (ExecutionException expected) { NullPointerException cause = (NullPointerException) expected.getCause(); assertThat(cause) .hasMessageThat() .contains( "AsyncCallable.call returned null instead of a Future. " + "Did you mean to return immediateFuture(null)?"); } }
public void testTransformAsync_getThrowsRuntimeException() throws Exception { ListenableFuture<Object> input = UncheckedThrowingFuture.throwingRuntimeException(new MyRuntimeException()); ListenableFuture<Object> output = transformAsync(input, asyncIdentity(), directExecutor()); try { getDone(output); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyRuntimeException.class); } }
public void testTransformAsync_getThrowsError() throws Exception { ListenableFuture<Object> input = UncheckedThrowingFuture.throwingError(new MyError()); ListenableFuture<Object> output = transformAsync(input, asyncIdentity(), directExecutor()); try { getDone(output); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyError.class); } }
/** The same exception happening on multiple futures should not be logged. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_same_exception() throws Exception { try { MyException sameInstance = new MyException(); getDone(allAsList(immediateFailedFuture(sameInstance), immediateFailedFuture(sameInstance))); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyException.class); assertEquals( "Nothing should be logged", 0, aggregateFutureLogHandler.getStoredLogRecords().size()); } }
/** All as list will log extra exceptions that have already occurred. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_multipleExceptions_alreadyDone() throws Exception { try { getDone( allAsList( immediateFailedFuture(new MyException()), immediateFailedFuture(new MyException()))); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyException.class); List<LogRecord> logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // the second failure is logged assertThat(logged.get(0).getThrown()).isInstanceOf(MyException.class); } }