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 testAllAsList_resultCancelled() throws Exception { SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = allAsList(future1, future2); future2.set(DATA2); assertFalse(compound.isDone()); assertTrue(compound.cancel(false)); assertTrue(compound.isCancelled()); assertTrue(future1.isCancelled()); assertFalse(future1.wasInterrupted()); }
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 testAllAsList_resultCancelled_withSecondaryListFuture() throws Exception { SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); ListenableFuture<List<String>> compound = allAsList(future1, future2); // This next call is "unused," but it is an important part of the test. Don't remove it! ListenableFuture<List<String>> unused = allAsList(future1, future2); assertTrue(compound.cancel(false)); assertTrue(future1.isCancelled()); assertFalse(future1.wasInterrupted()); assertTrue(future2.isCancelled()); assertFalse(future2.wasInterrupted()); }
public void testCompletionOrderFutureInterruption() throws Exception { SettableFuture<Long> future1 = SettableFuture.create(); SettableFuture<Long> future2 = SettableFuture.create(); SettableFuture<Long> future3 = SettableFuture.create(); ImmutableList<ListenableFuture<Long>> futures = inCompletionOrder(ImmutableList.<ListenableFuture<Long>>of(future1, future2, future3)); future2.set(1L); futures.get(1).cancel(true); futures.get(2).cancel(false); assertTrue(future1.isCancelled()); assertFalse(future1.wasInterrupted()); assertTrue(future3.isCancelled()); assertFalse(future3.wasInterrupted()); }
public void testSuccessfulAsList_resultCancelled() throws Exception { SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = successfulAsList(future1, future2); future2.set(DATA2); assertFalse(compound.isDone()); assertTrue(compound.cancel(false)); assertTrue(compound.isCancelled()); assertTrue(future1.isCancelled()); assertFalse(future1.wasInterrupted()); }
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 testTransformAsync_cancelPropagatesToInput() throws Exception { SettableFuture<Foo> input = SettableFuture.create(); AsyncFunction<Foo, Bar> function = new AsyncFunction<Foo, Bar>() { @Override public ListenableFuture<Bar> apply(Foo unused) { throw new AssertionFailedError("Unexpeted call to apply."); } }; assertTrue(transformAsync(input, function, directExecutor()).cancel(false)); assertTrue(input.isCancelled()); assertFalse(input.wasInterrupted()); }
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 testCatchingAsync_resultCancelledBeforeFallback() throws Exception { SettableFuture<Integer> primary = SettableFuture.create(); AsyncFunction<Throwable, Integer> fallback = unexpectedAsyncFunction(); ListenableFuture<Integer> derived = catchingAsync(primary, Throwable.class, fallback, directExecutor()); derived.cancel(false); assertTrue(primary.isCancelled()); assertFalse(primary.wasInterrupted()); }
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 testAllAsList_resultInterrupted() throws Exception { SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = allAsList(future1, future2); future2.set(DATA2); assertFalse(compound.isDone()); assertTrue(compound.cancel(true)); assertTrue(compound.isCancelled()); assertTrue(future1.isCancelled()); assertTrue(future1.wasInterrupted()); }
public void testNonCancellationPropagating_doesNotPropagate() throws Exception { SettableFuture<Foo> input = SettableFuture.create(); ListenableFuture<Foo> wrapper = nonCancellationPropagating(input); assertTrue(wrapper.cancel(true)); assertTrue(wrapper.isCancelled()); assertTrue(wrapper.isDone()); assertFalse(input.isCancelled()); assertFalse(input.isDone()); }
public void testTransformAsync_cancelPropagatesToAsyncOutput() throws Exception { ListenableFuture<Foo> immediate = immediateFuture(new Foo()); final SettableFuture<Bar> secondary = SettableFuture.create(); AsyncFunction<Foo, Bar> function = new AsyncFunction<Foo, Bar>() { @Override public ListenableFuture<Bar> apply(Foo unused) { return secondary; } }; assertTrue(transformAsync(immediate, function, directExecutor()).cancel(false)); assertTrue(secondary.isCancelled()); assertFalse(secondary.wasInterrupted()); }
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(); }
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 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()); } }
public void testNonCancellationPropagating_delegateCancelled() throws Exception { SettableFuture<Foo> input = SettableFuture.create(); ListenableFuture<Foo> wrapper = nonCancellationPropagating(input); assertFalse(wrapper.isDone()); assertTrue(input.cancel(false)); assertTrue(wrapper.isCancelled()); }
public void testSuccessfulAsList_resultInterrupted() throws Exception { SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = successfulAsList(future1, future2); future2.set(DATA2); assertFalse(compound.isDone()); assertTrue(compound.cancel(true)); assertTrue(compound.isCancelled()); assertTrue(future1.isCancelled()); assertTrue(future1.wasInterrupted()); }
public void testAllAsList_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 = allAsList(future1, future2); compound.addListener(listener, directExecutor()); listener.expectCall(); future1.cancel(true); assertTrue(compound.isDone()); assertTrue(listener.wasCalled()); assertFalse(future2.isDone()); try { getDone(compound); fail(); } catch (CancellationException expected) { } }