-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce log_helper, and use it in config_parser_SUITE
Small tests used logger_ct_backend, but it was meant for big tests: - The default formatter was used in small tests (no app.config), requiring regexp matching. - RPC was used, but it was not needed. Advantages of the new helper: - Concise and simple eunit-like assertions - Does not consume unwanted received messages thanks to pattern matching in 'receive' - Easier set-up and tear-down with one handler per testcase.
- Loading branch information
Showing
3 changed files
with
72 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
%% Helper module for checking expected log messages | ||
%% For assertions, see log_helper.hrl | ||
|
||
-module(log_helper). | ||
-compile([export_all, nowarn_export_all]). | ||
|
||
%% Call in init_per_testcase | ||
set_up() -> | ||
logger:add_handler(handler_id(), ?MODULE, #{receiver => self()}). | ||
|
||
%% Call in end_per_testcase | ||
tear_down() -> | ||
logger:remove_handler(handler_id()). | ||
|
||
%% Logger callback | ||
log(Event, #{receiver := Receiver}) -> | ||
Receiver ! {log, Event}. | ||
|
||
handler_id() -> | ||
list_to_atom(pid_to_list(self())). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
%% Assertions for expected logs. Naming follows eunit.hrl | ||
%% For set-up and tear-down, see log_helper.erl | ||
|
||
-define(assertNoLog(LevelPattern, MsgPattern), | ||
case ?receiveLog(LevelPattern, MsgPattern) of | ||
no_log -> ok; | ||
_ -> ct:fail("Received unexpected log") | ||
end). | ||
|
||
-define(assertLog(LevelPattern, MsgPattern), | ||
case ?receiveLog(LevelPattern, MsgPattern) of | ||
no_log -> ct:fail("Expected log not received"); | ||
_ -> ok | ||
end). | ||
|
||
-define(receiveLog(LevelPattern, MsgPattern), | ||
?wrap(receive | ||
{log, #{level := Level = LevelPattern, | ||
msg := {report, Msg = MsgPattern}}} -> | ||
ct:log("Received ~p log: ~p", [Level, Msg]), | ||
{Level, Msg} | ||
after | ||
0 -> no_log | ||
end)). | ||
|
||
%% Wrap in a fun to avoid unsafe variables | ||
-define(wrap(Expr), (fun() -> Expr end)()). |