Skip to content

Commit 9a02e3e

Browse files
committed
rabbit_logger_std_h: Mock calls to io:put_chars/2 during testing
[Why] Before `rabbitmq_prelaunch` was moved from `deps/rabbit/apps` to `deps`, it would inherit compile flags from `deps/rabbit`. Therefore, when `rabbit` was tested, `rabbit_logger_std_h` simply replaced the calls to `io:put_chars/2` with an internal macro to also call `ct:log/2`. This is not possible anymore after the move and the move broke the console-based testcases. [How] `rabbit_logger_std_h` now uses an indirect internal call to a wrapper of `io:put_chars/2`. This allows the `logging_SUITE` to mock that call and add the addition call to `ct:log/2`. We need to do an explicit `?MODULE:io_put_chars/2` even though a local call would work, otherwise meck can't intercept the calls.
1 parent 89c6c59 commit 9a02e3e

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

deps/rabbit/test/logging_SUITE.erl

+21-1
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,12 @@ groups() ->
107107

108108
init_per_suite(Config) ->
109109
rabbit_ct_helpers:log_environment(),
110-
rabbit_ct_helpers:run_setup_steps(Config).
110+
Config1 = rabbit_ct_helpers:run_setup_steps(Config),
111+
meck_rabbit_logger_std_h(),
112+
Config1.
111113

112114
end_per_suite(Config) ->
115+
unmeck_rabbit_logger_std_h(),
113116
Config.
114117

115118
init_per_group(syslog_output, Config) ->
@@ -186,6 +189,23 @@ end_per_testcase(Testcase, Config) ->
186189
end,
187190
rabbit_ct_helpers:testcase_finished(Config1, Testcase).
188191

192+
meck_rabbit_logger_std_h() ->
193+
ok = meck:new(rabbit_logger_std_h, [no_link, passthrough]),
194+
ok = meck:expect(
195+
rabbit_logger_std_h, io_put_chars,
196+
fun(DEVICE, DATA) ->
197+
%% We log to Common Test log as well.
198+
%% This is the file we use to check
199+
%% the message made it to
200+
%% stdout/stderr.
201+
ct:log("~ts", [DATA]),
202+
io:put_chars(DEVICE, DATA)
203+
end).
204+
205+
unmeck_rabbit_logger_std_h() ->
206+
?assert(meck:validate(rabbit_logger_std_h)),
207+
ok = meck:unload(rabbit_logger_std_h).
208+
189209
remove_all_handlers() ->
190210
_ = [logger:remove_handler(Id)
191211
|| #{id := Id} <- logger:get_handler_config()].

deps/rabbitmq_prelaunch/src/rabbit_logger_std_h.erl

+11-15
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,9 @@
2020
-module(rabbit_logger_std_h).
2121

2222
-ifdef(TEST).
23-
-define(io_put_chars(DEVICE, DATA), begin
24-
%% We log to Common Test log as well.
25-
%% This is the file we use to check
26-
%% the message made it to
27-
%% stdout/stderr.
28-
ct:log("~ts", [DATA]),
29-
io:put_chars(DEVICE, DATA)
30-
end).
31-
3223
-export([parse_date_spec/1, parse_day_of_week/2, parse_day_of_month/2, parse_hour/2, parse_minute/2]).
33-
-else.
34-
-define(io_put_chars(DEVICE, DATA), io:put_chars(DEVICE, DATA)).
3524
-endif.
25+
3626
-define(file_write(DEVICE, DATA), file:write(DEVICE, DATA)).
3727
-define(file_datasync(DEVICE), file:datasync(DEVICE)).
3828

@@ -50,6 +40,9 @@
5040
-export([log/2, adding_handler/1, removing_handler/1, changing_config/3,
5141
filter_config/1]).
5242

43+
%% Internal export to allow the use of meck.
44+
-export([io_put_chars/2]).
45+
5346
-define(DEFAULT_CALL_TIMEOUT, 5000).
5447

5548
%%%===================================================================
@@ -549,15 +542,15 @@ ensure_open(Filename, Modes) ->
549542

550543
write_to_dev(Bin,#{dev:=standard_io}=State) ->
551544
try
552-
io:put_chars(user, Bin)
545+
?MODULE:io_put_chars(user, Bin)
553546
catch _E:_R ->
554-
io:put_chars(
547+
?MODULE:io_put_chars(
555548
standard_error, "Failed to write log message to stdout, trying stderr\n"),
556-
io:put_chars(standard_error, Bin)
549+
?MODULE:io_put_chars(standard_error, Bin)
557550
end,
558551
State;
559552
write_to_dev(Bin,#{dev:=DevName}=State) ->
560-
?io_put_chars(DevName, Bin),
553+
?MODULE:io_put_chars(DevName, Bin),
561554
State;
562555
write_to_dev(Bin, State) ->
563556
State1 = #{fd:=Fd} = maybe_ensure_file(State),
@@ -566,6 +559,9 @@ write_to_dev(Bin, State) ->
566559
maybe_notify_error(write,Result,State2),
567560
State2#{synced=>false,write_res=>Result}.
568561

562+
io_put_chars(DevName, Bin) ->
563+
io:put_chars(DevName, Bin).
564+
569565
sync_dev(#{synced:=false}=State) ->
570566
State1 = #{fd:=Fd} = maybe_ensure_file(State),
571567
Result = ?file_datasync(Fd),

0 commit comments

Comments
 (0)