From 9479c2e9cce154b70ff6ba0cad16bae32446ac3f Mon Sep 17 00:00:00 2001 From: Philip Kuryloski Date: Thu, 2 Jul 2020 11:33:17 +0200 Subject: [PATCH] Add ?awaitMatch macro awaitMatch(Guard, Expr, Timeout) reevaluates Expr until it matches Guard up to Timeout milliseconds from now. It returns the matched value, in the event said value is useful later in a test. Additionally simplify an instance of ?assertEqual(true, ... to ?assert( --- .../include/rabbit_assert.hrl | 21 +++++++++++++++++++ .../src/rabbit_ct_proper_helpers.erl | 3 +-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 deps/rabbitmq_ct_helpers/include/rabbit_assert.hrl diff --git a/deps/rabbitmq_ct_helpers/include/rabbit_assert.hrl b/deps/rabbitmq_ct_helpers/include/rabbit_assert.hrl new file mode 100644 index 0000000000..a8213e9977 --- /dev/null +++ b/deps/rabbitmq_ct_helpers/include/rabbit_assert.hrl @@ -0,0 +1,21 @@ +-define(awaitMatch(Guard, Expr, Timeout), + begin + ((fun Filter(Horizon) -> + R = Expr, + case (R) of + Guard -> R; + __V -> case erlang:system_time(millisecond) of + Now when Now < Horizon -> + timer:sleep(min(50, Horizon - Now)), + Filter(Horizon); + _ -> + erlang:error({awaitMatch, + [{module, ?MODULE}, + {line, ?LINE}, + {expression, (??Expr)}, + {pattern, (??Guard)}, + {value, __V}]}) + end + end + end)(erlang:system_time(millisecond) + Timeout)) + end). diff --git a/deps/rabbitmq_ct_helpers/src/rabbit_ct_proper_helpers.erl b/deps/rabbitmq_ct_helpers/src/rabbit_ct_proper_helpers.erl index 581af69f2c..91cfae67e3 100644 --- a/deps/rabbitmq_ct_helpers/src/rabbit_ct_proper_helpers.erl +++ b/deps/rabbitmq_ct_helpers/src/rabbit_ct_proper_helpers.erl @@ -20,8 +20,7 @@ -export([run_proper/3]). run_proper(Fun, Args, NumTests) -> - ?assertEqual( - true, + ?assert( proper:counterexample(erlang:apply(Fun, Args), [{numtests, NumTests}, {on_output, fun(".", _) -> ok; % don't print the '.'s on new lines