@CanIgnoreReturnValue static <V> V get(ListenableFuture<V> future) throws ExecutionException, InterruptedException { return getDone(future); } }
public void testCancelled() throws ExecutionException { try { getDone(immediateCancelledFuture()); fail(); } catch (CancellationException expected) { } }
public void testTransformAsync_genericsWildcard_AsyncFunction() throws Exception { ListenableFuture<?> nullFuture = immediateFuture(null); ListenableFuture<?> chainedFuture = transformAsync(nullFuture, constantAsyncFunction(nullFuture), directExecutor()); assertNull(getDone(chainedFuture)); }
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 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_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 testCatchingAsync_Throwable() throws Exception { AsyncFunction<Throwable, Integer> fallback = asyncFunctionReturningOne(); ListenableFuture<Integer> originalFuture = immediateFailedFuture(new IOException()); ListenableFuture<Integer> faultTolerantFuture = catchingAsync(originalFuture, Throwable.class, fallback, directExecutor()); assertEquals(1, (int) getDone(faultTolerantFuture)); }
public void testAllAsList_emptyList() throws Exception { SingleCallListener listener = new SingleCallListener(); listener.expectCall(); List<ListenableFuture<String>> futures = ImmutableList.of(); ListenableFuture<List<String>> compound = allAsList(futures); compound.addListener(listener, directExecutor()); assertThat(getDone(compound)).isEmpty(); assertTrue(listener.wasCalled()); }
/** 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_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 testCatchingAsync_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 = catchingAsync(input, Throwable.class, asyncIdentity(), directExecutor()); assertThat(getDone(output)).isInstanceOf(MyError.class); }
public void testAllAsList_emptyArray() throws Exception { SingleCallListener listener = new SingleCallListener(); listener.expectCall(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = allAsList(); compound.addListener(listener, directExecutor()); assertThat(getDone(compound)).isEmpty(); assertTrue(listener.wasCalled()); }
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); } }
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); }
@GwtIncompatible // non-Throwable exceptionType public void testCatchingAsync_customTypeMatch() throws Exception { AsyncFunction<IOException, Integer> fallback = asyncFunctionReturningOne(); ListenableFuture<Integer> originalFuture = immediateFailedFuture(new FileNotFoundException()); ListenableFuture<Integer> faultTolerantFuture = catchingAsync(originalFuture, IOException.class, fallback, directExecutor()); assertEquals(1, (int) getDone(faultTolerantFuture)); }
public void testSuccessfulAsList_emptyList() throws Exception { SingleCallListener listener = new SingleCallListener(); listener.expectCall(); List<ListenableFuture<String>> futures = ImmutableList.of(); ListenableFuture<List<String>> compound = successfulAsList(futures); compound.addListener(listener, directExecutor()); assertThat(getDone(compound)).isEmpty(); assertTrue(listener.wasCalled()); }
public void testSuccessfulAsList_emptyArray() throws Exception { SingleCallListener listener = new SingleCallListener(); listener.expectCall(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = successfulAsList(); compound.addListener(listener, directExecutor()); assertThat(getDone(compound)).isEmpty(); assertTrue(listener.wasCalled()); }
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 testSuccessful() throws Exception { TrustedListenableFutureTask<Integer> task = TrustedListenableFutureTask.create(returning(2)); assertFalse(task.isDone()); task.run(); assertTrue(task.isDone()); assertFalse(task.isCancelled()); assertEquals(2, getDone(task).intValue()); }