refactor: kickout_client doesn't need a format fun

This commit is contained in:
Stefan Strigler 2023-02-16 17:04:15 +01:00
parent a6d88c3caa
commit f3ced5d5eb
4 changed files with 25 additions and 11 deletions

View File

@ -267,7 +267,7 @@ lookup_client({username, Username}, FormatFun) ->
|| Node <- mria_mnesia:running_nodes() || Node <- mria_mnesia:running_nodes()
]). ]).
lookup_client(Node, Key, {M, F}) -> lookup_client(Node, Key, FormatFun) ->
case unwrap_rpc(emqx_cm_proto_v1:lookup_client(Node, Key)) of case unwrap_rpc(emqx_cm_proto_v1:lookup_client(Node, Key)) of
{error, Err} -> {error, Err} ->
{error, Err}; {error, Err};
@ -275,14 +275,19 @@ lookup_client(Node, Key, {M, F}) ->
lists:map( lists:map(
fun({Chan, Info0, Stats}) -> fun({Chan, Info0, Stats}) ->
Info = Info0#{node => Node}, Info = Info0#{node => Node},
M:F({Chan, Info, Stats}) maybe_format(FormatFun, {Chan, Info, Stats})
end, end,
L L
) )
end. end.
kickout_client({ClientID, FormatFun}) -> maybe_format(undefined, A) ->
case lookup_client({clientid, ClientID}, FormatFun) of A;
maybe_format({M, F}, A) ->
M:F(A).
kickout_client(ClientID) ->
case lookup_client({clientid, ClientID}, undefined) of
[] -> [] ->
{error, not_found}; {error, not_found};
_ -> _ ->

View File

@ -677,7 +677,7 @@ lookup(#{clientid := ClientID}) ->
end. end.
kickout(#{clientid := ClientID}) -> kickout(#{clientid := ClientID}) ->
case emqx_mgmt:kickout_client({ClientID, ?FORMAT_FUN}) of case emqx_mgmt:kickout_client(ClientID) of
{error, not_found} -> {error, not_found} ->
{404, ?CLIENTID_NOT_FOUND}; {404, ?CLIENTID_NOT_FOUND};
_ -> _ ->

View File

@ -23,6 +23,8 @@
-export([ident/1]). -export([ident/1]).
-define(FORMATFUN, {?MODULE, ident}).
all() -> all() ->
emqx_common_test_helpers:all(?MODULE). emqx_common_test_helpers:all(?MODULE).
@ -117,12 +119,12 @@ t_lookup_client('end', Config) ->
disconnect_clients(Config). disconnect_clients(Config).
t_lookup_client(_Config) -> t_lookup_client(_Config) ->
[{Chan, Info, Stats}] = emqx_mgmt:lookup_client({clientid, <<"client1">>}, {?MODULE, ident}), [{Chan, Info, Stats}] = emqx_mgmt:lookup_client({clientid, <<"client1">>}, ?FORMATFUN),
?assertEqual( ?assertEqual(
[{Chan, Info, Stats}], [{Chan, Info, Stats}],
emqx_mgmt:lookup_client({username, <<"user1">>}, {?MODULE, ident}) emqx_mgmt:lookup_client({username, <<"user1">>}, ?FORMATFUN)
), ),
?assertEqual([], emqx_mgmt:lookup_client({clientid, <<"notfound">>}, {?MODULE, ident})). ?assertEqual([], emqx_mgmt:lookup_client({clientid, <<"notfound">>}, ?FORMATFUN)).
t_kickout_client(init, Config) -> t_kickout_client(init, Config) ->
process_flag(trap_exit, true), process_flag(trap_exit, true),
@ -132,7 +134,7 @@ t_kickout_client('end', _Config) ->
t_kickout_client(Config) -> t_kickout_client(Config) ->
[C | _] = ?config(clients, Config), [C | _] = ?config(clients, Config),
ok = emqx_mgmt:kickout_client({<<"client1">>, {?MODULE, ident}}), ok = emqx_mgmt:kickout_client(<<"client1">>),
receive receive
{'EXIT', C, Reason} -> {'EXIT', C, Reason} ->
?assertEqual({shutdown, tcp_closed}, Reason); ?assertEqual({shutdown, tcp_closed}, Reason);
@ -141,7 +143,7 @@ t_kickout_client(Config) ->
after 1000 -> after 1000 ->
error(timeout) error(timeout)
end, end,
?assertEqual({error, not_found}, emqx_mgmt:kickout_client({<<"notfound">>, {?MODULE, ident}})). ?assertEqual({error, not_found}, emqx_mgmt:kickout_client(<<"notfound">>)).
%%% helpers %%% helpers
ident(Arg) -> ident(Arg) ->

View File

@ -78,7 +78,14 @@ t_clients(_) ->
%% delete /clients/:clientid kickout %% delete /clients/:clientid kickout
Client2Path = emqx_mgmt_api_test_util:api_path(["clients", binary_to_list(ClientId2)]), Client2Path = emqx_mgmt_api_test_util:api_path(["clients", binary_to_list(ClientId2)]),
{ok, _} = emqx_mgmt_api_test_util:request_api(delete, Client2Path), {ok, _} = emqx_mgmt_api_test_util:request_api(delete, Client2Path),
timer:sleep(300), Kick =
receive
{'EXIT', C2, _} ->
ok
after 300 ->
timeout
end,
?assertEqual(ok, Kick),
AfterKickoutResponse2 = emqx_mgmt_api_test_util:request_api(get, Client2Path), AfterKickoutResponse2 = emqx_mgmt_api_test_util:request_api(get, Client2Path),
?assertEqual({error, {"HTTP/1.1", 404, "Not Found"}}, AfterKickoutResponse2), ?assertEqual({error, {"HTTP/1.1", 404, "Not Found"}}, AfterKickoutResponse2),