FutureSubscriber extends CountDownLatch implements FlowableSubscriber<T>, Future<T>, Subscription { @Override public boolean cancel(boolean mayInterruptIfRunning) { for (;;) { Subscription a = s.get(); if (a == this || a == SubscriptionHelper.CANCELLED) { return false; } if (s.compareAndSet(a, SubscriptionHelper.CANCELLED)) { if (a != null) { a.cancel(); } countDown(); return true; } } } FutureSubscriber(); @Override boolean cancel(boolean mayInterruptIfRunning); @Override boolean isCancelled(); @Override boolean isDone(); @Override T get(); @Override T get(long timeout, TimeUnit unit); @Override void onSubscribe(Subscription s); @Override void onNext(T t); @Override void onError(Throwable t); @Override void onComplete(); @Override void cancel(); @Override void request(long n);  }
@Test public void cancel() throws Exception { assertFalse(fs.isDone()); assertFalse(fs.isCancelled()); fs.cancel(); fs.cancel(); fs.request(10); fs.request(-99); fs.cancel(false); assertTrue(fs.isDone()); assertTrue(fs.isCancelled()); try { fs.get(); fail("Should have thrown"); } catch (CancellationException ex) { } try { fs.get(1, TimeUnit.MILLISECONDS); fail("Should have thrown"); } catch (CancellationException ex) { } }
@Test public void cancelRace() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final FutureSubscriber<Integer> fs = new FutureSubscriber<Integer>(); Runnable r = new Runnable() { @Override public void run() { fs.cancel(false); } }; TestHelper.race(r, r); } }