@Override public ListenableFuture<T> call() throws Exception { if (!runningState.compareAndSet(NOT_RUN, STARTED)) { return immediateCancelledFuture(); } return callable.call(); }
static ListenableFuture<String> makeImmediateCancelledFuture() { return immediateCancelledFuture(); } }
public void testToString_cancelled() throws Exception { assertThat(Futures.immediateCancelledFuture().toString()) .matches("[^\\[]+\\[status=CANCELLED\\]"); }
private Future<?> processMessage(final Message msg, final MessageOutput output, final CountDownLatch doneSignal) { if (output == null) { LOG.error("Output was null!"); return Futures.immediateCancelledFuture(); return Futures.immediateCancelledFuture();
public void testCancelled() throws ExecutionException { try { getDone(immediateCancelledFuture()); fail(); } catch (CancellationException expected) { } }
@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()); }
@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()); }
Callable<Void> setFutureCancelRunnable = new Callable<Void>() { ListenableFuture<String> future = Futures.immediateCancelledFuture();
ListenableFuture<Object> cancelledFuture = Futures.immediateCancelledFuture(); SettableFuture<Object> cancellationFuture = SettableFuture.create();
@Test(groups = "unit") public void should_run_if_another_handler_was_cancelled() { future.set(Futures.immediateCancelledFuture()); handler.start(); schedule.tick(); work.nextReconnect = ReconnectBehavior.SUCCEED; work.tick(); waitForCompletion(); assertThat(work.success).isTrue(); assertThat(work.tries).isEqualTo(1); assertThat(future.get()).isNull(); }
/** * {@inheritDoc} */ @Override public Future<LogEvent> post(final Callable<LogEvent> eventCallaback) { checkNotNull(eventCallaback); try { return executor.submit(new Callable<LogEvent>() { @Override public LogEvent call() throws Exception { LogEvent logEvent = checkNotNull(eventCallaback.call()); dispatchEvent(logEvent); return logEvent; } }); } catch (RejectedExecutionException ex) { return Futures.immediateCancelledFuture(); } }
@Override public ListenableFuture<T> apply(T input) throws Exception { if (context.isCanceled()) { return Futures.immediateCancelledFuture(); } return Futures.immediateFuture(input); } }, executor);
@Override public ListenableFuture<T> call() throws Exception { if (!runningState.compareAndSet(NOT_RUN, STARTED)) { return immediateCancelledFuture(); } return callable.call(); } };
static ListenableFuture<String> makeImmediateCancelledFuture() { return immediateCancelledFuture(); } }
@Override public final ListenableFuture<T> get() { synchronized (futures) { if (cancelled) { return Futures.immediateCancelledFuture(); } final ListenableFuture<T> future = callDependencyMethod(); if (!future.isDone() && futures.add(future)) { future.addListener( new Runnable() { @Override public void run() { synchronized (futures) { futures.remove(future); } } }, directExecutor()); } return future; } }
public void testToString_cancelled() throws Exception { assertThat(Futures.immediateCancelledFuture().toString()) .matches("[^\\[]+\\[status=CANCELLED\\]"); }
@Test public void createImageDoesNotRegisterInCacheWhenCancelled() { ImageCacheSupplier imageCache = createMock(ImageCacheSupplier.class); ImageExtension delegate = createMock(ImageExtension.class); AddDefaultCredentialsToImage credsToImage = createMock(AddDefaultCredentialsToImage.class); ImageTemplate template = new ImageTemplateImpl("test") { }; expect(delegate.createImage(template)).andReturn(Futures.<Image> immediateCancelledFuture()); replay(delegate, imageCache, credsToImage); new DelegatingImageExtension(imageCache, delegate, credsToImage, null).createImage(template); verify(delegate, imageCache, credsToImage); }
@Test(groups = "unit") public void should_run_if_another_handler_was_cancelled() { future.set(Futures.immediateCancelledFuture()); handler.start(); schedule.tick(); work.nextReconnect = ReconnectBehavior.SUCCEED; work.tick(); waitForCompletion(); assertThat(work.success).isTrue(); assertThat(work.tries).isEqualTo(1); assertThat(future.get()).isNull(); }
public void testCancelled() throws ExecutionException { try { getDone(immediateCancelledFuture()); fail(); } catch (CancellationException expected) { } }
@Test public void testFutureCallInitialCancel() throws Exception { String opName = "testFutureCallInitialCancel"; OperationSnapshot initialOperation = getOperation(opName, null, null, null, false); OperationSnapshot resultOperation = getOperation(opName, null, null, null, false); UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation); LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, resultOperation); OperationCallableImpl<Integer, Color, Currency> callableImpl = Callables.longRunningOperationImpl( initialCallable, callSettings, initialContext, longRunningClient); OperationFutureImpl<Color, Currency> future = callableImpl.futureCall( new ListenableFutureToApiFuture<>( Futures.<OperationSnapshot>immediateCancelledFuture()), FakeCallContext.createDefault()); Exception exception = null; try { future.get(3, TimeUnit.SECONDS); } catch (CancellationException e) { exception = e; } assertThat(exception).isNotNull(); assertThat(future.isDone()).isTrue(); assertThat(future.isCancelled()).isTrue(); assertThat(future.getInitialFuture().isDone()).isTrue(); assertThat(future.getInitialFuture().isCancelled()).isTrue(); assertFutureCancelMetaCancel(future); assertThat(executor.getIterationsCount()).isEqualTo(0); }