public final <X extends Throwable> FluentFuture<V> catching( Class<X> exceptionType, Function<? super X, ? extends V> fallback, Executor executor) { return (FluentFuture<V>) Futures.catching(this, exceptionType, fallback, executor);
@Override public FluentFuture<V> catching(Function<Throwable, V> fallback) { return from(Futures.catching(this, Throwable.class, fallback, executor)); }
public void testCatching_inputDoesNotRaiseException() throws Exception { Function<Throwable, Integer> fallback = unexpectedFunction(); ListenableFuture<Integer> originalFuture = immediateFuture(7); ListenableFuture<Integer> faultTolerantFuture = catching(originalFuture, Throwable.class, fallback, directExecutor()); assertEquals(7, getDone(faultTolerantFuture).intValue()); }
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 testCatching_ErrorAfterCancellation() throws Exception { class Fallback implements Function<Throwable, Object> { ListenableFuture<Object> output; @Override public Object apply(Throwable input) { output.cancel(false); throw new MyError(); } } Fallback fallback = new Fallback(); SettableFuture<Object> input = SettableFuture.create(); ListenableFuture<Object> output = catching(input, Throwable.class, fallback, directExecutor()); fallback.output = output; input.setException(new MyException()); assertTrue(output.isCancelled()); }
public void testCatching_ExceptionAfterCancellation() throws Exception { class Fallback implements Function<Throwable, Object> { ListenableFuture<Object> output; @Override public Object apply(Throwable input) { output.cancel(false); throw new MyRuntimeException(); } } Fallback fallback = new Fallback(); SettableFuture<Object> input = SettableFuture.create(); ListenableFuture<Object> output = catching(input, Throwable.class, fallback, directExecutor()); fallback.output = output; input.setException(new MyException()); assertTrue(output.isCancelled()); }
public void testCatching_listenerThrowsError() throws Exception { SettableFuture<Object> input = SettableFuture.create(); ListenableFuture<Object> output = catching(input, Throwable.class, identity(), directExecutor()); output.addListener( new Runnable() { @Override public void run() { throw new MyError(); } }, directExecutor()); try { input.setException(new MyException()); fail(); } catch (MyError expected) { } }
public void testCatching_Throwable() throws Exception { Function<Throwable, Integer> fallback = functionReturningOne(); ListenableFuture<Integer> originalFuture = immediateFailedFuture(new IOException()); ListenableFuture<Integer> faultTolerantFuture = catching(originalFuture, Throwable.class, fallback, directExecutor()); assertEquals(1, (int) getDone(faultTolerantFuture)); }
public void testCatching_fallbackGeneratesError() throws Exception { final Error error = new Error("deliberate"); Function<Throwable, Integer> fallback = new Function<Throwable, Integer>() { @Override public Integer apply(Throwable t) { throw error; } }; ListenableFuture<Integer> failingFuture = immediateFailedFuture(new RuntimeException()); try { getDone(catching(failingFuture, Throwable.class, fallback, directExecutor())); fail(); } catch (ExecutionException expected) { assertSame(error, expected.getCause()); } }
@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 // StackOverflowError public void testCatching_StackOverflow() throws Exception { { /* * Initialize all relevant classes before running the test, which may otherwise poison any * classes it is trying to load during its stack overflow. */ SettableFuture<Object> root = SettableFuture.create(); ListenableFuture<Object> unused = catching(root, MyException.class, identity(), directExecutor()); root.setException(new MyException()); } SettableFuture<Object> root = SettableFuture.create(); ListenableFuture<Object> output = root; for (int i = 0; i < 10000; i++) { output = catching(output, MyException.class, identity(), directExecutor()); } try { root.setException(new MyException()); fail(); } catch (StackOverflowError expected) { } }
@GwtIncompatible // non-Throwable exceptionType public void testCatching_customTypeMatch() throws Exception { Function<IOException, Integer> fallback = functionReturningOne(); ListenableFuture<Integer> originalFuture = immediateFailedFuture(new FileNotFoundException()); ListenableFuture<Integer> faultTolerantFuture = catching(originalFuture, IOException.class, fallback, directExecutor()); assertEquals(1, (int) getDone(faultTolerantFuture)); }
public void testCatching_getThrowsRuntimeException() throws Exception { ListenableFuture<Object> input = UncheckedThrowingFuture.throwingRuntimeException(new MyRuntimeException()); // We'd catch only MyRuntimeException.class here, but then the test won't compile under GWT. ListenableFuture<Object> output = catching(input, Throwable.class, identity(), directExecutor()); assertThat(getDone(output)).isInstanceOf(MyRuntimeException.class); }
public void testCatching_getThrowsError() throws Exception { ListenableFuture<Object> input = UncheckedThrowingFuture.throwingError(new MyError()); // We'd catch only MyError.class here, but then the test won't compile under GWT. ListenableFuture<Object> output = catching(input, Throwable.class, identity(), directExecutor()); assertThat(getDone(output)).isInstanceOf(MyError.class); }
public void testCatching_inputRaisesException() throws Exception { final RuntimeException raisedException = new RuntimeException(); FunctionSpy<Throwable, Integer> fallback = spy( new Function<Throwable, Integer>() { @Override public Integer apply(Throwable t) { assertThat(t).isSameAs(raisedException); return 20; } }); ListenableFuture<Integer> failingFuture = immediateFailedFuture(raisedException); ListenableFuture<Integer> faultTolerantFuture = catching(failingFuture, Throwable.class, fallback, directExecutor()); assertEquals(20, getDone(faultTolerantFuture).intValue()); fallback.verifyCallCount(1); }
public void testCatching_resultCancelledBeforeFallback() throws Exception { SettableFuture<Integer> primary = SettableFuture.create(); Function<Throwable, Integer> fallback = unexpectedFunction(); ListenableFuture<Integer> derived = catching(primary, Throwable.class, fallback, directExecutor()); derived.cancel(false); assertTrue(primary.isCancelled()); assertFalse(primary.wasInterrupted()); }
@Nonnull @Override public ListenableFuture<byte[]> submit(@Nonnull ServerQueryRequest queryRequest) { ListenableFuture<DataTable> dataTable = resourceManager.getQueryRunners().submit(() -> { throw new RuntimeException("query processing error"); }); ListenableFuture<DataTable> queryResponse = Futures.catching(dataTable, Throwable.class, input -> { DataTable result = new DataTableImplV2(); result.addException(QueryException.INTERNAL_ERROR); return result; }); return serializeData(queryResponse); }
public void testCatching_resultInterruptedBeforeFallback() throws Exception { SettableFuture<Integer> primary = SettableFuture.create(); Function<Throwable, Integer> fallback = unexpectedFunction(); ListenableFuture<Integer> derived = catching(primary, Throwable.class, fallback, directExecutor()); derived.cancel(true); assertTrue(primary.isCancelled()); assertTrue(primary.wasInterrupted()); }
@GwtIncompatible // non-Throwable exceptionType public void testCatching_inputCancelledWithoutFallback() throws Exception { Function<IOException, Integer> fallback = unexpectedFunction(); ListenableFuture<Integer> originalFuture = immediateCancelledFuture(); ListenableFuture<Integer> faultTolerantFuture = catching(originalFuture, IOException.class, fallback, directExecutor()); assertTrue(faultTolerantFuture.isCancelled()); }
private void runExpectedExceptionCatchingTest(final RuntimeException expectedException) throws Exception { FunctionSpy<Throwable, Integer> fallback = spy( new Function<Throwable, Integer>() { @Override public Integer apply(Throwable t) { throw expectedException; } }); ListenableFuture<Integer> failingFuture = immediateFailedFuture(new RuntimeException()); ListenableFuture<Integer> faultTolerantFuture = catching(failingFuture, Throwable.class, fallback, directExecutor()); try { getDone(faultTolerantFuture); fail(); } catch (ExecutionException expected) { assertSame(expectedException, expected.getCause()); } fallback.verifyCallCount(1); }