@Test public void testCancellationListenersCalledOnFutureCancelled() { final AtomicReference<Completer<String>> completerRef = new AtomicReference<>(); final AtomicBoolean wasCalled = new AtomicBoolean(); ListenableFuture<String> future = CallbackToFutureAdapter.getFuture( new CallbackToFutureAdapter.Resolver<String>() { @Override public Object attachCompleter(Completer<String> completer) { completer.addCancellationListener(new Runnable() { @Override public void run() { wasCalled.set(true); } }, DirectExecutor.INSTANCE); completerRef.set(completer); return null; } }); assertThat(future.cancel(true)).isTrue(); assertThat(wasCalled.get()).isTrue(); assertThat(completerRef.get().setCancelled()).isFalse(); }
@NonNull public static <T> ListenableFuture<T> getFuture(@NonNull Resolver<T> callback) { Completer<T> completer = new Completer<>(); SafeFuture<T> safeFuture = new SafeFuture<>(completer); completer.future = safeFuture; completer.tag = callback.getClass(); final Object tag; try { tag = callback.attachCompleter(completer); if (tag != null) { completer.tag = tag; } } catch (Exception e) { safeFuture.setException(e); } return safeFuture; }
CallbackToFutureAdapter { @NonNull public static <T> ListenableFuture<T> getFuture(@NonNull Resolver<T> callback) { Completer<T> completer = new Completer<>(); SafeFuture<T> safeFuture = new SafeFuture<>(completer); completer.future = safeFuture; completer.tag = callback.getClass(); final Object tag; try { tag = callback.attachCompleter(completer); if (tag != null) { completer.tag = tag; } } catch (Exception e) { safeFuture.setException(e); } return safeFuture; } }
CallbackToFutureAdapter { @NonNull public static <T> ListenableFuture<T> getFuture(@NonNull Resolver<T> callback) { Completer<T> completer = new Completer<>(); SafeFuture<T> safeFuture = new SafeFuture<>(completer); completer.future = safeFuture; completer.tag = callback.getClass(); final Object tag; try { tag = callback.attachCompleter(completer); if (tag != null) { completer.tag = tag; } } catch (Exception e) { safeFuture.setException(e); } return safeFuture; } private  CallbackToFutureAdapter(); }
CallbackToFutureAdapter { @NonNull public static <T> ListenableFuture<T> getFuture(@NonNull Resolver<T> callback) { Completer<T> completer = new Completer<>(); SafeFuture<T> safeFuture = new SafeFuture<>(completer); completer.future = safeFuture; completer.tag = callback.getClass(); final Object tag; try { tag = callback.attachCompleter(completer); if (tag != null) { completer.tag = tag; } } catch (Exception e) { safeFuture.setException(e); } return safeFuture; } private  CallbackToFutureAdapter(); @NonNull static ListenableFuture<T> getFuture(@NonNull Resolver<T> callback); }
CallbackToFutureAdapter { @NonNull public static <T> ListenableFuture<T> getFuture(@NonNull Resolver<T> callback) { Completer<T> completer = new Completer<>(); SafeFuture<T> safeFuture = new SafeFuture<>(completer); completer.future = safeFuture; completer.tag = callback.getClass(); final Object tag; try { tag = callback.attachCompleter(completer); if (tag != null) { completer.tag = tag; } } catch (Exception e) { safeFuture.setException(e); } return safeFuture; } private  CallbackToFutureAdapter(); @NonNull static ListenableFuture<T> getFuture(@NonNull Resolver<T> callback);  }