@Override public boolean cancel(boolean interrupt) { // we need a copy in case canceling clears out the futures ArrayList<ListenableFuture<? extends T>> futures = this.futures; if (super.cancel(interrupt)) { FutureUtils.cancelIncompleteFutures(futures, interrupt); return true; } else { return false; } }
protected boolean cancelRegardlessOfDelegateFutureState(boolean interruptThread) { ListenableFuture<?> cancelDelegateFuture = this.delegateFuture; if (super.cancel(interruptThread)) { cancelDelegateFuture.cancel(interruptThread); return true; } else { return false; } }
@Override public ListenableFuture<?> makeCanceled() { SettableListenableFuture<?> slf = new SettableListenableFuture<>(); slf.cancel(false); return slf; }
@Test public void getCancellationTest() throws InterruptedException, ExecutionException { slf.cancel(false); try { slf.get(); fail("Exception should have thrown"); } catch (CancellationException e) { // expected } }
@Test public void cancelationExceptionMessageTest() throws InterruptedException { String msg = StringUtils.makeRandomString(5); SettableListenableFuture<Void> slf = new CancelMessageTestSettableListenableFuture(msg); slf.cancel(false); verifyCancelationExceptionMessageOnGet(msg, slf); }
@Test public void futureCallbackCancelationExceptionMessageTest() throws InterruptedException, TimeoutException { String msg = StringUtils.makeRandomString(5); SettableListenableFuture<Void> slf = new CancelMessageTestSettableListenableFuture(msg); slf.cancel(false); verifyCancelationExceptionMessageInCallback(msg, slf); }
@Test public void cancelSetFailureTest() { slf = new SettableListenableFuture<>(false); assertTrue(slf.cancel(false)); assertFalse(slf.setFailure(null)); }
@Test public void makeSuccessListFutureWithCancelErrorTest() throws ExecutionException, InterruptedException, TimeoutException { List<ListenableFuture<?>> futures = makeFutures(TEST_QTY, -1); SettableListenableFuture<?> cancelFuture = new SettableListenableFuture<>(); cancelFuture.cancel(false); futures.add(cancelFuture); ListenableFuture<List<ListenableFuture<?>>> f = FutureUtils.makeSuccessListFuture(futures); verifyCompleteFuture(f, futures); verifyAllIncluded(futures, f.get(), cancelFuture); }
@Test public void cancelSetResultTest() { slf = new SettableListenableFuture<>(false); assertTrue(slf.cancel(false)); assertFalse(slf.setResult(null)); }
@Test public void makeFailurePropagatingCompleteFuturePropagateCancelTest() { SettableListenableFuture<?> slf = new SettableListenableFuture<>(); assertTrue(slf.cancel(false)); ListenableFuture<?> f = FutureUtils.makeFailurePropagatingCompleteFuture(Collections.singletonList(slf)); assertTrue(f.isDone()); assertTrue(f.isCancelled()); }
@Test public void mapCancelationExceptionMessageAlreadyDoneTest() throws InterruptedException, TimeoutException { String msg = StringUtils.makeRandomString(5); SettableListenableFuture<Void> slf = new CancelMessageTestSettableListenableFuture(msg); slf.cancel(false); ListenableFuture<Void> mappedFuture = slf.map((v) -> v); verifyCancelationExceptionMessageOnGet(msg, mappedFuture); verifyCancelationExceptionMessageInCallback(msg, mappedFuture); }
@Test public void chainCanceledFutureTest() { SettableListenableFuture<String> canceledSlf = new SettableListenableFuture<>(); assertTrue(canceledSlf.cancel(false)); canceledSlf.addCallback(slf); assertTrue(slf.isCancelled()); }
@Test public void mapCancelationExceptionMessageTest() throws InterruptedException, TimeoutException { String msg = StringUtils.makeRandomString(5); SettableListenableFuture<Void> slf = new CancelMessageTestSettableListenableFuture(msg); ListenableFuture<Void> mappedFuture = slf.map((v) -> v); slf.cancel(false); verifyCancelationExceptionMessageOnGet(msg, mappedFuture); verifyCancelationExceptionMessageInCallback(msg, mappedFuture); }
@Test public void failureMapCancelationExceptionMessageTest() throws InterruptedException, TimeoutException { String msg = StringUtils.makeRandomString(5); SettableListenableFuture<Void> slf = new CancelMessageTestSettableListenableFuture(msg); ListenableFuture<Void> mappedFuture = slf.mapFailure(CancellationException.class, (c) -> { throw c; }); slf.cancel(false); verifyCancelationExceptionMessageOnGet(msg, mappedFuture); verifyCancelationExceptionMessageInCallback(msg, mappedFuture); }
@Test public void cancelTest() { SettableListenableFuture<Object> slf = new SettableListenableFuture<>(); slf.cancel(false); ListenableFutureAdapterTask<Object> adapter = new ListenableFutureAdapterTask<>(slf); adapter.run(); assertTrue(adapter.isCancelled()); }
@Test public void flatMapCancelationExceptionMessageAlreadyDoneTest() throws InterruptedException, TimeoutException { String msg = StringUtils.makeRandomString(5); SettableListenableFuture<Void> slf = new CancelMessageTestSettableListenableFuture(msg); slf.cancel(false); ListenableFuture<Void> mappedFuture = slf.flatMap((v) -> FutureUtils.immediateResultFuture(null)); verifyCancelationExceptionMessageOnGet(msg, mappedFuture); verifyCancelationExceptionMessageInCallback(msg, mappedFuture); }
@Test public void flatMapCancelationExceptionMessageTest() throws InterruptedException, TimeoutException { String msg = StringUtils.makeRandomString(5); SettableListenableFuture<Void> slf = new CancelMessageTestSettableListenableFuture(msg); ListenableFuture<Void> mappedFuture = slf.flatMap((v) -> FutureUtils.immediateResultFuture(null)); slf.cancel(false); verifyCancelationExceptionMessageOnGet(msg, mappedFuture); verifyCancelationExceptionMessageInCallback(msg, mappedFuture); }
@Test public void failureFlatMapCancelationExceptionMessageAlreadyDoneTest() throws InterruptedException, TimeoutException { String msg = StringUtils.makeRandomString(5); SettableListenableFuture<Void> slf = new CancelMessageTestSettableListenableFuture(msg); slf.cancel(false); ListenableFuture<Void> mappedFuture = slf.flatMapFailure(CancellationException.class, (c) -> FutureUtils.immediateFailureFuture(c)); verifyCancelationExceptionMessageOnGet(msg, mappedFuture); verifyCancelationExceptionMessageInCallback(msg, mappedFuture); }
@Test public void scheduleWhileAlreadyDoneCanceledTest() { TestableScheduler scheduler = new TestableScheduler(); SettableListenableFuture<?> startingFuture = new SettableListenableFuture<>(); startingFuture.cancel(false); ListenableFuture<Object> f = FutureUtils.scheduleWhile(scheduler, 2, startingFuture, () -> null, (o) -> false); assertTrue(f.isDone()); assertTrue(f.isCancelled()); }
@Test public void watchIncompleteCanceledFutureTest() { poller = new Poller(scheduler, POLL_INTERVAL, POLL_INTERVAL * 2); // must have timeout to avoid just casting SLF SettableListenableFuture<Object> slf = new SettableListenableFuture<>(); ListenableFuture<Object> lfResult = poller.watch(slf); assertFalse(lfResult.isDone()); slf.cancel(false); assertEquals(1, scheduler.advance(POLL_INTERVAL)); assertTrue(lfResult.isDone()); assertTrue(lfResult.isCancelled()); }