test(cm): cover {takeover, 'begin'/'end'} action
This commit is contained in:
parent
6dd0b49dd2
commit
66807f17df
|
@ -187,57 +187,89 @@ t_open_session_race_condition(_) ->
|
||||||
ok = flush_emqx_pool(),
|
ok = flush_emqx_pool(),
|
||||||
?assertEqual([], emqx_cm:lookup_channels(ClientId)).
|
?assertEqual([], emqx_cm:lookup_channels(ClientId)).
|
||||||
|
|
||||||
t_kick_session_discard_normal(_) ->
|
t_call_session_discard_normal(_) ->
|
||||||
test_kick_session(discard, normal).
|
test_call_session(discard, normal).
|
||||||
|
|
||||||
t_kick_session_discard_shutdown(_) ->
|
t_call_session_discard_shutdown(_) ->
|
||||||
test_kick_session(discard, shutdown).
|
test_call_session(discard, shutdown).
|
||||||
|
|
||||||
t_kick_session_discard_shutdown_with_reason(_) ->
|
t_call_session_discard_shutdown_with_reason(_) ->
|
||||||
test_kick_session(discard, {shutdown, discard}).
|
test_call_session(discard, {shutdown, discard}).
|
||||||
|
|
||||||
t_kick_session_discard_timeout(_) ->
|
t_call_session_discard_timeout(_) ->
|
||||||
test_kick_session(discard, timeout).
|
test_call_session(discard, timeout).
|
||||||
|
|
||||||
t_kick_session_discard_noproc(_) ->
|
t_call_session_discard_noproc(_) ->
|
||||||
test_kick_session(discard, noproc).
|
test_call_session(discard, noproc).
|
||||||
|
|
||||||
t_kick_session_kick_normal(_) ->
|
t_call_session_kick_normal(_) ->
|
||||||
test_kick_session(discard, normal).
|
test_call_session(kick, normal).
|
||||||
|
|
||||||
t_kick_session_kick_shutdown(_) ->
|
t_call_session_kick_shutdown(_) ->
|
||||||
test_kick_session(discard, shutdown).
|
test_call_session(kick, shutdown).
|
||||||
|
|
||||||
t_kick_session_kick_shutdown_with_reason(_) ->
|
t_call_session_kick_shutdown_with_reason(_) ->
|
||||||
test_kick_session(discard, {shutdown, discard}).
|
test_call_session(kick, {shutdown, discard}).
|
||||||
|
|
||||||
t_kick_session_kick_timeout(_) ->
|
t_call_session_kick_timeout(_) ->
|
||||||
test_kick_session(discard, timeout).
|
test_call_session(kick, timeout).
|
||||||
|
|
||||||
t_kick_session_kick_noproc(_) ->
|
t_call_session_kick_noproc(_) ->
|
||||||
test_kick_session(discard, noproc).
|
test_call_session(discard, noproc).
|
||||||
|
|
||||||
test_kick_session(Action, Reason) ->
|
t_call_session_takeover_begin_normal(_) ->
|
||||||
|
test_call_session({takeover, 'begin'}, normal).
|
||||||
|
|
||||||
|
t_call_session_takeover_begin_shutdown(_) ->
|
||||||
|
test_call_session({takeover, 'begin'}, shutdown).
|
||||||
|
|
||||||
|
t_call_session_takeover_begin_shutdown_with_reason(_) ->
|
||||||
|
test_call_session({takeover, 'begin'}, {shutdown, discard}).
|
||||||
|
|
||||||
|
t_call_session_takeover_begin_timeout(_) ->
|
||||||
|
test_call_session({takeover, 'begin'}, timeout).
|
||||||
|
|
||||||
|
t_call_session_takeover_begin_noproc(_) ->
|
||||||
|
test_call_session({takeover, 'begin'}, noproc).
|
||||||
|
|
||||||
|
t_call_session_takeover_end_normal(_) ->
|
||||||
|
test_call_session({takeover, 'end'}, normal).
|
||||||
|
|
||||||
|
t_call_session_takeover_end_shutdown(_) ->
|
||||||
|
test_call_session({takeover, 'end'}, shutdown).
|
||||||
|
|
||||||
|
t_call_session_takeover_end_shutdown_with_reason(_) ->
|
||||||
|
test_call_session({takeover, 'end'}, {shutdown, discard}).
|
||||||
|
|
||||||
|
t_call_session_takeover_end_timeout(_) ->
|
||||||
|
test_call_session({takeover, 'end'}, timeout).
|
||||||
|
|
||||||
|
t_call_session_takeover_end_noproc(_) ->
|
||||||
|
test_call_session({takeover, 'end'}, noproc).
|
||||||
|
|
||||||
|
test_call_session(Action, Reason) ->
|
||||||
ClientId = rand_client_id(),
|
ClientId = rand_client_id(),
|
||||||
#{conninfo := ConnInfo} = ?ChanInfo,
|
#{conninfo := ConnInfo} = ?ChanInfo,
|
||||||
FakeSessionFun =
|
FakeSessionFun =
|
||||||
fun Loop() ->
|
fun Loop() ->
|
||||||
receive
|
receive
|
||||||
{'$gen_call', From, A} when A =:= kick orelse
|
{'$gen_call', From, A} when A =:= kick orelse
|
||||||
A =:= discard ->
|
A =:= discard orelse
|
||||||
case Reason of
|
A =:= {takeover, 'begin'} orelse
|
||||||
normal ->
|
A =:= {takeover, 'end'} ->
|
||||||
gen_server:reply(From, ok);
|
case Reason of
|
||||||
timeout ->
|
normal when A =:= kick orelse A =:= discard ->
|
||||||
%% no response to the call
|
gen_server:reply(From, ok);
|
||||||
Loop();
|
timeout ->
|
||||||
_ ->
|
%% no response to the call
|
||||||
exit(Reason)
|
Loop();
|
||||||
end;
|
_ ->
|
||||||
Msg ->
|
exit(Reason)
|
||||||
ct:pal("(~p) fake_session_discarded ~p", [Action, Msg]),
|
end;
|
||||||
Loop()
|
Msg ->
|
||||||
end
|
ct:pal("(~p) fake_session_discarded ~p", [Action, Msg]),
|
||||||
|
Loop()
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
{Pid1, _} = spawn_monitor(FakeSessionFun),
|
{Pid1, _} = spawn_monitor(FakeSessionFun),
|
||||||
{Pid2, _} = spawn_monitor(FakeSessionFun),
|
{Pid2, _} = spawn_monitor(FakeSessionFun),
|
||||||
|
@ -249,10 +281,11 @@ test_kick_session(Action, Reason) ->
|
||||||
noproc -> exit(Pid1, kill), exit(Pid2, kill);
|
noproc -> exit(Pid1, kill), exit(Pid2, kill);
|
||||||
_ -> ok
|
_ -> ok
|
||||||
end,
|
end,
|
||||||
ok = case Action of
|
_ = case Action of
|
||||||
kick -> emqx_cm:kick_session(ClientId);
|
kick -> emqx_cm:kick_session(ClientId);
|
||||||
discard -> emqx_cm:discard_session(ClientId)
|
discard -> emqx_cm:discard_session(ClientId);
|
||||||
end,
|
{takeover, _} -> emqx_cm:takeover_session(ClientId)
|
||||||
|
end,
|
||||||
case Reason =:= timeout orelse Reason =:= noproc of
|
case Reason =:= timeout orelse Reason =:= noproc of
|
||||||
true ->
|
true ->
|
||||||
?assertEqual(killed, ?WAIT({'DOWN', _, process, Pid1, R}, 2_000, R)),
|
?assertEqual(killed, ?WAIT({'DOWN', _, process, Pid1, R}, 2_000, R)),
|
||||||
|
|
Loading…
Reference in New Issue