test: ensure emqx_metrics gen_server stopped after each test

This commit is contained in:
Zaiming (Stone) Shi 2022-04-28 13:36:46 +02:00 committed by Shawn
parent 4e65322667
commit 1e170da9e8
3 changed files with 27 additions and 8 deletions

View File

@ -279,7 +279,14 @@ start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
-spec stop() -> ok. -spec stop() -> ok.
stop() -> gen_server:stop(?SERVER). stop() ->
try
gen_server:stop(?SERVER)
catch
exit:R when R =:= noproc orelse R =:= timeout ->
%% pid is killed after timeout
ok
end.
%% BACKW: v4.3.0 %% BACKW: v4.3.0
upgrade_retained_delayed_counter_type() -> upgrade_retained_delayed_counter_type() ->

View File

@ -166,7 +166,16 @@ t_trans(_) ->
). ).
with_metrics_server(Fun) -> with_metrics_server(Fun) ->
_ = supervisor:terminate_child(emqx_kernel_sup, emqx_metrics), try
supervisor:terminate_child(emqx_kernel_sup, emqx_metrics)
catch
exit:_ ->
ok
end,
{ok, _} = emqx_metrics:start_link(), {ok, _} = emqx_metrics:start_link(),
try
_ = Fun(), _ = Fun(),
ok = emqx_metrics:stop(). ok
after
ok = emqx_metrics:stop()
end.

View File

@ -2,10 +2,13 @@
set -euo pipefail set -euo pipefail
files="$(git diff --cached --name-only | grep -E '.*\.erl' || true)" OPT="${1:--c}"
if [[ "${files}" == '' ]]; then
files_dirty="$(git diff --name-only | grep -E '.*\.erl' || true)"
files_cached="$(git diff --cached --name-only | grep -E '.*\.erl' || true)"
if [[ "${files_dirty}" == '' ]] && [[ "${files_cached}" == '' ]]; then
exit 0 exit 0
fi fi
files="$(echo -e "$files" | xargs)" files="$(echo -e "${files_dirty} \n ${files_cached}" | xargs)"
# shellcheck disable=SC2086 # shellcheck disable=SC2086
./scripts/erlfmt -c $files ./scripts/erlfmt $OPT $files