Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed May 29, 2024
1 parent 22fb94f commit af5d56e
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 196 deletions.
19 changes: 11 additions & 8 deletions src/gen_hook.erl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ run_fold(HookName, Tag, Acc, Params) ->
Key = hook_key(HookName, Tag),
case persistent_term:get(?MODULE, #{}) of
#{Key := Ls} ->
mongoose_metrics:increment_generic_hook_metric(Tag, HookName),
mongoose_instrument_hooks:execute(HookName, Tag),
run_hook(Ls, Acc, Params, Key);
_ ->
{ok, Acc}
Expand All @@ -156,12 +156,15 @@ init([]) ->
erlang:process_flag(trap_exit, true), %% We need to make sure that terminate is called in tests
{ok, #{}}.

handle_call({add_handler, Key, #hook_handler{} = HookHandler}, _From, State) ->
handle_call({add_handler, Key = {Name, Tag}, #hook_handler{} = HookHandler}, _From, State) ->
NewState =
case maps:get(Key, State, []) of
[] ->
NewLs = [HookHandler],
create_hook_metric(Key),
%% NOTE: the following would fail when trying to register both global and host-type
%% handlers for the same hook. This is fine, because the two types of tags
%% shouldn't be mixed anyway for a single hook.
mongoose_instrument_hooks:set_up(Name, Tag),
maps:put(Key, NewLs, State);
Ls ->
case lists:search(fun_is_handler_equal_to(HookHandler), Ls) of
Expand All @@ -178,7 +181,7 @@ handle_call({add_handler, Key, #hook_handler{} = HookHandler}, _From, State) ->
end,
maybe_insert_immediately(NewState),
{reply, ok, NewState};
handle_call({delete_handler, Key, #hook_handler{} = HookHandler}, _From, State) ->
handle_call({delete_handler, Key = {Name, Tag}, #hook_handler{} = HookHandler}, _From, State) ->
NewState =
case maps:get(Key, State, []) of
[] ->
Expand All @@ -190,6 +193,10 @@ handle_call({delete_handler, Key, #hook_handler{} = HookHandler}, _From, State)
%% by using `erlang:fun_info/2`
Pred = fun_is_handler_equal_to(HookHandler),
{_, NewLs} = lists:partition(Pred, Ls),
case NewLs of
[] -> mongoose_instrument_hooks:tear_down(Name, Tag);
_ -> ok
end,
maps:put(Key, NewLs, State)
end,
maybe_insert_immediately(NewState),
Expand Down Expand Up @@ -324,7 +331,3 @@ extend_extra({HookName, Tag, _Function, OriginalExtra, _Priority}) ->
%% only the new keys from the ExtraExtension map will be added
%% to the NewExtra map
maps:merge(ExtraExtension, OriginalExtra).

-spec create_hook_metric(Key :: key()) -> any().
create_hook_metric({HookName, Tag}) ->
mongoose_metrics:create_generic_hook_metric(Tag, HookName).
58 changes: 18 additions & 40 deletions test/auth_tokens_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,67 +39,51 @@ groups() ->
]}
].

init_per_suite(C) ->
init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(jid),
mongoose_config:set_opts(opts()),
C.
async_helper:start(Config, [{mongoose_instrument, start_link, []},
{mongooseim_helper, start_link_loaded_hooks, []}]).

end_per_suite(_C) ->
end_per_suite(Config) ->
async_helper:stop_all(Config),
mongoose_config:erase_opts().

opts() ->
#{{modules, host_type()} => #{?TESTED => config_parser_helper:default_mod_config(?TESTED)}}.
#{{modules, host_type()} => #{?TESTED => config_parser_helper:default_mod_config(?TESTED)},
instrumentation => config_parser_helper:default_config([instrumentation])}.

init_per_testcase(Test, Config)
when Test =:= serialize_deserialize_property;
Test =:= validation_test;
Test =:= validation_property;
Test =:= choose_key_by_token_type ->
mock_mongoose_metrics(),
Config1 = async_helper:start(Config, [{mongooseim_helper, start_link_loaded_hooks, []}]),
mock_keystore(),
mock_rdbms_backend(),
Config1;

Config;
init_per_testcase(validity_period_test, Config) ->
mock_rdbms_backend(),
mock_mongoose_metrics(),
mock_gen_iq_handler(),
async_helper:start(Config, [{mongooseim_helper, start_link_loaded_hooks, []}]);

Config;
init_per_testcase(revoked_token_is_not_valid, Config) ->
mock_mongoose_metrics(),
mock_tested_backend(),
Config1 = async_helper:start(Config, [{mongooseim_helper, start_link_loaded_hooks, []}]),
mock_keystore(),
Config1;

Config;
init_per_testcase(_, C) -> C.

end_per_testcase(Test, C)
end_per_testcase(Test, _C)
when Test =:= serialize_deserialize_property;
Test =:= validation_test;
Test =:= validation_property;
Test =:= choose_key_by_token_type ->
meck:unload(mongoose_metrics),
meck:unload(mod_auth_token_backend);
end_per_testcase(validity_period_test, _C) ->
meck:unload(mod_auth_token_backend),
async_helper:stop_all(C),
C;

end_per_testcase(validity_period_test, C) ->
meck:unload(mod_auth_token_backend),
meck:unload(mongoose_metrics),
meck:unload(gen_iq_handler),
async_helper:stop_all(C),
C;

end_per_testcase(revoked_token_is_not_valid, C) ->
meck:unload(mongoose_metrics),
meck:unload(mod_auth_token_backend),
async_helper:stop_all(C),
C;

end_per_testcase(_, C) -> C.
meck:unload(gen_iq_handler);
end_per_testcase(revoked_token_is_not_valid, _C) ->
meck:unload(mod_auth_token_backend);
end_per_testcase(_, _) ->
ok.

%%
%% Tests
Expand Down Expand Up @@ -218,12 +202,6 @@ negative_prop(Name, Prop) ->
Props = proper:conjunction([{Name, Prop}]),
[[{Name, _}]] = proper:quickcheck(Props, [verbose, long_result, {numtests, 50}]).

mock_mongoose_metrics() ->
meck:new(mongoose_metrics, []),
meck:expect(mongoose_metrics, create_generic_hook_metric, fun (_, _) -> ok end),
meck:expect(mongoose_metrics, increment_generic_hook_metric, fun (_, _) -> ok end),
ok.

mock_rdbms_backend() ->
meck:new(mod_auth_token_backend, []),
meck:expect(mod_auth_token_backend, start, fun(_, _) -> ok end),
Expand Down
22 changes: 11 additions & 11 deletions test/component_reg_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,24 @@
all() ->
[ registering, registering_with_local ].

init_per_suite(C) ->
init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(jid),
ok = mnesia:create_schema([node()]),
ok = mnesia:start(),
mongoose_config:set_opts(opts()),
meck:new(mongoose_domain_api, [no_link]),
meck:expect(mongoose_domain_api, get_host_type,
fun(_) -> {error, not_found} end),
meck:expect(mongoose_domain_api, get_host_type, fun(_) -> {error, not_found} end),
application:ensure_all_started(exometer_core),
mongooseim_helper:start_link_loaded_hooks(),
ejabberd_router:start_link(),
C.
async_helper:start(Config, [{mongoose_instrument, start_link, []},
{mongooseim_helper, start_link_loaded_hooks, []},
{ejabberd_router, start_link, []}]).

init_per_testcase(_, C) ->
init_per_testcase(_, Config) ->
mongoose_router:start(),
mongooseim_helper:start_link_loaded_hooks(),
C.
Config.

end_per_suite(_C) ->
end_per_suite(Config) ->
async_helper:stop_all(Config),
mnesia:stop(),
mnesia:delete_schema([node()]),
meck:unload(),
Expand All @@ -36,7 +35,8 @@ end_per_suite(_C) ->
opts() ->
#{all_metrics_are_global => false,
component_backend => mnesia,
routing_modules => [xmpp_router_a, xmpp_router_b, xmpp_router_c]}.
routing_modules => [xmpp_router_a, xmpp_router_b, xmpp_router_c],
instrumentation => config_parser_helper:default_config([instrumentation])}.

registering(_C) ->
Dom = <<"aaa.bbb.com">>,
Expand Down
19 changes: 10 additions & 9 deletions test/event_pusher_sns_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,15 @@ calls_callback_module_to_retrieve_attributes_for_message(Config) ->

init_per_suite(Config) ->
{ok, _} = application:ensure_all_started(jid),
Config.
mongoose_config:set_opts(opts()),
async_helper:start(Config, [{mongoose_instrument, start_link, []},
{mongooseim_helper, start_link_loaded_hooks, []}]).

end_per_suite(_) ->
ok.
end_per_suite(Config) ->
async_helper:stop_all(Config),
mongoose_config:erase_opts().

init_per_testcase(CaseName, Config) ->
mongooseim_helper:start_link_loaded_hooks(),
meck:new(erlcloud_sns, [non_strict, passthrough]),
meck:new([mongoose_wpool, mongoose_metrics], [stub_all]),
meck:expect(erlcloud_sns, new, fun(_, _, _) -> mod_aws_sns_SUITE_erlcloud_sns_new end),
Expand Down Expand Up @@ -197,18 +199,17 @@ sns_config(_) ->
common_sns_opts().

start_modules(SNSExtra) ->
mongoose_config:set_opts(opts(SNSExtra)),
mongoose_config:set_opt({modules, host_type()}, modules(SNSExtra)),
mongoose_modules:start().

stop_modules() ->
mongoose_modules:stop(),
mongoose_config:erase_opts().
mongoose_modules:stop().

opts(SNSExtra) ->
opts() ->
#{hosts => [host_type()],
host_types => [],
all_metrics_are_global => false,
{modules, host_type()} => modules(SNSExtra)}.
instrumentation => config_parser_helper:default_config([instrumentation])}.

modules(SNSExtra) ->
gen_mod_deps:resolve_deps(host_type(), #{mod_event_pusher => module_opts(SNSExtra)}).
Expand Down
Loading

0 comments on commit af5d56e

Please sign in to comment.