Skip to content

Commit

Permalink
Pass negative cases as a separate argument into instrument_helper:sta…
Browse files Browse the repository at this point in the history
…rt/2
  • Loading branch information
arcusfelis committed Jun 18, 2024
1 parent 4f2bd26 commit ffce191
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
8 changes: 6 additions & 2 deletions big_tests/tests/bosh_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ acks_test_cases() ->
%%--------------------------------------------------------------------

init_per_suite(Config) ->
instrument_helper:start(instrumentation_events() ++ negative_instrumentation_events()),
instrument_helper:start(instrumentation_events(), negative_instrumentation_events()),
Config1 = dynamic_modules:save_modules(host_type(), Config),
escalus:init_per_suite([{escalus_user_db, {module, escalus_ejabberd}} | Config1]).

Expand Down Expand Up @@ -196,7 +196,7 @@ create_and_terminate_session(Config) ->
|| {Event, Label} <- instrumentation_events(), Event =/= c2s_message_processing_time],

%% Verify C2S listener is not used
instrument_helper:assert_not_emitted(negative_instrumentation_events(), true),
instrument_helper:assert_not_emitted(negative_instrumentation_events()),

%% Assert the session was terminated.
wait_for_zero_bosh_sessions().
Expand Down Expand Up @@ -371,6 +371,10 @@ interleave_requests(Config) ->
escalus:assert(is_chat_message, [Msg4],
escalus_client:wait_for_stanza(Geralt)),

[instrument_helper:assert(Event, Label, fun(#{byte_size := BS}) -> BS > 0;
(#{time := Time}) -> Time > 0 end)
|| {Event, Label} <- instrumentation_events()],

true = is_bosh_connected(Carol)
end).

Expand Down
32 changes: 18 additions & 14 deletions big_tests/tests/instrument_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
-module(instrument_helper).

-export([declared_events/1, declared_events/2,
start/1, stop/0,
start/1, start/2, stop/0,
assert/3, assert/4,
assert_not_emitted/2, assert_not_emitted/3,
assert_not_emitted/1, assert_not_emitted/2,
wait_for/2, wait_for_new/2,
lookup/2, take/2]).

Expand Down Expand Up @@ -34,25 +34,30 @@ declared_events(Module, Args) ->
Specs = rpc(mim(), Module, instrumentation, Args),
[{Event, Labels} || {Event, Labels, _Config} <- Specs].

%% @doc Only `DeclaredEvents' will be logged, and can be tested with `assert/3'
-spec start([{event_name(), labels()} | module()]) -> ok.
-spec start([{event_name(), labels()}]) -> ok.
start(DeclaredEvents) ->
start(DeclaredEvents, []).

%% @doc Only `DeclaredEvents' will be logged, and can be tested with `assert/3'
-spec start([{event_name(), labels()}], [{event_name(), labels()}]) -> ok.
start(DeclaredEvents, NegativeEvents) ->
mongoose_helper:inject_module(ets_helper),
mongoose_helper:inject_module(?HANDLER_MODULE),
ets_helper:new(?STATUS_TABLE),
[ets:insert(?STATUS_TABLE, {Event, negative}) || Event <- NegativeEvents],
[ets:insert(?STATUS_TABLE, {Event, untested}) || Event <- DeclaredEvents],
rpc(mim(), mongoose_instrument, add_handler,
[event_table, #{declared_events => DeclaredEvents}]).
[event_table, #{declared_events => DeclaredEvents ++ NegativeEvents}]).

-spec stop() -> ok.
stop() ->
#{tested := Tested, untested := Untested} = classify_events(),
#{tested := Tested, untested := Untested, negative := Negative} = classify_events(),
ets_helper:delete(?STATUS_TABLE),
Logged = rpc(mim(), ?HANDLER_MODULE, all_keys, []),
rpc(mim(), mongoose_instrument, remove_handler, [event_table]),
ct:log("Tested instrumentation events:~n~p", [lists:sort(Tested)]),
verify_unlogged(Untested -- Logged),
verify_logged_but_untested(Logged -- Tested).
verify_unlogged((Untested -- Logged) -- Negative),
verify_logged_but_untested((Logged -- Tested) -- Negative).

-spec assert(event_name(), labels(), fun((measurements()) -> boolean())) -> ok.
assert(EventName, Labels, CheckF) ->
Expand All @@ -75,17 +80,16 @@ assert(EventName, Labels, MeasurementsList, CheckF) ->
event_tested(EventName, Labels)
end.

assert_not_emitted(EventName, Labels, MarkAsTested) ->
assert_not_emitted(EventName, Labels) ->
case lookup(EventName, Labels) of
[] ->
event_tested(EventName, Labels);
ok;
Events ->
ct:fail("Measurements emitted but should not ~p", [Events])
end.

assert_not_emitted(Events, MarkAsTested) ->
[assert_not_emitted(Event, Label, MarkAsTested)
|| {Event, Label} <- Events].
assert_not_emitted(Events) ->
[assert_not_emitted(Event, Label) || {Event, Label} <- Events].

%% @doc Remove previous events, and wait for a new one. Use for probes only.
-spec wait_for_new(event_name(), labels()) -> [measurements()].
Expand Down Expand Up @@ -129,7 +133,7 @@ event_tested(EventName, Labels) ->
ok.

classify_events() ->
ets:foldl(fun classify_event/2, #{tested => [], untested => []}, ?STATUS_TABLE).
ets:foldl(fun classify_event/2, #{tested => [], untested => [], negative => []}, ?STATUS_TABLE).

classify_event({Event, Status}, M) ->
M#{Status => [Event | maps:get(Status, M)]}.
6 changes: 4 additions & 2 deletions big_tests/tests/mim_c2s_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ two_users_can_log_and_chat(Config) ->
[instrument_helper:assert(Event, Label, fun(#{byte_size := BS}) -> BS > 0;
(#{time := Time}) -> Time > 0 end)
|| {Event, Label} <- tcp_instrumentation_events() ++ common_instrumentation_events()],
instrument_helper:assert_not_emitted(tls_instrumentation_events(), true)
instrument_helper:assert_not_emitted(tls_instrumentation_events())
end).

too_big_stanza_is_rejected(Config) ->
Expand Down Expand Up @@ -253,4 +253,6 @@ tls_instrumentation_events() ->

common_instrumentation_events() ->
HostType = domain_helper:host_type(),
[{c2s_message_processing_time, #{host_type => HostType}}].
[{c2s_message_processing_time, #{host_type => HostType}},
{xmpp_stanza_size_received, #{}},
{xmpp_stanza_size_sent, #{}}].
4 changes: 2 additions & 2 deletions big_tests/tests/websockets_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ suite() ->
%%--------------------------------------------------------------------

init_per_suite(Config) ->
instrument_helper:start(instrumentation_events() ++ negative_instrumentation_events()),
instrument_helper:start(instrumentation_events(), negative_instrumentation_events()),
Config1 = escalus:init_per_suite(Config),
Config2 = setup_listeners(Config1),
escalus:create_users(Config2, escalus:get_users([alice, geralt, geralt_s, carol])).
Expand Down Expand Up @@ -117,7 +117,7 @@ metrics_test(Config) ->
|| {Event, Label} <- instrumentation_events()],

%% Verify C2S listener is not used
instrument_helper:assert_not_emitted(negative_instrumentation_events(), true),
instrument_helper:assert_not_emitted(negative_instrumentation_events()),
ok
end).

Expand Down

0 comments on commit ffce191

Please sign in to comment.