Fire and Forget task in After hook in cucumber

Viewed 44
  • I am trying to trigger a fire and forget task in the @After hook of Cucumber.
  • I am trying to achieve it using completable future
@After(order=1)
public void deleteData(Scenario scenario) {
    ExecutorService executor = Executors.newFixedThreadPool(10);
    CompletableFuture.supplyAsync(
            () -> {
              boolean result = false;
              try {

                // SOME CALL TO DATABASE which takes some time to complete

              } catch (Exception ex) {
                System.out.println(ex);
                throw new RuntimeException(ex.getMessage());
              }
              return result;
            },
            executor)
        .handle(
            (aBoolean, throwable) -> {
              if (throwable instanceof Exception) {
                return false;
              }
              return true;
            })
        .thenAccept(
            aBoolean -> {
              System.out.println(aBoolean);
              executor.shutdown();
            });
  }

The task in the supplyAsync block is not completing fully and is dyeing in the middle once the test completes it execution.

I need to add join() at the end, in order to wait for the async task to complete fully.

Even though I am running the task on a separate executor service, why is the async task not completing fully and dyeing after the main thread ends ??

0 Answers
Related