From b15e81baf2f7cec3afe66b8a61d128c126f029a0 Mon Sep 17 00:00:00 2001 From: Andrew Mayorov Date: Tue, 13 Feb 2024 19:07:51 +0100 Subject: [PATCH] test(slowsub): update and simplify testsuite setup --- .../test/emqx_slow_subs_SUITE.erl | 55 +++++------------ .../test/emqx_slow_subs_api_SUITE.erl | 60 ++++++------------- 2 files changed, 34 insertions(+), 81 deletions(-) diff --git a/apps/emqx_slow_subs/test/emqx_slow_subs_SUITE.erl b/apps/emqx_slow_subs/test/emqx_slow_subs_SUITE.erl index d93a0b4a2..49f8fcfcf 100644 --- a/apps/emqx_slow_subs/test/emqx_slow_subs_SUITE.erl +++ b/apps/emqx_slow_subs/test/emqx_slow_subs_SUITE.erl @@ -20,76 +20,51 @@ -compile(nowarn_export_all). -include_lib("eunit/include/eunit.hrl"). +-include_lib("common_test/include/ct.hrl"). -include_lib("emqx/include/emqx_mqtt.hrl"). -include_lib("emqx/include/emqx.hrl"). -include_lib("emqx_slow_subs/include/emqx_slow_subs.hrl"). -define(NOW, erlang:system_time(millisecond)). --define(CLUSTER_RPC_SHARD, emqx_cluster_rpc_shard). -define(LANTENCY, 101). -define(BASE_CONF, << - "" - "\n" "slow_subs {\n" " enable = true\n" " top_k_num = 5\n" " threshold = 100ms\n" " expire_interval = 5m\n" " stats_type = whole\n" - " }" - "" + "}" >>). all() -> emqx_common_test_helpers:all(?MODULE). init_per_suite(Config) -> - application:load(emqx_conf), - ok = ekka:start(), - ok = mria_rlog:wait_for_shards([?CLUSTER_RPC_SHARD], infinity), - meck:new(emqx_alarm, [non_strict, passthrough, no_link]), - meck:expect(emqx_alarm, activate, 3, ok), - meck:expect(emqx_alarm, deactivate, 3, ok), + Apps = emqx_cth_suite:start( + [ + emqx, + emqx_conf, + {emqx_slow_subs, ?BASE_CONF} + ], + #{work_dir => emqx_cth_suite:work_dir(Config)} + ), + [{suite_apps, Apps} | Config]. - ok = emqx_common_test_helpers:load_config(emqx_slow_subs_schema, ?BASE_CONF), - emqx_common_test_helpers:start_apps([emqx_slow_subs]), - Config. - -end_per_suite(_Config) -> - ekka:stop(), - mria:stop(), - mria_mnesia:delete_schema(), - meck:unload(emqx_alarm), - - emqx_common_test_helpers:stop_apps([emqx_slow_subs]). - -init_per_testcase(t_expire, Config) -> - {ok, _} = emqx_cluster_rpc:start_link(), - update_config(<<"expire_interval">>, <<"1500ms">>), - Config; -init_per_testcase(_, Config) -> - {ok, _} = emqx_cluster_rpc:start_link(), - Config. - -end_per_testcase(_, _) -> - case erlang:whereis(node()) of - undefined -> - ok; - P -> - erlang:unlink(P), - erlang:exit(P, kill) - end, - ok. +end_per_suite(Config) -> + ok = emqx_cth_suite:stop(?config(suite_apps, Config)). %%-------------------------------------------------------------------- %% Test Cases %%-------------------------------------------------------------------- + t_pub(_) -> _ = [stats_with_type(Type) || Type <- [whole, internal, response]], ok. t_expire(_) -> + _ = update_config(<<"expire_interval">>, <<"1500ms">>), Now = ?NOW, Each = fun(I) -> ClientId = erlang:list_to_binary(io_lib:format("test_~p", [I])), diff --git a/apps/emqx_slow_subs/test/emqx_slow_subs_api_SUITE.erl b/apps/emqx_slow_subs/test/emqx_slow_subs_api_SUITE.erl index 66ab745d1..e193b305a 100644 --- a/apps/emqx_slow_subs/test/emqx_slow_subs_api_SUITE.erl +++ b/apps/emqx_slow_subs/test/emqx_slow_subs_api_SUITE.erl @@ -20,10 +20,8 @@ -compile(nowarn_export_all). -include_lib("eunit/include/eunit.hrl"). - +-include_lib("common_test/include/ct.hrl"). -include_lib("emqx/include/emqx.hrl"). --include_lib("emqx/include/emqx_mqtt.hrl"). --include_lib("emqx_management/include/emqx_mgmt.hrl"). -include_lib("emqx_slow_subs/include/emqx_slow_subs.hrl"). -define(HOST, "http://127.0.0.1:18083/"). @@ -32,63 +30,43 @@ -define(BASE_PATH, "api"). -define(NOW, erlang:system_time(millisecond)). --define(CLUSTER_RPC_SHARD, emqx_cluster_rpc_shard). -define(CONF_DEFAULT, << - "" - "\n" - "slow_subs\n" - "{\n" + "slow_subs {\n" " enable = true\n" " top_k_num = 5,\n" " expire_interval = 60s\n" " stats_type = whole\n" "}" - "" >>). all() -> emqx_common_test_helpers:all(?MODULE). init_per_suite(Config) -> - application:load(emqx_conf), - ok = ekka:start(), - ok = mria_rlog:wait_for_shards([?CLUSTER_RPC_SHARD], infinity), - meck:new(emqx_alarm, [non_strict, passthrough, no_link]), - meck:expect(emqx_alarm, activate, 3, ok), - meck:expect(emqx_alarm, deactivate, 3, ok), - - ok = emqx_common_test_helpers:load_config(emqx_slow_subs_schema, ?CONF_DEFAULT), - emqx_mgmt_api_test_util:init_suite([emqx_slow_subs]), - {ok, _} = application:ensure_all_started(emqx_auth), - Config. + Apps = emqx_cth_suite:start( + [ + emqx, + emqx_auth, + emqx_conf, + emqx_management, + {emqx_slow_subs, ?CONF_DEFAULT}, + {emqx_dashboard, "dashboard.listeners.http { enable = true, bind = 18083 }"} + ], + #{work_dir => emqx_cth_suite:work_dir(Config)} + ), + _ = emqx_common_test_http:create_default_app(), + [{suite_apps, Apps} | Config]. end_per_suite(Config) -> - ekka:stop(), - mria:stop(), - mria_mnesia:delete_schema(), - meck:unload(emqx_alarm), - - application:stop(emqx_auth), - emqx_mgmt_api_test_util:end_suite([emqx_slow_subs]), - Config. + ok = emqx_cth_suite:stop(?config(suite_apps, Config)). init_per_testcase(_, Config) -> - {ok, _} = emqx_cluster_rpc:start_link(), - application:ensure_all_started(emqx_slow_subs), - timer:sleep(500), + {ok, _} = application:ensure_all_started(emqx_slow_subs), Config. -end_per_testcase(_, Config) -> - application:stop(emqx_slow_subs), - case erlang:whereis(node()) of - undefined -> - ok; - P -> - erlang:unlink(P), - erlang:exit(P, kill) - end, - Config. +end_per_testcase(_, _Config) -> + ok = application:stop(emqx_slow_subs). t_get_history(_) -> Now = ?NOW,