Worker { @NotNull V perform(@NotNull AsyncSupplier<V> task) { if (myResultFuture == null) { myResultFuture = task.get(); } if (myResultFuture.isCancelled()) { myResultFuture = task.get(); return myResult; } if (!myResultFuture.isDone()) { return myResult; } try { myResult = myResultFuture.get(); myResultFuture = task.get(); return myResult; } catch (InterruptedException exception) { Thread.currentThread().interrupt(); throw new RuntimeException(exception); } catch (ExecutionException exception) { Logger.getInstance(Worker.class).warn(exception); myResultFuture = task.get(); return myResult; } } Worker(@NotNull V result);   }
@Test public void performResultFutureIsCancelled() { Object expectedResult = new Object(); Worker<Object> worker = new Worker<>(expectedResult); Object actualResult = worker.perform(Futures::immediateCancelledFuture); assertEquals(expectedResult, actualResult); }