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 testCatchingAsync_inputRaisesException() throws Exception { final RuntimeException raisedException = new RuntimeException(); AsyncFunctionSpy<Throwable, Integer> fallback = spy( new AsyncFunction<Throwable, Integer>() { @Override public ListenableFuture<Integer> apply(Throwable t) throws Exception { assertThat(t).isSameAs(raisedException); return immediateFuture(20); } }); ListenableFuture<Integer> failingFuture = immediateFailedFuture(raisedException); ListenableFuture<Integer> faultTolerantFuture = catchingAsync(failingFuture, Throwable.class, fallback, directExecutor()); assertEquals(20, getDone(faultTolerantFuture).intValue()); fallback.verifyCallCount(1); }
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); }
private void runExpectedExceptionCatchingAsyncTest( final Exception expectedException, final boolean wrapInFuture) throws Exception { AsyncFunctionSpy<Throwable, Integer> fallback = spy( new AsyncFunction<Throwable, Integer>() { @Override public ListenableFuture<Integer> apply(Throwable t) throws Exception { if (!wrapInFuture) { throw expectedException; } else { return immediateFailedFuture(expectedException); } } }); ListenableFuture<Integer> failingFuture = immediateFailedFuture(new RuntimeException()); ListenableFuture<Integer> faultTolerantFuture = catchingAsync(failingFuture, Throwable.class, fallback, directExecutor()); try { getDone(faultTolerantFuture); fail(); } catch (ExecutionException expected) { assertSame(expectedException, expected.getCause()); } fallback.verifyCallCount(1); }
@GwtIncompatible // mocks // TODO(cpovirk): eliminate use of mocks @SuppressWarnings("unchecked") public void testCatchingAsync_resultCancelledAfterFallback() throws Exception { final SettableFuture<Integer> secondary = SettableFuture.create(); final RuntimeException raisedException = new RuntimeException(); AsyncFunctionSpy<Throwable, Integer> fallback = spy( new AsyncFunction<Throwable, Integer>() { @Override public ListenableFuture<Integer> apply(Throwable t) throws Exception { assertThat(t).isSameAs(raisedException); return secondary; } }); ListenableFuture<Integer> failingFuture = immediateFailedFuture(raisedException); ListenableFuture<Integer> derived = catchingAsync(failingFuture, Throwable.class, fallback, directExecutor()); derived.cancel(false); assertTrue(secondary.isCancelled()); assertFalse(secondary.wasInterrupted()); fallback.verifyCallCount(1); }
public void testCatchingAsync_inputRaisesException() throws Exception { final RuntimeException raisedException = new RuntimeException(); AsyncFunctionSpy<Throwable, Integer> fallback = spy(new AsyncFunction<Throwable, Integer>() { @Override public ListenableFuture<Integer> apply(Throwable t) throws Exception { assertThat(t).isSameAs(raisedException); return immediateFuture(20); } }); ListenableFuture<Integer> failingFuture = immediateFailedFuture(raisedException); ListenableFuture<Integer> faultTolerantFuture = catchingAsync(failingFuture, Throwable.class, fallback, directExecutor()); assertEquals(20, getDone(faultTolerantFuture).intValue()); fallback.verifyCallCount(1); }
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); }
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); }
private void runExpectedExceptionCatchingAsyncTest( final Exception expectedException, final boolean wrapInFuture) throws Exception { AsyncFunctionSpy<Throwable, Integer> fallback = spy(new AsyncFunction<Throwable, Integer>() { @Override public ListenableFuture<Integer> apply(Throwable t) throws Exception { if (!wrapInFuture) { throw expectedException; } else { return immediateFailedFuture(expectedException); } } }); ListenableFuture<Integer> failingFuture = immediateFailedFuture(new RuntimeException()); ListenableFuture<Integer> faultTolerantFuture = catchingAsync(failingFuture, Throwable.class, fallback, directExecutor()); try { getDone(faultTolerantFuture); fail(); } catch (ExecutionException expected) { assertSame(expectedException, expected.getCause()); } fallback.verifyCallCount(1); }
@GwtIncompatible // mocks // TODO(cpovirk): eliminate use of mocks @SuppressWarnings("unchecked") public void testCatchingAsync_resultCancelledAfterFallback() throws Exception { final SettableFuture<Integer> secondary = SettableFuture.create(); final RuntimeException raisedException = new RuntimeException(); AsyncFunctionSpy<Throwable, Integer> fallback = spy(new AsyncFunction<Throwable, Integer>() { @Override public ListenableFuture<Integer> apply(Throwable t) throws Exception { assertThat(t).isSameAs(raisedException); return secondary; } }); ListenableFuture<Integer> failingFuture = immediateFailedFuture(raisedException); ListenableFuture<Integer> derived = catchingAsync(failingFuture, Throwable.class, fallback, directExecutor()); derived.cancel(false); assertTrue(secondary.isCancelled()); assertFalse(secondary.wasInterrupted()); fallback.verifyCallCount(1); }