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.

Functions

Waits for Fun to return ExpectedValue.

Waits for Fun to return a value validator accepts.

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 opts(Expected) ::
          #{validator => validator(Expected),
            expected_value => Expected,
            time_left => timeout(),
            sleep_time => sleep_time(),
            on_error => fun(() -> term()),
            no_throw => boolean(),
            name => name()}.
-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.

-type validator(Expected) :: fun((Expected | term()) -> boolean()).

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

-type wait_error(Expected) :: {name(), Expected, history(), undefined | term()}.

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

Link to this function

wait_until(Fun, Expected)

View Source
-spec wait_until(callback(Expected), Expected) -> return(Expected).

Waits for Fun to return ExpectedValue.

See also: wait_until/3.

Link to this function

wait_until(Fun, Expected, Opts)

View Source
-spec wait_until(callback(Expected), Expected, opts(Expected)) -> return(Expected).

Waits for Fun to return a value validator accepts.

  • If the result of Fun equals ExpectedValue, returns {ok, ExpectedValue}
  • If no value is returned or the result doesn't equal ExpectedValue:
    • If no_throw => true is given, it returns {error, Error}, where Error is of type wait_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)