Class CompletionStageTestAwaiter<T>

  • public final class CompletionStageTestAwaiter<T>
    extends Object
    Test utility to await the completion of a CompletionStage.

    Note that if you have a CompletableFuture instead of a CompletionStage, then you could just use CompletableFuture.join(), which does the same thing as the await500ms(CompletionStage) here. However, to test operations which return only a CompletionStage but not a full CompletableFuture, you use this. (CompletionStage is often a more suitable operation return type, because it "does not define methods for initially creating, forcibly completing normally or exceptionally, probing completion status or results, or awaiting completion of a stage".)

    Note that the CompletionStage.toCompletableFuture() "back-door" may "throw an UnsupportedOperationException if an implementation does not inter-operate with CompletableFuture".

    In production code, you normally never should have to await/join/get results returned as CompletionStage. If you are, you're probably using an API wrong. (Valid deviations to this rule include scenarios where you bridge an async API to an existing legacy API, which you cannot change, using synchronous signature methods which do not return an async types such as CompletionStage or (a subtype of) Future.)

    See the CompletionStageAwaitExampleTest for an illustration how to use this.