-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce log_helper, and use it in config_parser_SUITE #4239
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just note, if doing it in parallel, it would stop working because see: https://github.com/arcusfelis/logger_debug_h/blob/main/src/logger_debug_h.erl#L23 currently it is sequential in this case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did indeed manage to get a fail after 1000 retries of many parallel tests. I will investigate the reason, if I have time. It only happens if I do remove handlers. Have you logged the issue at OTP? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just FYI: I confirm that adding/removal of handlers is done in asynchronous processes. |
||
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}) -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't aware of that tool. It looks quite complicated. I'd stick with the simple solution for now. |
||
Receiver ! {log, Event}. | ||
|
||
handler_id() -> | ||
list_to_atom(pid_to_list(self())). |
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)()). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just getting rid of these regexes and actually doing struct matching is such a great win!