Package ch.powerunit.extensions.async.lang

This this the main package of the powerunit-extensions-async library that exposes easy async test operation.

The framework exposes DSL to do repeated test of condition (possibly in another thread), for example to wait of an asynchronously result. The builder mainly returns CompletableFuture and let the caller handle it, but also provides some shortcut methods to directly retrieve the result.

Basic usage

 
 Optional<MyResultClass> result = WaitResult.
   of(MyCallable).
   expecting(MyPredicate).
   repeat(2).
   every(2,TimeUnit.MILLISECONDS).
   get();
 
 
This sample defines an execution of maximal 2 retry, waiting 2 ms between the retry and return the result of the execution. The Optional will be present if a result is accepted by the predicate. In case of error during the execution of the Callable an AssertionError will be thrown. Everything will be executed in this thread.

The sequence of actions will be :

  1. Execution of the Callable. In case of Exception, an AssertionError is thrown (the actions are terminated).
  2. Verify of the result of the execution is accepted by the Predicate. If this is the case, return it as an Optional (the actions are terminated).
  3. Wait for 2 ms by sleeping the thread
  4. Execution of the Callable. In case of Exception, an AssertionError is thrown (the actions are terminated).
  5. Verify of the result of the execution is accepted by the Predicate. If this is the case, return it as an Optional (the actions are terminated).
  6. Finally as no result were accepted, return an empty Optional.

Options

It is also possible to specify with the DSL, if error must be ignored or not, and which executor must be used for the asynchronous operations.

The general form to use this DSL is :

 
 ... result = 
   WaitResult.
     of(MyCallable). <-- Specify the action to be repeated
     [ignoreException().] <-- May specify how to handle the exception
     expecting(MyPredicate). <-- Specify the way to control the result
     repeat(2). <-- Specify how much time the retry should be done
     every(2,TimeUnit.MILLISECONDS). <--Specify how much time to wait between the retry
     usingDefaultExecutor(). <-- Specify the executor to be used for the asynchronous actions
     asyncExec(); <-- Finally return the way to wait for the result
 
 
Since:
1.0.0 Before this version, the main package was ch.powerunit.extensions.async ; Also starting from version 1.0.0 this library doesn't support version before Java 9.
See Also:
WaitResult which is the main entry point of this library., CompletableFuture
Skip navigation links

Copyright © 2021. All rights reserved.