public void testImmediateFuture() throws Exception { ListenableFuture<String> future = immediateFuture(DATA1); assertSame(DATA1, getDone(future)); assertSame(DATA1, getDoneFromTimeoutOverload(future)); assertThat(future.toString()).contains("[status=SUCCESS, result=[" + DATA1 + "]]"); }
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 testTransform_genericsHierarchy() throws Exception { ListenableFuture<FooChild> future = immediateFuture(null); final BarChild barChild = new BarChild(); Function<Foo, BarChild> function = new Function<Foo, BarChild>() { @Override public BarChild apply(Foo unused) { return barChild; } }; Bar bar = getDone(transform(future, function, directExecutor())); assertSame(barChild, bar); }
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()); } }
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()); } }
@GwtIncompatible // immediateCheckedFuture public void testMultipleImmediateCheckedFutures() throws Exception { CheckedFuture<String, MyException> future1 = immediateCheckedFuture(DATA1); CheckedFuture<String, MyException> future2 = immediateCheckedFuture(DATA2); // Verify that the proper objects are returned without waiting assertSame(DATA1, future1.get(0L, MILLISECONDS)); assertSame(DATA1, future1.checkedGet(0L, MILLISECONDS)); assertSame(DATA2, future2.get(0L, MILLISECONDS)); assertSame(DATA2, future2.checkedGet(0L, MILLISECONDS)); }
@GwtIncompatible // immediateCheckedFuture public void testImmediateCheckedFuture() throws Exception { CheckedFuture<String, MyException> future = immediateCheckedFuture(DATA1); assertThat(future.toString()).endsWith("[status=SUCCESS, result=[" + DATA1 + "]]"); // Verify that the proper object is returned without waiting assertSame(DATA1, future.get(0L, MILLISECONDS)); assertSame(DATA1, future.checkedGet(0L, MILLISECONDS)); }
public void testSubmitAsync_asyncCallable_error() throws InterruptedException { final Error error = new Error("deliberate"); AsyncCallable<Integer> callable = new AsyncCallable<Integer>() { @Override public ListenableFuture<Integer> call() { throw error; } }; SettableFuture<String> inputFuture = SettableFuture.create(); ListenableFuture<Integer> outputFuture = submitAsync(callable, directExecutor()); inputFuture.set("value"); try { getDone(outputFuture); fail(); } catch (ExecutionException expected) { assertSame(error, expected.getCause()); } }
public void testTransform_Executor() throws Exception { Object value = new Object(); ExecutorSpy spy = new ExecutorSpy(directExecutor()); assertFalse(spy.wasExecuted); ListenableFuture<Object> future = transform(immediateFuture(value), identity(), spy); assertSame(value, getDone(future)); assertTrue(spy.wasExecuted); }
public void testTransformAsync_asyncFunction_error() throws InterruptedException { final Error error = new Error("deliberate"); AsyncFunction<String, Integer> function = new AsyncFunction<String, Integer>() { @Override public ListenableFuture<Integer> apply(String input) { throw error; } }; SettableFuture<String> inputFuture = SettableFuture.create(); ListenableFuture<Integer> outputFuture = transformAsync(inputFuture, function, directExecutor()); inputFuture.set("value"); try { getDone(outputFuture); fail(); } catch (ExecutionException expected) { assertSame(error, expected.getCause()); } }
public void testImmediateFailedFuture() throws Exception { Exception exception = new Exception(); ListenableFuture<String> future = immediateFailedFuture(exception); assertThat(future.toString()).endsWith("[status=FAILURE, cause=[" + exception + "]]"); try { getDone(future); fail(); } catch (ExecutionException expected) { assertSame(exception, expected.getCause()); } try { getDoneFromTimeoutOverload(future); fail(); } catch (ExecutionException expected) { assertSame(exception, expected.getCause()); } }
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()); } }
public void testTransformAsync_genericsHierarchy_AsyncFunction() throws Exception { ListenableFuture<FooChild> future = immediateFuture(null); final BarChild barChild = new BarChild(); AsyncFunction<Foo, BarChild> function = new AsyncFunction<Foo, BarChild>() { @Override public AbstractFuture<BarChild> apply(Foo unused) { AbstractFuture<BarChild> future = new AbstractFuture<BarChild>() {}; future.set(barChild); return future; } }; Bar bar = getDone(transformAsync(future, function, directExecutor())); assertSame(barChild, bar); }
@GwtIncompatible // threads public void testScheduleAsync_asyncCallable_error() throws InterruptedException { final Error error = new Error("deliberate"); AsyncCallable<Integer> callable = new AsyncCallable<Integer>() { @Override public ListenableFuture<Integer> call() { throw error; } }; SettableFuture<String> inputFuture = SettableFuture.create(); ListenableFuture<Integer> outputFuture = submitAsync(callable, directExecutor()); inputFuture.set("value"); try { getDone(outputFuture); fail(); } catch (ExecutionException expected) { assertSame(error, expected.getCause()); } }
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()); } }
public void testCatchingAsync_fallbackGeneratesError() throws Exception { final Error error = new Error("deliberate"); AsyncFunction<Throwable, Integer> fallback = new AsyncFunction<Throwable, Integer>() { @Override public ListenableFuture<Integer> apply(Throwable t) throws Exception { throw error; } }; ListenableFuture<Integer> failingFuture = immediateFailedFuture(new RuntimeException()); try { getDone(catchingAsync(failingFuture, Throwable.class, fallback, directExecutor())); fail(); } catch (ExecutionException expected) { assertSame(error, expected.getCause()); } }
public void testImmediateFailedFuture_cancellationException() throws Exception { CancellationException exception = new CancellationException(); ListenableFuture<String> future = immediateFailedFuture(exception); assertFalse(future.isCancelled()); assertThat(future.toString()).endsWith("[status=FAILURE, cause=[" + exception + "]]"); try { getDone(future); fail(); } catch (ExecutionException expected) { assertSame(exception, expected.getCause()); } try { getDoneFromTimeoutOverload(future); fail(); } catch (ExecutionException expected) { assertSame(exception, expected.getCause()); } }
@GwtIncompatible // immediateFailedCheckedFuture public void testImmediateFailedCheckedFuture() throws Exception { MyException exception = new MyException(); CheckedFuture<String, MyException> future = immediateFailedCheckedFuture(exception); assertThat(future.toString()).endsWith("[status=FAILURE, cause=[" + exception + "]]"); try { future.get(0L, MILLISECONDS); fail(); } catch (ExecutionException expected) { assertSame(exception, expected.getCause()); } try { future.checkedGet(0L, MILLISECONDS); fail(); } catch (MyException expected) { assertSame(exception, expected); } }
public void testNonCancellationPropagating_successful() throws Exception { SettableFuture<Foo> input = SettableFuture.create(); ListenableFuture<Foo> wrapper = nonCancellationPropagating(input); Foo foo = new Foo(); assertFalse(wrapper.isDone()); input.set(foo); assertTrue(wrapper.isDone()); assertSame(foo, getDone(wrapper)); }
public void testAllAsList_failure() throws Exception { SingleCallListener listener = new SingleCallListener(); SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = allAsList(future1, future2); compound.addListener(listener, directExecutor()); listener.expectCall(); Throwable exception = new Throwable("failed1"); future1.setException(exception); assertTrue(compound.isDone()); assertTrue(listener.wasCalled()); assertFalse(future2.isDone()); try { getDone(compound); fail(); } catch (ExecutionException expected) { assertSame(exception, expected.getCause()); } }