Merge pull request #7176 from ieQu1/fix-add-hook

test(emqx_hook): Fix flaky t_add_hook_order test
This commit is contained in:
Dmitrii 2022-03-01 15:52:22 +01:00 committed by GitHub
commit b4439c6710
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 4 deletions

View File

@ -46,7 +46,9 @@ end_per_testcase(_) ->
% error('TODO'). % error('TODO').
t_add_hook_order(_) -> t_add_hook_order(_) ->
?assert(proper:quickcheck(add_hook_order_prop(), [{on_output, fun ct:print/2}])). ?assert(proper:quickcheck(add_hook_order_prop(),
[{on_output, fun ct:print/2},
{numtests, 1000}])).
add_hook_order_prop() -> add_hook_order_prop() ->
%% Note: order is inversed, since higher prio hooks run first: %% Note: order is inversed, since higher prio hooks run first:
@ -55,11 +57,10 @@ add_hook_order_prop() ->
(Prio1 =:= Prio2 andalso {M1, F1} =< {M2, F2}) (Prio1 =:= Prio2 andalso {M1, F1} =< {M2, F2})
end, end,
?FORALL( ?FORALL(
Hooks0, list({range(-1, 5), atom(), atom()}), Hooks, hooks(),
try try
Hooks = sets:to_list(sets:from_list(Hooks0)),
{ok, _} = emqx_hooks:start_link(), {ok, _} = emqx_hooks:start_link(),
[emqx:hook(prop_hook, {M, F, []}, Prio) || {Prio, M, F} <- Hooks], [ok = emqx:hook(prop_hook, {M, F, []}, Prio) || {Prio, M, F} <- Hooks],
Callbacks = emqx_hooks:lookup(prop_hook), Callbacks = emqx_hooks:lookup(prop_hook),
Order = [{Prio, M, F} || {callback, {M, F, _}, _Filter, Prio} <- Callbacks], Order = [{Prio, M, F} || {callback, {M, F, _}, _Filter, Prio} <- Callbacks],
?assertEqual(lists:sort(Comparator, Hooks), ?assertEqual(lists:sort(Comparator, Hooks),
@ -69,6 +70,15 @@ add_hook_order_prop() ->
emqx_hooks:stop() emqx_hooks:stop()
end). end).
hooks() ->
?SUCHTHAT(L0, list({range(-1, 5), atom(), atom()}),
begin
%% Duplicate callbacks are ignored, so check that
%% all callbacks are unique:
L = [{M, F} || {_Prio, M, F} <- L0],
length(lists:usort(L)) =:= length(L0)
end).
t_add_put_del_hook(_) -> t_add_put_del_hook(_) ->
{ok, _} = emqx_hooks:start_link(), {ok, _} = emqx_hooks:start_link(),
ok = emqx:hook(test_hook, {?MODULE, hook_fun1, []}), ok = emqx:hook(test_hook, {?MODULE, hook_fun1, []}),