Class CompletionStageTestAwaiter<T>
- java.lang.Object
-
- org.opendaylight.infrautils.testutils.concurrent.CompletionStageTestAwaiter<T>
-
public final class CompletionStageTestAwaiter<T> extends Object
Test utility to await the completion of aCompletionStage
.Note that if you have a
CompletableFuture
instead of aCompletionStage
, then you could just useCompletableFuture.join()
, which does the same thing as theawait500ms(CompletionStage)
here. However, to test operations which return only aCompletionStage
but not a fullCompletableFuture
, 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 asCompletionStage
or (a subtype of)Future
.)See the
CompletionStageAwaitExampleTest
for an illustration how to use this.- Author:
- Michael Vorburger.ch
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
await(CompletionStage<T> completionStage, long timeout, TimeUnit unit)
Await a configurable amount of time for the CompletionStage argument to complete.static <T> T
await500ms(CompletionStage<T> completionStage)
Await max.
-
-
-
Method Detail
-
await500ms
public static <T> T await500ms(CompletionStage<T> completionStage) throws TimeoutException
Await max. 500ms for the CompletionStage argument to complete.- Throws:
CompletionException
- if it completed unsuccessfullyCancellationException
- if it got cancelled while awaiting completionTimeoutException
- if it timed out awaiting completion
-
await
public static <T> T await(CompletionStage<T> completionStage, long timeout, TimeUnit unit) throws TimeoutException
Await a configurable amount of time for the CompletionStage argument to complete.- Throws:
CompletionException
- if it completed unsuccessfullyCancellationException
- if it got cancelled while awaiting completionTimeoutException
- if it timed out awaiting completion
-
-