View Source wait_helper (wait_helper v0.2.1)
Helper module to implement waiting conditions.
It periodically queries and validates values from a callback until the condition is met or a timeout passes.
Common method when testing for eventual consistency.
Summary
Types
A function that returns a value to verify.
History of results.
Condition name, defaults to timeout.
How long to wait between queries for new results.
Validation to do against the callbacks return value.
An error type that can be returned or thrown when the condition eventually fails.
Types
-type callback(Expected) :: fun(() -> Expected | term()).
A function that returns a value to verify.
By default, the return value will be checked for equality against the expected value, but a custom validator/1 function can be specified.
The return value of this callback is what will be returned by the whole wait_until operation.
-type history() :: [term()].
History of results.
Note that this history will be simplified, that is, if elements repeat consecutively, they will be agregated to the history only once together with a count.
-type name() :: atom().
Condition name, defaults to timeout.
-type return(Expected) :: {ok, Expected} | {error, wait_error(Expected)} | no_return().
-type sleep_time() :: non_neg_integer().
How long to wait between queries for new results.
Validation to do against the callbacks return value.
This is useful when an intermediate value of the callback is desired in the return value, but the condition to be checked requires further computation that is not needed outside of the scope of the condition.
For example:
Validator = fun(ReturnValue) -> contains(ReturnValue, ...) end,
{ok, Result} = mongoose_helper:wait_until(
fun() -> do_get_value(...) end,
expected,
#{validator => Validator}),where Result is the return value of the original callback, after we know it is accepted by the validator
An error type that can be returned or thrown when the condition eventually fails.
The last element of the tuple is the return value of the on_error callback given to opts/1, if given, otherwise undefined.
Functions
Waits for Fun to return ExpectedValue.
See also: wait_until/3.
Waits for Fun to return a value validator accepts.
- If the result of
FunequalsExpectedValue, returns{ok, ExpectedValue} - If no value is returned or the result doesn't equal
ExpectedValue: - If
no_throw => trueis given, it returns{error, Error}, whereErroris of typewait_error/1. - If throws the following error:
wait_error/1.
Example:
Opts = #{no_throw => true, time_left => timer:seconds(2)},
{error, _} = wait_until(fun() -> ... end, SomeVal, Opts)