From 14af90d0c3ea4c319d920e16f2012af9e988658e Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Fri, 9 Jul 2021 15:38:51 +0800 Subject: [PATCH] fix(test): update test cases for emqx_acl_cache_SUITE --- apps/emqx/src/emqx_acl_cache.erl | 7 +-- apps/emqx/src/emqx_channel.erl | 5 +- apps/emqx/test/emqx_acl_cache_SUITE.erl | 71 ++----------------------- 3 files changed, 10 insertions(+), 73 deletions(-) diff --git a/apps/emqx/src/emqx_acl_cache.erl b/apps/emqx/src/emqx_acl_cache.erl index a49f42c83..d4c7cfbdb 100644 --- a/apps/emqx/src/emqx_acl_cache.erl +++ b/apps/emqx/src/emqx_acl_cache.erl @@ -18,7 +18,7 @@ -include("emqx.hrl"). --export([ list_acl_cache/0 +-export([ list_acl_cache/2 , get_acl_cache/4 , put_acl_cache/5 , cleanup_acl_cache/2 @@ -62,8 +62,9 @@ get_cache_max_size(Zone, Listener) -> get_cache_ttl(Zone, Listener) -> emqx_config:get_listener_conf(Zone, Listener, [acl, cache, ttl]). --spec(list_acl_cache() -> [acl_cache_entry()]). -list_acl_cache() -> +-spec(list_acl_cache(atom(), atom()) -> [acl_cache_entry()]). +list_acl_cache(Zone, Listener) -> + cleanup_acl_cache(Zone, Listener), map_acl_cache(fun(Cache) -> Cache end). %% We'll cleanup the cache before replacing an expired acl. diff --git a/apps/emqx/src/emqx_channel.erl b/apps/emqx/src/emqx_channel.erl index 600e8bed0..9b43b3bf3 100644 --- a/apps/emqx/src/emqx_channel.erl +++ b/apps/emqx/src/emqx_channel.erl @@ -944,8 +944,9 @@ handle_call({takeover, 'end'}, Channel = #channel{session = Session, AllPendings = lists:append(Delivers, Pendings), disconnect_and_shutdown(takeovered, AllPendings, Channel); -handle_call(list_acl_cache, Channel) -> - {reply, emqx_acl_cache:list_acl_cache(), Channel}; +handle_call(list_acl_cache, #channel{clientinfo = #{zone := Zone, listener := Listener}} + = Channel) -> + {reply, emqx_acl_cache:list_acl_cache(Zone, Listener), Channel}; handle_call({quota, Policy}, Channel) -> Zone = info(zone, Channel), diff --git a/apps/emqx/test/emqx_acl_cache_SUITE.erl b/apps/emqx/test/emqx_acl_cache_SUITE.erl index be7c29055..f631f18cb 100644 --- a/apps/emqx/test/emqx_acl_cache_SUITE.erl +++ b/apps/emqx/test/emqx_acl_cache_SUITE.erl @@ -26,6 +26,7 @@ all() -> emqx_ct:all(?MODULE). init_per_suite(Config) -> emqx_ct_helpers:boot_modules(all), emqx_ct_helpers:start_apps([]), + toggle_acl(true), Config. end_per_suite(_Config) -> @@ -55,7 +56,6 @@ t_clean_acl_cache(_) -> ?assertEqual(0, length(gen_server:call(ClientPid, list_acl_cache))), emqtt:stop(Client). - t_drain_acl_cache(_) -> {ok, Client} = emqtt:start_link([{clientid, <<"emqx_c">>}]), {ok, _} = emqtt:connect(Client), @@ -79,70 +79,5 @@ t_drain_acl_cache(_) -> ?assert(length(gen_server:call(ClientPid, list_acl_cache)) > 0), emqtt:stop(Client). -% optimize?? -t_reload_aclfile_and_cleanall(_Config) -> - - RasieMsg = fun() -> Self = self(), #{puback => fun(Msg) -> Self ! {puback, Msg} end, - disconnected => fun(_) -> ok end, - publish => fun(_) -> ok end } end, - - {ok, Client} = emqtt:start_link([{clientid, <<"emqx_c">>}, {proto_ver, v5}, - {msg_handler, RasieMsg()}]), - {ok, _} = emqtt:connect(Client), - - {ok, PktId} = emqtt:publish(Client, <<"t1">>, <<"{\"x\":1}">>, qos1), - - %% Success publish to broker - receive - {puback, #{packet_id := PktId, reason_code := Rc}} -> - ?assertEqual(16#10, Rc); - _ -> - ?assert(false) - end, - - %% Check acl cache list - [ClientPid] = emqx_cm:lookup_channels(<<"emqx_c">>), - ?assert(length(gen_server:call(ClientPid, list_acl_cache)) > 0), - emqtt:stop(Client). - -%% @private -testdir(DataPath) -> - Ls = filename:split(DataPath), - filename:join(lists:sublist(Ls, 1, length(Ls) - 1)). - -% t_cache_k(_) -> -% error('TODO'). - -% t_cache_v(_) -> -% error('TODO'). - -% t_cleanup_acl_cache(_) -> -% error('TODO'). - -% t_get_oldest_key(_) -> -% error('TODO'). - -% t_get_newest_key(_) -> -% error('TODO'). - -% t_get_cache_max_size(_) -> -% error('TODO'). - -% t_get_cache_size(_) -> -% error('TODO'). - -% t_dump_acl_cache(_) -> -% error('TODO'). - -% t_empty_acl_cache(_) -> -% error('TODO'). - -% t_put_acl_cache(_) -> -% error('TODO'). - -% t_get_acl_cache(_) -> -% error('TODO'). - -% t_is_enabled(_) -> -% error('TODO'). - +toggle_acl(Bool) when is_boolean(Bool) -> + emqx_config:put_listener_conf(default, mqtt_tcp, [acl, enable], Bool).