diff --git a/src/gen_hook.erl b/src/gen_hook.erl index 7c5157ed7a0..af014d89d55 100644 --- a/src/gen_hook.erl +++ b/src/gen_hook.erl @@ -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} @@ -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 @@ -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 [] -> @@ -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), @@ -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). diff --git a/test/auth_tokens_SUITE.erl b/test/auth_tokens_SUITE.erl index 98ba07a62aa..ffa5156fe8e 100644 --- a/test/auth_tokens_SUITE.erl +++ b/test/auth_tokens_SUITE.erl @@ -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 @@ -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), diff --git a/test/component_reg_SUITE.erl b/test/component_reg_SUITE.erl index 994e5d07df5..36e8b5479ee 100644 --- a/test/component_reg_SUITE.erl +++ b/test/component_reg_SUITE.erl @@ -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(), @@ -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">>, diff --git a/test/event_pusher_sns_SUITE.erl b/test/event_pusher_sns_SUITE.erl index bcbaef342ac..d5a5187abcc 100644 --- a/test/event_pusher_sns_SUITE.erl +++ b/test/event_pusher_sns_SUITE.erl @@ -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), @@ -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)}). diff --git a/test/gen_hook_SUITE.erl b/test/gen_hook_SUITE.erl index cfd360c4061..7d8dce3e2f8 100644 --- a/test/gen_hook_SUITE.erl +++ b/test/gen_hook_SUITE.erl @@ -3,8 +3,9 @@ -include_lib("eunit/include/eunit.hrl"). --define(HOOK_TAG1, global). --define(HOOK_TAG2, <<"some tag">>). +-define(HOOK_TAG, global). +-define(HOST_TYPE1, <<"first">>). +-define(HOST_TYPE2, <<"second">>). -define(assertEqualLists(L1, L2), ?assertEqual(lists:sort(L1), lists:sort(L2))). @@ -24,7 +25,7 @@ all() -> init_per_suite(Config) -> application:ensure_all_started(exometer_core), - mongoose_config:set_opts(#{all_metrics_are_global => false}), + mongoose_config:set_opts(opts()), Config. end_per_suite(Config) -> @@ -33,6 +34,7 @@ end_per_suite(Config) -> Config. init_per_testcase(_, Config) -> + mongoose_instrument:start_link(), mongooseim_helper:start_link_loaded_hooks(), Config. @@ -40,6 +42,8 @@ end_per_testcase(_, Config) -> meck:unload(), Config. +opts() -> + #{instrumentation => config_parser_helper:default_config([instrumentation])}. %%---------------------------------------------------------------- %% test cases @@ -51,47 +55,49 @@ single_handler_can_be_added_and_removed(_) -> %% check that there are no hook handlers added yet ?assertEqual([], get_handlers_for_all_hooks()), %% add various hook handlers - ?assertEqual(ok, gen_hook:add_handler(calculate, ?HOOK_TAG1, MultiplyHandlerFn, + ?assertEqual(ok, gen_hook:add_handler(calculate, ?HOST_TYPE1, MultiplyHandlerFn, #{id => 2}, 2)), - ?assertEqual(ok, gen_hook:add_handler(calculate, ?HOOK_TAG1, PlusHandlerFn, + ?assertEqual(ok, gen_hook:add_handler(calculate, ?HOST_TYPE1, PlusHandlerFn, #{id => 1}, 1)), - ?assertEqual(ok, gen_hook:add_handler(calculate, ?HOOK_TAG2, PlusHandlerFn, + ?assertEqual(ok, gen_hook:add_handler(calculate, ?HOST_TYPE2, PlusHandlerFn, #{id => 1}, 1)), %% check that hook handlers are added Tag1Handlers = [%% this list must be sorted by priority {hook_handler, 1, PlusHandlerFn, - #{hook_name => calculate, hook_tag => ?HOOK_TAG1, id => 1}}, + #{hook_name => calculate, hook_tag => ?HOST_TYPE1, + host_type => ?HOST_TYPE1, id => 1}}, {hook_handler, 2, MultiplyHandlerFn, - #{hook_name => calculate, hook_tag => ?HOOK_TAG1, id => 2}}], - AllHandlers = [{{calculate, ?HOOK_TAG1}, Tag1Handlers}, - {{calculate, ?HOOK_TAG2}, + #{hook_name => calculate, hook_tag => ?HOST_TYPE1, + host_type => ?HOST_TYPE1, id => 2}}], + AllHandlers = [{{calculate, ?HOST_TYPE1}, Tag1Handlers}, + {{calculate, ?HOST_TYPE2}, [{hook_handler, 1, PlusHandlerFn, - #{hook_name => calculate, hook_tag => ?HOOK_TAG2, - host_type =>?HOOK_TAG2, id => 1}}]}], + #{hook_name => calculate, hook_tag => ?HOST_TYPE2, + host_type => ?HOST_TYPE2, id => 1}}]}], ?assertEqualLists(AllHandlers, get_handlers_for_all_hooks()), %% try to add some hook handler second time and check that nothing has changed - ?assertEqual(ok, gen_hook:add_handler(calculate, ?HOOK_TAG1, MultiplyHandlerFn, + ?assertEqual(ok, gen_hook:add_handler(calculate, ?HOST_TYPE1, MultiplyHandlerFn, #{id => 2}, 2)), ?assertEqualLists(AllHandlers, get_handlers_for_all_hooks()), - %% try to remove hook handler for ?HOOK_TAG2 and check that it's removed - ?assertEqual(ok, gen_hook:delete_handler(calculate, ?HOOK_TAG2, PlusHandlerFn, + %% try to remove hook handler for ?HOST_TYPE2 and check that it's removed + ?assertEqual(ok, gen_hook:delete_handler(calculate, ?HOST_TYPE2, PlusHandlerFn, #{id => 1}, 1)), - ?assertEqualLists([{{calculate, ?HOOK_TAG1}, Tag1Handlers}, - {{calculate, ?HOOK_TAG2}, []}], + ?assertEqualLists([{{calculate, ?HOST_TYPE1}, Tag1Handlers}, + {{calculate, ?HOST_TYPE2}, []}], get_handlers_for_all_hooks()), - %% try to remove hook handler for ?HOOK_TAG2 second time + %% try to remove hook handler for ?HOST_TYPE2 second time %% and check that nothing has changed - ?assertEqual(ok, gen_hook:delete_handler(calculate, ?HOOK_TAG2, PlusHandlerFn, + ?assertEqual(ok, gen_hook:delete_handler(calculate, ?HOST_TYPE2, PlusHandlerFn, #{id => 1}, 1)), - ?assertEqualLists([{{calculate, ?HOOK_TAG1}, Tag1Handlers}, - {{calculate, ?HOOK_TAG2}, []}], + ?assertEqualLists([{{calculate, ?HOST_TYPE1}, Tag1Handlers}, + {{calculate, ?HOST_TYPE2}, []}], get_handlers_for_all_hooks()), - %% try to remove hook handlers for ?HOOK_TAG1 and check that they are removed - ?assertEqual(ok, gen_hook:delete_handler(calculate, ?HOOK_TAG1, MultiplyHandlerFn, + %% try to remove hook handlers for ?HOST_TYPE1 and check that they are removed + ?assertEqual(ok, gen_hook:delete_handler(calculate, ?HOST_TYPE1, MultiplyHandlerFn, #{id => 2}, 2)), - ?assertEqual(ok, gen_hook:delete_handler(calculate, ?HOOK_TAG1, PlusHandlerFn, + ?assertEqual(ok, gen_hook:delete_handler(calculate, ?HOST_TYPE1, PlusHandlerFn, #{id => 1}, 1)), - ?assertEqualLists([{{calculate, ?HOOK_TAG1}, []}, {{calculate, ?HOOK_TAG2}, []}], + ?assertEqualLists([{{calculate, ?HOST_TYPE1}, []}, {{calculate, ?HOST_TYPE2}, []}], get_handlers_for_all_hooks()). multiple_handlers_can_be_added_and_removed(_) -> @@ -101,32 +107,34 @@ multiple_handlers_can_be_added_and_removed(_) -> %% check that there are no hook handlers added yet ?assertEqual([], get_handlers_for_all_hooks()), %% add various hook handlers - HookHandlers = [{calculate, ?HOOK_TAG1, MultiplyHandlerFn, #{id => 2}, 2}, - {calculate, ?HOOK_TAG2, PlusHandlerFn, #{id => 1}, 1}, - {calculate, ?HOOK_TAG1, PlusHandlerFn, #{id => 1}, 1}], + HookHandlers = [{calculate, ?HOST_TYPE1, MultiplyHandlerFn, #{id => 2}, 2}, + {calculate, ?HOST_TYPE2, PlusHandlerFn, #{id => 1}, 1}, + {calculate, ?HOST_TYPE1, PlusHandlerFn, #{id => 1}, 1}], ?assertEqual(ok, gen_hook:add_handlers(HookHandlers)), %% check that hook handlers are added Tag1Handlers = [%% this list must be sorted by priority {hook_handler, 1, PlusHandlerFn, - #{hook_name => calculate, hook_tag => ?HOOK_TAG1, id => 1}}, + #{hook_name => calculate, hook_tag => ?HOST_TYPE1, + host_type => ?HOST_TYPE1, id => 1}}, {hook_handler, 2, MultiplyHandlerFn, - #{hook_name => calculate, hook_tag => ?HOOK_TAG1, id => 2}}], - AllHandlers = [{{calculate, ?HOOK_TAG1}, Tag1Handlers}, - {{calculate, ?HOOK_TAG2}, + #{hook_name => calculate, hook_tag => ?HOST_TYPE1, + host_type => ?HOST_TYPE1, id => 2}}], + AllHandlers = [{{calculate, ?HOST_TYPE1}, Tag1Handlers}, + {{calculate, ?HOST_TYPE2}, [{hook_handler, 1, PlusHandlerFn, - #{hook_name => calculate, hook_tag => ?HOOK_TAG2, - host_type =>?HOOK_TAG2, id => 1}}]}], + #{hook_name => calculate, hook_tag => ?HOST_TYPE2, + host_type =>?HOST_TYPE2, id => 1}}]}], ?assertEqualLists(AllHandlers, get_handlers_for_all_hooks()), %% try to add hook handlers second time and check that nothing has changed ?assertEqual(ok, gen_hook:add_handlers(HookHandlers)), ?assertEqualLists(AllHandlers, get_handlers_for_all_hooks()), %% try to remove hook handlers and check that they are removed ?assertEqual(ok, gen_hook:delete_handlers(HookHandlers)), - ?assertEqualLists([{{calculate, ?HOOK_TAG1}, []}, {{calculate, ?HOOK_TAG2}, []}], + ?assertEqualLists([{{calculate, ?HOST_TYPE1}, []}, {{calculate, ?HOST_TYPE2}, []}], get_handlers_for_all_hooks()), %% try to remove hook handlers second time and check that nothing has changed ?assertEqual(ok, gen_hook:delete_handlers(HookHandlers)), - ?assertEqualLists([{{calculate, ?HOOK_TAG1}, []}, {{calculate, ?HOOK_TAG2}, []}], + ?assertEqualLists([{{calculate, ?HOST_TYPE1}, []}, {{calculate, ?HOST_TYPE2}, []}], get_handlers_for_all_hooks()). local_fun_references_causes_error(_) -> @@ -137,23 +145,24 @@ local_fun_references_causes_error(_) -> ?assertEqual([], get_handlers_for_all_hooks()), %% try to add multiple hook handlers, when one of them uses local function reference LocalFunctionReference = fun hook_handler_plus/3, - HookHandlers = [{calculate, ?HOOK_TAG1, MultiplyHandlerFn, #{id => 2}, 2}, - {calculate, ?HOOK_TAG2, LocalFunctionReference, #{id => 1}, 1}, - {calculate, ?HOOK_TAG1, PlusHandlerFn, #{id => 1}, 1}], + HookHandlers = [{calculate, ?HOST_TYPE1, MultiplyHandlerFn, #{id => 2}, 2}, + {calculate, ?HOST_TYPE2, LocalFunctionReference, #{id => 1}, 1}, + {calculate, ?HOST_TYPE1, PlusHandlerFn, #{id => 1}, 1}], ?assertError(#{what := only_external_function_references_allowed, function := LocalFunctionReference}, gen_hook:add_handlers(HookHandlers)), %% check that handlers in the list are partially added (till error occurs) - ?assertEqual([{{calculate, ?HOOK_TAG1}, + ?assertEqual([{{calculate, ?HOST_TYPE1}, [{hook_handler, 2, MultiplyHandlerFn, - #{hook_name => calculate, hook_tag => ?HOOK_TAG1, id => 2}}]}], + #{hook_name => calculate, hook_tag => ?HOST_TYPE1, + host_type => ?HOST_TYPE1, id => 2}}]}], get_handlers_for_all_hooks()), %% try to remove the same list of handlers ?assertError(#{what := only_external_function_references_allowed, function := LocalFunctionReference}, gen_hook:delete_handlers(HookHandlers)), %% check that partially added handlers are removed - ?assertEqual([{{calculate, ?HOOK_TAG1}, []}], get_handlers_for_all_hooks()). + ?assertEqual([{{calculate, ?HOST_TYPE1}, []}], get_handlers_for_all_hooks()). anonymous_fun_references_causes_error(_) -> %% check that there are no hook handlers added yet @@ -162,7 +171,7 @@ anonymous_fun_references_causes_error(_) -> AnonymousFunctionReference = fun(Acc, _, _) -> {ok, Acc} end, ?assertError(#{what := only_external_function_references_allowed, function := AnonymousFunctionReference}, - gen_hook:add_handler(calculate, ?HOOK_TAG1, AnonymousFunctionReference, + gen_hook:add_handler(calculate, ?HOOK_TAG, AnonymousFunctionReference, #{id => 2}, 2)), %% check that nothing is added ?assertEqual([], get_handlers_for_all_hooks()). @@ -173,14 +182,14 @@ not_exported_external_fun_references_causes_error(_) -> %% try to add hook handler using function reference for a missing module NotExportedExternalFunctionReference1 = fun missing_module:missing_function/3, ?assertError(#{what := module_is_not_loaded, module := missing_module}, - gen_hook:add_handler(calculate, ?HOOK_TAG1, + gen_hook:add_handler(calculate, ?HOOK_TAG, NotExportedExternalFunctionReference1, #{id => 2}, 2)), %% try to add hook handler using function reference for a missing module NotExportedExternalFunctionReference2 = fun ?MODULE:missing_function/3, ?assertError(#{what := function_is_not_exported, function := NotExportedExternalFunctionReference2}, - gen_hook:add_handler(calculate, ?HOOK_TAG1, + gen_hook:add_handler(calculate, ?HOOK_TAG, NotExportedExternalFunctionReference2, #{id => 2}, 2)), %% check that nothing is added @@ -190,9 +199,9 @@ invalid_hook_handler_parameters_causes_error(_) -> %% check that there are no hook handlers added yet ?assertEqual([], get_handlers_for_all_hooks()), HandlerFn = fun ?MODULE:hook_handler_stop/3, - InvalidHookHandlers = [{calculate, ?HOOK_TAG1, HandlerFn, invalid_extra_param, 2}, - {<<"invalid hook name">>, ?HOOK_TAG1, HandlerFn, #{}, 2}, - {calculate, ?HOOK_TAG1, HandlerFn, #{}, invalid_priority}, + InvalidHookHandlers = [{calculate, ?HOOK_TAG, HandlerFn, invalid_extra_param, 2}, + {<<"invalid hook name">>, ?HOOK_TAG, HandlerFn, #{}, 2}, + {calculate, ?HOOK_TAG, HandlerFn, #{}, invalid_priority}, {calculate, invalid_hook_tag, HandlerFn, #{}, 2}], [?assertError(function_clause, gen_hook:add_handlers([HookHandler])) || HookHandler <- InvalidHookHandlers], @@ -205,31 +214,31 @@ run_fold_executes_handlers_in_the_right_order(_) -> %% check that there are no hook handlers added yet ?assertEqual([], get_handlers_for_all_hooks()), %% add various hook handlers - HookHandlers = [{calculate, ?HOOK_TAG1, MultiplyHandlerFn, #{n => 5}, 5}, - {calculate, ?HOOK_TAG1, MultiplyHandlerFn, #{}, 2}, - {calculate, ?HOOK_TAG1, PlusHandlerFn, #{n => 3}, 1}, - {calculate, ?HOOK_TAG1, PlusHandlerFn, #{}, 4}], + HookHandlers = [{calculate, ?HOOK_TAG, MultiplyHandlerFn, #{n => 5}, 5}, + {calculate, ?HOOK_TAG, MultiplyHandlerFn, #{}, 2}, + {calculate, ?HOOK_TAG, PlusHandlerFn, #{n => 3}, 1}, + {calculate, ?HOOK_TAG, PlusHandlerFn, #{}, 4}], ?assertEqual(ok, gen_hook:add_handlers(HookHandlers)), %% run the hook N = (((0 + 3) * 2) + 2) * 5, %% 40 - ?assertEqual({ok, N}, gen_hook:run_fold(calculate, ?HOOK_TAG1, 0, #{n => 2})), + ?assertEqual({ok, N}, gen_hook:run_fold(calculate, ?HOOK_TAG, 0, #{n => 2})), %% check hook handlers execution sequence Self = self(), ?assertEqual([{Self, {mod1, plus, [0, #{n => 2}, #{hook_name => calculate, n => 3, - hook_tag => ?HOOK_TAG1}]}, + hook_tag => ?HOOK_TAG}]}, {ok, 3}}, {Self, {mod1, multiply, [3, #{n => 2}, #{hook_name => calculate, - hook_tag => ?HOOK_TAG1}]}, + hook_tag => ?HOOK_TAG}]}, {ok, 6}}, {Self, {mod1, plus, [6, #{n => 2}, #{hook_name => calculate, - hook_tag => ?HOOK_TAG1}]}, + hook_tag => ?HOOK_TAG}]}, {ok, 8}}, {Self, {mod1, multiply, [8, #{n => 2}, #{hook_name => calculate, n => 5, - hook_tag => ?HOOK_TAG1}]}, + hook_tag => ?HOOK_TAG}]}, {ok, 40}}], meck:history(mod1)). @@ -241,28 +250,28 @@ run_fold_stops_when_handler_returns_stop(_) -> %% check that there are no hook handlers added yet ?assertEqual([], get_handlers_for_all_hooks()), %% add various hook handlers - HookHandlers = [{calculate, ?HOOK_TAG1, MultiplyHandlerFn, #{n => 5}, 5}, - {calculate, ?HOOK_TAG1, MultiplyHandlerFn, #{}, 2}, - {calculate, ?HOOK_TAG1, PlusHandlerFn, #{n => 3}, 1}, - {calculate, ?HOOK_TAG1, PlusHandlerFn, #{}, 4}, - {calculate, ?HOOK_TAG1, StopHandlerFn, #{}, 3}], + HookHandlers = [{calculate, ?HOOK_TAG, MultiplyHandlerFn, #{n => 5}, 5}, + {calculate, ?HOOK_TAG, MultiplyHandlerFn, #{}, 2}, + {calculate, ?HOOK_TAG, PlusHandlerFn, #{n => 3}, 1}, + {calculate, ?HOOK_TAG, PlusHandlerFn, #{}, 4}, + {calculate, ?HOOK_TAG, StopHandlerFn, #{}, 3}], ?assertEqual(ok, gen_hook:add_handlers(HookHandlers)), %% run the hook N = ((0 + 3) * 2), %% 6 - ?assertEqual({stop, N}, gen_hook:run_fold(calculate, ?HOOK_TAG1, 0, #{n => 2})), + ?assertEqual({stop, N}, gen_hook:run_fold(calculate, ?HOOK_TAG, 0, #{n => 2})), %% check hook handlers execution sequence Self = self(), ?assertEqual([{Self, {mod1, plus, [0, #{n => 2}, #{hook_name => calculate, n => 3, - hook_tag => ?HOOK_TAG1}]}, + hook_tag => ?HOOK_TAG}]}, {ok, 3}}, {Self, {mod1, multiply, [3, #{n => 2}, #{hook_name => calculate, - hook_tag => ?HOOK_TAG1}]}, + hook_tag => ?HOOK_TAG}]}, {ok, 6}}, {Self, {mod1, stop, [6, #{n => 2}, #{hook_name => calculate, - hook_tag => ?HOOK_TAG1}]}, + hook_tag => ?HOOK_TAG}]}, {stop, 6}}], meck:history(mod1)). @@ -275,42 +284,42 @@ errors_in_handlers_are_reported_but_ignored(_) -> %% check that there are no hook handlers added yet ?assertEqual([], get_handlers_for_all_hooks()), %% add various hook handlers - HookHandlers = [{calculate, ?HOOK_TAG1, MultiplyHandlerFn, #{n => 5}, 5}, - {calculate, ?HOOK_TAG1, MultiplyHandlerFn, #{}, 2}, - {calculate, ?HOOK_TAG1, PlusHandlerFn, #{n => 3}, 1}, - {calculate, ?HOOK_TAG1, PlusHandlerFn, #{}, 4}, - {calculate, ?HOOK_TAG1, ErrorHandlerFn, #{}, 3}], + HookHandlers = [{calculate, ?HOOK_TAG, MultiplyHandlerFn, #{n => 5}, 5}, + {calculate, ?HOOK_TAG, MultiplyHandlerFn, #{}, 2}, + {calculate, ?HOOK_TAG, PlusHandlerFn, #{n => 3}, 1}, + {calculate, ?HOOK_TAG, PlusHandlerFn, #{}, 4}, + {calculate, ?HOOK_TAG, ErrorHandlerFn, #{}, 3}], ?assertEqual(ok, gen_hook:add_handlers(HookHandlers)), %% run the hook N = (((0 + 3) * 2) + 2) * 5, %% 40 - ?assertEqual({ok, N}, gen_hook:run_fold(calculate, ?HOOK_TAG1, 0, #{n => 2})), + ?assertEqual({ok, N}, gen_hook:run_fold(calculate, ?HOOK_TAG, 0, #{n => 2})), %% check that error is reported ?assertEqual(true, meck:called(gen_hook, error_running_hook, [#{class => error, reason => some_error, stacktrace => '_'}, {hook_handler, 3, ErrorHandlerFn, - #{hook_name => calculate, hook_tag => ?HOOK_TAG1}}, - 6, #{n => 2}, {calculate, ?HOOK_TAG1}])), + #{hook_name => calculate, hook_tag => ?HOOK_TAG}}, + 6, #{n => 2}, {calculate, ?HOOK_TAG}])), %% check hook handlers execution sequence Self = self(), ?assertMatch([{Self, {mod1, plus, [0, #{n := 2}, #{hook_name := calculate, n := 3, - hook_tag := ?HOOK_TAG1}]}, + hook_tag := ?HOOK_TAG}]}, {ok, 3}}, {Self, {mod1, multiply, [3, #{n := 2}, #{hook_name := calculate, - hook_tag := ?HOOK_TAG1}]}, + hook_tag := ?HOOK_TAG}]}, {ok, 6}}, {Self, {mod1, error, [6, #{n := 2}, #{hook_name := calculate, - hook_tag := ?HOOK_TAG1}]}, + hook_tag := ?HOOK_TAG}]}, error, some_error, _}, {Self, {mod1, plus, [6, #{n := 2}, #{hook_name := calculate, - hook_tag := ?HOOK_TAG1}]}, + hook_tag := ?HOOK_TAG}]}, {ok, 8}}, {Self, {mod1, multiply, [8, #{n := 2}, #{hook_name := calculate, n := 5, - hook_tag := ?HOOK_TAG1}]}, + hook_tag := ?HOOK_TAG}]}, {ok, 40}}], meck:history(mod1)). diff --git a/test/keystore_SUITE.erl b/test/keystore_SUITE.erl index 055c02b868e..3dded998ce0 100644 --- a/test/keystore_SUITE.erl +++ b/test/keystore_SUITE.erl @@ -15,33 +15,32 @@ all() -> multiple_domains_one_stopped ]. -init_per_suite(C) -> +init_per_suite(Config) -> {ok, _} = application:ensure_all_started(jid), ok = mnesia:create_schema([node()]), ok = mnesia:start(), - C. + mongoose_config:set_opts(opts()), + 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(), mnesia:stop(), mnesia:delete_schema([node()]). init_per_testcase(_, Config) -> - mock_mongoose_metrics(), - Config1 = async_helper:start(Config, mongooseim_helper, start_link_loaded_hooks, []), - mongoose_config:set_opts(opts()), - Config1. + Config. -end_per_testcase(_, C) -> - mongoose_modules:stop(), - mongoose_config:erase_opts(), - meck:unload(mongoose_metrics), - async_helper:stop_all(C), +end_per_testcase(_, _Config) -> + [mongoose_modules:ensure_stopped(Host, mod_keystore) || Host <- hosts()], mnesia:delete_table(key). opts() -> maps:from_list([{hosts, hosts()}, {host_types, []}, - {all_metrics_are_global, false} | + {all_metrics_are_global, false}, + {instrumentation, config_parser_helper:default_config([instrumentation])} | [{{modules, Host}, #{}} || Host <- hosts()]]). hosts() -> @@ -135,12 +134,6 @@ sized_ram_key(Size) -> mod_config(mod_keystore, #{keys => #{ram_key => ram}, ram_key_size => Size}). -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. - %% Use a function like this in your module which is a client of mod_keystore. -spec get_key(HostType, KeyName) -> Result when HostType :: mongooseim:host_type(), diff --git a/test/mod_global_distrib_SUITE.erl b/test/mod_global_distrib_SUITE.erl index 8c42b327da1..e2612e0dcfd 100644 --- a/test/mod_global_distrib_SUITE.erl +++ b/test/mod_global_distrib_SUITE.erl @@ -52,10 +52,13 @@ suite() -> init_per_suite(Config) -> {ok, _} = application:ensure_all_started(jid), {ok, _} = application:ensure_all_started(cache_tab), - Config. + mongoose_config:set_opts(opts()), + async_helper:start(Config, [{mongoose_instrument, start_link, []}, + {mongooseim_helper, start_link_loaded_hooks, []}]). end_per_suite(Config) -> - Config. + mongoose_config:erase_opts(), + async_helper:stop_all(Config). init_per_group(_GroupName, Config) -> Config. @@ -65,23 +68,20 @@ end_per_group(_GroupName, Config) -> init_per_testcase(_CaseName, Config) -> set_meck(), - mongoose_config:set_opts(opts()), mongoose_domain_sup:start_link(), mim_ct_sup:start_link(ejabberd_sup), - mongooseim_helper:start_link_loaded_hooks(), mongoose_modules:start(), Config. end_per_testcase(_CaseName, Config) -> mongoose_modules:stop(), - mongoose_config:erase_opts(), unset_meck(), Config. opts() -> maps:from_list([{hosts, hosts()}, {host_types, []}, - {all_metrics_are_global, false} | + {instrumentation, config_parser_helper:default_config([instrumentation])} | [{{modules, HostType}, modules(HostType)} || HostType <- hosts()]]). hosts() -> diff --git a/test/mongooseim_metrics_SUITE.erl b/test/mongooseim_metrics_SUITE.erl index da6f61e028a..a415dbe54d2 100644 --- a/test/mongooseim_metrics_SUITE.erl +++ b/test/mongooseim_metrics_SUITE.erl @@ -35,6 +35,8 @@ init_per_suite(C) -> application:load(exometer_core), application:set_env(exometer_core, mongooseim_report_interval, 1000), {Port, Socket} = carbon_cache_server:start(), + mongoose_config:set_opts(opts()), + C1 = async_helper:start(C, mongoose_instrument, start_link, []), Sup = spawn(fun() -> mim_ct_sup:start_link(ejabberd_sup), Hooks = {gen_hook, @@ -55,11 +57,13 @@ init_per_suite(C) -> gen_tcp:controlling_process(Socket, PortServer), {ok, _Apps} = application:ensure_all_started(exometer_core), exometer:new([carbon, packets], spiral), - [{carbon_port, Port}, {test_sup, Sup}, {carbon_server, PortServer}, {carbon_socket, Socket} | C]. + [{carbon_port, Port}, {test_sup, Sup}, {carbon_server, PortServer}, {carbon_socket, Socket} | C1]. end_per_suite(C) -> Sup = ?config(test_sup, C), Sup ! stop, + async_helper:stop_all(C), + mongoose_config:erase_opts(), CarbonServer = ?config(carbon_server, C), erlang:exit(CarbonServer, kill), CarbonSocket = ?config(carbon_socket, C), @@ -68,7 +72,7 @@ end_per_suite(C) -> C. init_per_group(Group, C) -> - mongoose_config:set_opts(opts(Group)), + mongoose_config:set_opt(all_metrics_are_global, Group =:= all_metrics_are_global), mongoose_metrics:init(), mongoose_metrics:init_mongooseim_metrics(), C. @@ -76,7 +80,7 @@ init_per_group(Group, C) -> end_per_group(_Group, _C) -> mongoose_metrics:remove_host_type_metrics(<<"localhost">>), mongoose_metrics:remove_host_type_metrics(global), - mongoose_config:erase_opts(). + mongoose_config:unset_opt(all_metrics_are_global). init_per_testcase(CN, C) when tcp_connections_detected =:= CN; tcp_metric_varies_with_tcp_variations =:= CN -> @@ -171,10 +175,10 @@ wait_for_update({ok, [{count,0}]}, N) -> timer:sleep(1000), wait_for_update(exometer:get_value([carbon, packets], count), N-1). -opts(Group) -> +opts() -> #{hosts => [<<"localhost">>], host_types => [], - all_metrics_are_global => Group =:= all_metrics_are_global}. + instrumentation => config_parser_helper:default_config([instrumentation])}. get_reporters_cfg(Port) -> [{reporters, [ diff --git a/test/router_SUITE.erl b/test/router_SUITE.erl index d5fffdcc060..e0f48730c0d 100644 --- a/test/router_SUITE.erl +++ b/test/router_SUITE.erl @@ -22,35 +22,23 @@ groups() -> ]} ]. -init_per_suite(C) -> +init_per_suite(Config) -> {ok, _} = application:ensure_all_started(jid), ok = mnesia:create_schema([node()]), ok = mnesia:start(), {ok, _} = application:ensure_all_started(exometer_core), - C. + mongoose_config:set_opts(opts()), + async_helper:start(Config, [{mongoose_instrument, start_link, []}, + {mongooseim_helper, start_link_loaded_hooks, []}, + {ejabberd_router, start_link, []}]). -end_per_suite(_C) -> +end_per_suite(Config) -> meck:unload(), mnesia:stop(), mnesia:delete_schema([node()]), - application:stop(exometer_core), - ok. - -init_per_group(routing, Config) -> - mongoose_config:set_opts(opts()), - mongooseim_helper:start_link_loaded_hooks(), - ejabberd_router:start_link(), - Config. - -end_per_group(routing, _Config) -> + async_helper:stop_all(Config), mongoose_config:erase_opts(). -init_per_testcase(_CaseName, Config) -> - Config. - -end_per_testcase(_CaseName, _Config) -> - ok. - %% --------------------------------------------------------------- %% Test cases %% --------------------------------------------------------------- @@ -173,5 +161,6 @@ resend_as_error(From0, To0, Acc0, Packet0) -> opts() -> RoutingModules = [xmpp_router_a, xmpp_router_b, xmpp_router_c], #{all_metrics_are_global => false, + instrumentation => config_parser_helper:default_config([instrumentation]), component_backend => mnesia, routing_modules => xmpp_router:expand_routing_modules(RoutingModules)}.