How to test that method is called after all previous async tasks are done? CompletableFuture in java

Viewed 152

I have a case where I have three methods like this:

public void mainMethod() {
    foo1();
    foo2();
    bar();
}

In second method (foo2) I am using CompletableFuture to block all async tasks which are inside:

Set<TaskRequest> requests = repository.findTaskRequests(TaskStatus.STATUS_LIST_FOR_DOWNLOAD);
CompletableFuture.allOf(requests.stream()
    .map(request -> runner.runTaskRequestAsync(request)
            .exceptionally(e -> {
                log.error(e.getMessage() + e);
                return null;})
            )
    .toArray(CompletableFuture<?>[]::new))
    .orTimeout(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES)
    .join();
log.info("All task request are done");

and task look like this:

@Async("executor")
public CompletableFuture<Void> runTaskRequestAsync(TaskRequest request) {
    //a lot of things are done here
    return CompletableFuture.completedFuture(null);
}

It is working and I see that bar() method is always called after all @Async task requests are done but how can I test it properly now?

So far I'm testing it based on log order but I don't know if that is correct way to do that.

0 Answers
Related