public static <T> Task<T> fromListenableFuture(ListenableFuture<T> future) {

    /**
     * BaseTask's promise will be listening to this
     * also see {@link BaseTask#contextRun(Context, Task, Collection)}
     */
    final SettablePromise<T> promise = Promises.settable();

    // Setup cancellation propagation from Task -> ListenableFuture.
    final Task<T> task =
        new BaseTask<T>("fromListenableFuture: " + Task._taskDescriptor.getDescription(future.getClass().getName())) {
          @Override
          public boolean cancel(Exception rootReason) {
            // <BaseTask>.cancel()'s result indicates whether cancel() successfully trigger state transition to "CANCELLED"
            // And we should only cancel GRPC future when the transition was conducted.
            boolean shouldCancelTask = super.cancel(rootReason);
            if (shouldCancelTask && !future.isCancelled()) {
              boolean futureCancelResult = future.cancel(true);
              if (!futureCancelResult) {
                LOGGER.warn("Unexpected: GRPC future was not cancelled but new attempt to cancel also failed.");
              }
            }
            return shouldCancelTask;
          }

          @Override
          protected Promise<? extends T> run(Context context) throws Throwable {
            return promise;
          }
        };


    // Setup forward event propagation ListenableFuture -> Task.
    Runnable callbackRunnable = () -> {
      if (promise.isDone()) {
        boolean isPromiseFailed = promise.isFailed();
        LOGGER.warn("ListenableFuture callback triggered but ParSeq already done. "
                + "Future is done: {}, "
                + "Future is cancelled: {}"
                + "Promise is failed:{}"
            + (isPromiseFailed? " Promise hold error: {}" : "Promise hold data:{}"),
            future.isDone(),
            future.isCancelled(),
            isPromiseFailed,
            isPromiseFailed ? promise.getError(): promise.get()
            );
        return;
      }
      try {
        final T value = future.get();
        promise.done(value);
      } catch (CancellationException ex) {
        task.cancel(ex);
      } catch (ExecutionException ex) {
        promise.fail(ex.getCause());
      } catch (Exception | Error ex) {
        promise.fail(ex);
      }
    };
    future.addListener(callbackRunnable, MoreExecutors.directExecutor());

    return task;
  }
@Test
  public void testFromListenableFuture() throws Exception {
    ListenableFutureUtil.SettableFuture<String> listenableFuture = new ListenableFutureUtil.SettableFuture<>();
    Task<String> task = ListenableFutureUtil.fromListenableFuture(listenableFuture);

    // Test cancel propagation from Task to ListenableFuture
    task.cancel(new RuntimeException());
    runUntilComplete(task);
    Assert.assertTrue(listenableFuture.isCancelled());

    listenableFuture = new ListenableFutureUtil.SettableFuture<>();
    task = ListenableFutureUtil.fromListenableFuture(listenableFuture);

    // Test successful completion of ListenableFuture.
    listenableFuture.set("COMPLETED");
    runUntilComplete(task);
    Assert.assertTrue(task.isDone());
    Assert.assertFalse(task.isFailed());
    Assert.assertEquals(task.get(), "COMPLETED");

    listenableFuture = new ListenableFutureUtil.SettableFuture<>();
    task = ListenableFutureUtil.fromListenableFuture(listenableFuture);

    // Test exceptional completion of ListenableFuture.
    listenableFuture.setException(new RuntimeException("Test"));
    runUntilComplete(task);
    Assert.assertTrue(task.isDone());
    Assert.assertTrue(task.isFailed());
    Assert.assertEquals(task.getError().getClass(), RuntimeException.class);
    Assert.assertEquals(task.getError().getMessage(), "Test");

    listenableFuture = new ListenableFutureUtil.SettableFuture<>();
    task = ListenableFutureUtil.fromListenableFuture(listenableFuture);

    // Test cancellation of ListenableFuture.
    listenableFuture.cancel(true);
    runUntilComplete(task);
    Assert.assertTrue(task.isDone());
    Assert.assertTrue(task.isFailed());
    Assert.assertEquals(task.getError().getCause().getClass(), CancellationException.class);
  }