@Override public void execute(Runnable runnable) { oldFuture.addListener(runnable, executor); } });
@Override public void addListener(Runnable listener, Executor executor) { delegate.addListener(listener, executor); }
public MockFutureListener(ListenableFuture<?> future) { this.countDownLatch = new CountDownLatch(1); this.future = future; future.addListener(this, directExecutor()); }
@Override public void addListener(Runnable listener, Executor exec) { delegate().addListener(listener, exec); }
static <V> ListenableFuture<V> create( ListenableFuture<V> delegate, long time, TimeUnit unit, ScheduledExecutorService scheduledExecutor) { TimeoutFuture<V> result = new TimeoutFuture<>(delegate); Fire<V> fire = new Fire<>(result); result.timer = scheduledExecutor.schedule(fire, time, unit); delegate.addListener(fire, directExecutor()); return result; }
static <I, O> ListenableFuture<O> create( ListenableFuture<I> input, AsyncFunction<? super I, ? extends O> function, Executor executor) { checkNotNull(executor); AsyncTransformFuture<I, O> output = new AsyncTransformFuture<>(input, function); input.addListener(output, rejectionPropagatingExecutor(executor, output)); return output; }
static <I, O> ListenableFuture<O> create( ListenableFuture<I> input, Function<? super I, ? extends O> function, Executor executor) { checkNotNull(function); TransformFuture<I, O> output = new TransformFuture<>(input, function); input.addListener(output, rejectionPropagatingExecutor(executor, output)); return output; }
static <V, X extends Throwable> ListenableFuture<V> create( ListenableFuture<? extends V> input, Class<X> exceptionType, Function<? super X, ? extends V> fallback, Executor executor) { CatchingFuture<V, X> future = new CatchingFuture<>(input, exceptionType, fallback); input.addListener(future, rejectionPropagatingExecutor(executor, future)); return future; }
/** * Returns a {@code ListenableFuture} whose result is set from the supplied future when it * completes. Cancelling the supplied future will also cancel the returned future, but cancelling * the returned future will have no effect on the supplied future. * * @since 15.0 */ public static <V> ListenableFuture<V> nonCancellationPropagating(ListenableFuture<V> future) { if (future.isDone()) { return future; } NonCancellationPropagatingFuture<V> output = new NonCancellationPropagatingFuture<>(future); future.addListener(output, directExecutor()); return output; }
static <X extends Throwable, V> ListenableFuture<V> create( ListenableFuture<? extends V> input, Class<X> exceptionType, AsyncFunction<? super X, ? extends V> fallback, Executor executor) { AsyncCatchingFuture<V, X> future = new AsyncCatchingFuture<>(input, exceptionType, fallback); input.addListener(future, rejectionPropagatingExecutor(executor, future)); return future; }
public void setUp() { future.addListener( new Runnable() { @Override public void run() { latch.countDown(); } }, exec); assertEquals(1, latch.getCount()); assertFalse(future.isDone()); assertFalse(future.isCancelled()); }
private static void assertListenerRunImmediately(ListenableFuture<?> future) { CountingRunnable listener = new CountingRunnable(); future.addListener(listener, directExecutor()); assertEquals(1, listener.count); }
public void testTransformAsync_listenerThrowsError() throws Exception { SettableFuture<Object> input = SettableFuture.create(); ListenableFuture<Object> output = transformAsync(input, asyncIdentity(), directExecutor()); output.addListener( new Runnable() { @Override public void run() { throw new MyError(); } }, directExecutor()); try { input.set("foo"); fail(); } catch (MyError expected) { } }
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()); }
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 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_totalFailure() 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 = successfulAsList(future1, future2); compound.addListener(listener, directExecutor()); assertFalse(compound.isDone()); future1.setException(new Throwable("failed1")); assertFalse(compound.isDone()); listener.expectCall(); future2.setException(new Throwable("failed2")); assertTrue(listener.wasCalled()); List<String> results = getDone(compound); assertThat(results).containsExactly(null, null).inOrder(); }
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 testSuccessfulAsList_partialFailure() 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 = successfulAsList(future1, future2); compound.addListener(listener, directExecutor()); assertFalse(compound.isDone()); future1.setException(new Throwable("failed1")); assertFalse(compound.isDone()); listener.expectCall(); future2.set(DATA2); assertTrue(listener.wasCalled()); List<String> results = getDone(compound); assertThat(results).containsExactly(null, DATA2).inOrder(); }
public void testSuccessfulAsList_cancelled() 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 = successfulAsList(future1, future2); compound.addListener(listener, directExecutor()); assertFalse(compound.isDone()); future1.cancel(true); assertFalse(compound.isDone()); listener.expectCall(); future2.set(DATA2); assertTrue(listener.wasCalled()); List<String> results = getDone(compound); assertThat(results).containsExactly(null, DATA2).inOrder(); }