public MockFutureListener(ListenableFuture<?> future) { this.countDownLatch = new CountDownLatch(1); this.future = future; future.addListener(this, directExecutor()); }
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; }
public void testSameThreadExecutionException() { SettableFuture<String> f = SettableFuture.create(); Exception e = new IllegalArgumentException("foo not found"); MockCallback callback = new MockCallback(e); addCallback(f, callback, directExecutor()); f.setException(e); }
@Override protected Executor executor() { transitionStates.add(state()); return directExecutor(); } }
public void testExceptionsCaught() { list.add(THROWING_RUNNABLE, directExecutor()); list.execute(); list.add(THROWING_RUNNABLE, directExecutor()); }
public void testCallableStartsAfterFirstFutureCompletes() { @SuppressWarnings({"unused", "nullness"}) Future<?> possiblyIgnoredError = serializer.submitAsync(firstCallable, directExecutor()); TestCallable secondCallable = new TestCallable(Futures.<Void>immediateFuture(null)); @SuppressWarnings({"unused", "nullness"}) Future<?> possiblyIgnoredError1 = serializer.submitAsync(secondCallable, directExecutor()); assertThat(firstCallable.called).isTrue(); assertThat(secondCallable.called).isFalse(); firstFuture.set(null); assertThat(secondCallable.called).isTrue(); }
public void testMisbehavingListenerAlreadyDone() { class BadRunnableException extends RuntimeException {} Runnable bad = new Runnable() { @Override public void run() { throw new BadRunnableException(); } }; future.set(1); future.addListener(bad, directExecutor()); // BadRunnableException must not propagate. }
public void testNullListener() { try { future.addListener(null, directExecutor()); fail(); } catch (NullPointerException expected) { } }
static RecordingListener record(Service service) { RecordingListener listener = new RecordingListener(service); service.addListener(listener, directExecutor()); return listener; }
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 testTransformAsync_genericsWildcard_AsyncFunction() throws Exception { ListenableFuture<?> nullFuture = immediateFuture(null); ListenableFuture<?> chainedFuture = transformAsync(nullFuture, constantAsyncFunction(nullFuture), directExecutor()); assertNull(getDone(chainedFuture)); }
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 testListenLaterFailed() { CountingRunnable listener = new CountingRunnable(); future.addListener(listener, directExecutor()); listener.assertNotRun(); future.setException(new Exception()); listener.assertRun(); }
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_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_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()); }
public void testListenLaterSuccessful() { CountingRunnable listener = new CountingRunnable(); future.addListener(listener, directExecutor()); listener.assertNotRun(); future.set(1); listener.assertRun(); }
private static void assertPending(AbstractFuture<Integer> future) { assertThat(future.isDone()).isFalse(); assertThat(future.isCancelled()).isFalse(); CountingRunnable listener = new CountingRunnable(); future.addListener(listener, directExecutor()); listener.assertNotRun(); verifyGetOnPendingFuture(future); verifyTimedGetOnPendingFuture(future); }
private static void assertDone(AbstractFuture<Integer> future) { CountingRunnable listener = new CountingRunnable(); future.addListener(listener, directExecutor()); listener.assertRun(); assertThat(future.isDone()).isTrue(); assertCannotSet(future); assertCannotCancel(future); }