@Override public boolean isCancelled() { return delegate.isCancelled(); }
@Override public void run() { if (taskFuture.isDone() // If this CAS succeeds, we know that the provided callable will never be invoked, // so when oldFuture completes it is safe to allow the next submitted task to // proceed. || (outputFuture.isCancelled() && runningState.compareAndSet(NOT_RUN, CANCELLED))) { // Since the value of oldFuture can only ever be immediateFuture(null) or setFuture of // a future that eventually came from immediateFuture(null), this doesn't leak // throwables or completion values. newFuture.setFuture(oldFuture); } } };
public void testCancelledFuture() throws InterruptedException, ExecutionException { assertTrue(future.isDone()); assertTrue(future.isCancelled()); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertTrue(future.isDone()); assertTrue(future.isCancelled()); try { future.get(); fail("Future should throw CancellationException on cancel."); } catch (CancellationException expected) { } }
public void testCompletedFuture(@Nullable Object expectedValue) throws InterruptedException, ExecutionException { assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertEquals(expectedValue, future.get()); }
public void setUp() { future.addListener( new Runnable() { @Override public void run() { latch.countDown(); } }, exec); assertEquals(1, latch.getCount()); assertFalse(future.isDone()); assertFalse(future.isCancelled()); }
public void testTransformAsync_ErrorAfterCancellation() throws Exception { class Transformer implements AsyncFunction<Object, Object> { ListenableFuture<Object> output; @Override public ListenableFuture<Object> apply(Object input) { output.cancel(false); throw new MyError(); } } Transformer transformer = new Transformer(); SettableFuture<Object> input = SettableFuture.create(); ListenableFuture<Object> output = transformAsync(input, transformer, directExecutor()); transformer.output = output; input.set("foo"); assertTrue(output.isCancelled()); }
public void testFailedFuture(@Nullable String message) throws InterruptedException { assertTrue(future.isDone()); assertFalse(future.isCancelled()); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertTrue(future.isDone()); assertFalse(future.isCancelled()); try { future.get(); fail("Future should rethrow the exception."); } catch (ExecutionException e) { assertThat(e).hasCauseThat().hasMessageThat().isEqualTo(message); } } }
public void testImmediateCancelledFutureBasic() throws Exception { ListenableFuture<String> future = CallerClass1.makeImmediateCancelledFuture(); assertTrue(future.isCancelled()); }
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()); }
@GwtIncompatible // non-Throwable exceptionType public void testCatching_inputCancelledWithoutFallback() throws Exception { Function<IOException, Integer> fallback = unexpectedFunction(); ListenableFuture<Integer> originalFuture = immediateCancelledFuture(); ListenableFuture<Integer> faultTolerantFuture = catching(originalFuture, IOException.class, fallback, directExecutor()); assertTrue(faultTolerantFuture.isCancelled()); }
@GwtIncompatible // non-Throwable exceptionType public void testCatchingAsync_inputCancelledWithoutFallback() throws Exception { AsyncFunction<Throwable, Integer> fallback = unexpectedAsyncFunction(); ListenableFuture<Integer> originalFuture = immediateCancelledFuture(); ListenableFuture<Integer> faultTolerantFuture = catchingAsync(originalFuture, IOException.class, fallback, directExecutor()); assertTrue(faultTolerantFuture.isCancelled()); }
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_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 testFutureCancelAfterCompletion() throws Exception { inputFuture.set(VALID_INPUT_DATA); assertFalse(resultFuture.cancel(true)); assertFalse(resultFuture.isCancelled()); assertFalse(inputFuture.isCancelled()); assertFalse(outputFuture.isCancelled()); assertEquals(RESULT_DATA, resultFuture.get()); }
public void testFutureCancellableBeforeOutputCompletion() throws Exception { inputFuture.set(SLOW_OUTPUT_VALID_INPUT_DATA); assertTrue(resultFuture.cancel(true)); assertTrue(resultFuture.isCancelled()); assertFalse(inputFuture.isCancelled()); assertTrue(outputFuture.isCancelled()); try { resultFuture.get(); fail("Result future is cancelled and should have thrown a" + " CancellationException"); } catch (CancellationException expected) { } }
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_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_resultCancelledInterrupted_withSecondaryListFuture() throws Exception { SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); ListenableFuture<List<String>> compound = allAsList(future1, future2); // There was a bug where the event listener for the combined future would // result in the sub-futures being cancelled without being interrupted. ListenableFuture<List<String>> otherCompound = allAsList(future1, future2); assertTrue(compound.cancel(true)); assertTrue(future1.isCancelled()); assertTrue(future1.wasInterrupted()); assertTrue(future2.isCancelled()); assertTrue(future2.wasInterrupted()); assertTrue(otherCompound.isCancelled()); }
public void testFutureCancelBeforeInputCompletion() throws Exception { assertTrue(resultFuture.cancel(true)); assertTrue(resultFuture.isCancelled()); assertTrue(inputFuture.isCancelled()); assertFalse(outputFuture.isCancelled()); try { resultFuture.get(); fail("Result future is cancelled and should have thrown a" + " CancellationException"); } catch (CancellationException expected) { } }
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()); }