Skip to content

Commit

Permalink
Merge pull request #4243 from esl/log-helper-parallel
Browse files Browse the repository at this point in the history
Prepare log_helper for parallel tests
  • Loading branch information
jacekwegr authored Mar 13, 2024
2 parents 27c1d85 + 4e94a99 commit 23a4f06
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
33 changes: 33 additions & 0 deletions test/common/ets_helper.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
%% Helper module for an ETS table with an owner process

-module(ets_helper).

-compile([export_all, nowarn_export_all]).

%% API

new(Name) ->
new(Name, []).

new(Name, ExtraOpts) ->
TabOwner = spawn(?MODULE, init_tab, [self(), Name, ExtraOpts]),
receive
table_created -> ok
after
5000 ->
exit(TabOwner, kill),
error("Table owner did not respond")
end.

delete(Name) ->
TabOwner = ets:info(Name, owner),
ets:delete(Name),
TabOwner ! stop,
ok.

%% Helpers

init_tab(Caller, Name, ExtraOpts) ->
ets:new(Name, [named_table, public | ExtraOpts]),
Caller ! table_created,
receive stop -> ok end.
8 changes: 6 additions & 2 deletions test/config_parser_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -251,24 +251,28 @@ init_per_group(dynamic_domains, Config) ->
meck:expect(ejabberd_auth_http, supported_features, fun() -> [] end),
meck:expect(mod_test, supported_features, fun() -> [] end),
Config;
init_per_group(logs, _Config) ->
log_helper:set_up();
init_per_group(_, Config) ->
Config.

end_per_group(dynamic_domains, _Config) ->
meck:unload();
end_per_group(logs, _Config) ->
log_helper:tear_down();
end_per_group(_, _Config) ->
ok.

init_per_testcase(CaseName, Config) ->
case lists:member(CaseName, log_cases()) of
true -> log_helper:set_up();
true -> log_helper:subscribe();
false -> ok
end,
Config.

end_per_testcase(CaseName, _Config) ->
case lists:member(CaseName, log_cases()) of
true -> log_helper:tear_down();
true -> log_helper:unsubscribe();
false -> ok
end.

Expand Down
31 changes: 23 additions & 8 deletions test/log_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@
-module(log_helper).
-compile([export_all, nowarn_export_all]).

%% Call in init_per_testcase
%% Call in init_per_suite/group
set_up() ->
logger:add_handler(handler_id(), ?MODULE, #{receiver => self()}).
ets_helper:new(?MODULE),
ok = logger:add_handler(?MODULE, ?MODULE, #{}).

%% Call in init_per_testcase
subscribe() ->
subscribe(self()).

subscribe(Pid) ->
ets:insert(?MODULE, {Pid}),
ok.

%% Call in end_per_testcase
unsubscribe() ->
unsubscribe(self()).

unsubscribe(Pid) ->
ets:delete(?MODULE, Pid),
ok.

%% Call in end_per_suite/group
tear_down() ->
logger:remove_handler(handler_id()).
logger:remove_handler(?MODULE),
ets_helper:delete(?MODULE).

%% Logger callback
log(Event, #{receiver := Receiver}) ->
Receiver ! {log, Event}.

handler_id() ->
list_to_atom(pid_to_list(self())).
log(Event, #{}) ->
[Receiver ! {log, Event} || {Receiver} <- ets:tab2list(?MODULE)].

0 comments on commit 23a4f06

Please sign in to comment.