Fix testcases for emqx_connection

This commit is contained in:
terry-xiaoyu 2019-11-01 19:57:14 +08:00
parent 7df8d0246c
commit 865192c631
1 changed files with 42 additions and 16 deletions

View File

@ -27,7 +27,14 @@
send_pend send_pend
]). ]).
all() -> emqx_ct:all(?MODULE). all() -> emqx_ct:all(?MODULE) ++ [{group, real_client}].
groups() ->
[{real_client, [non_parallel_tests],
[
g_get_conn_stats,
g_handle_sock_passive
]}].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% CT callbacks %% CT callbacks
@ -39,6 +46,16 @@ init_per_suite(Config) ->
end_per_suite(_Config) -> end_per_suite(_Config) ->
ok. ok.
init_per_group(real_client, Config) ->
emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([]),
Config;
init_per_group(_, Config) -> Config.
end_per_group(real_client, _Config) ->
emqx_ct_helpers:stop_apps([]);
end_per_group(_, Config) -> Config.
init_per_testcase(_TestCase, Config) -> init_per_testcase(_TestCase, Config) ->
%% Meck Transport %% Meck Transport
ok = meck:new(emqx_transport, [non_strict, passthrough, no_history]), ok = meck:new(emqx_transport, [non_strict, passthrough, no_history]),
@ -95,22 +112,19 @@ t_start_link_exit_on_activate(_) ->
t_get_conn_info(_) -> t_get_conn_info(_) ->
with_connection(fun(CPid) -> with_connection(fun(CPid) ->
#{sockinfo := SockInfo} = emqx_connection:info(CPid), #{sockinfo := SockInfo} = emqx_connection:info(CPid),
?assertEqual(#{active_n => 100, ?assertEqual(#{active_n => 100,limiter => undefined,
peername => {{127,0,0,1},3456}, peername => {{127,0,0,1},3456},
pub_limit => undefined,
rate_limit => undefined,
sockname => {{127,0,0,1},1883}, sockname => {{127,0,0,1},1883},
sockstate => running, sockstate => running,
socktype => tcp}, SockInfo) socktype => tcp}, SockInfo)
end). end).
t_get_conn_stats(_) -> g_get_conn_stats(_) ->
with_connection(fun(CPid) -> with_client(fun(CPid) ->
Stats = emqx_connection:stats(CPid), Stats = emqx_connection:stats(CPid),
lists:foreach(fun(Key) -> ct:pal("==== stats: ~p", [Stats]),
0 = proplists:get_value(Key, Stats) [?assert(proplists:get_value(Key, Stats) >= 0) || Key <- ?STATS_KYES]
end, ?STATS_KYES) end, []).
end).
t_handle_call_discard(_) -> t_handle_call_discard(_) ->
with_connection(fun(CPid) -> with_connection(fun(CPid) ->
@ -190,8 +204,8 @@ t_handle_sock_error(_) ->
trap_exit(CPid, {shutdown, econnreset}) trap_exit(CPid, {shutdown, econnreset})
end, #{trap_exit => true}). end, #{trap_exit => true}).
t_handle_sock_passive(_) -> g_handle_sock_passive(_) ->
with_connection(fun(CPid) -> CPid ! {tcp_passive, sock} end). with_client(fun(CPid) -> CPid ! {tcp_passive, sock} end, []).
t_handle_sock_activate(_) -> t_handle_sock_activate(_) ->
with_connection(fun(CPid) -> CPid ! activate_socket end). with_connection(fun(CPid) -> CPid ! activate_socket end).
@ -313,6 +327,18 @@ with_connection(TestFun, Options) ->
TrapExit orelse emqx_connection:stop(CPid), TrapExit orelse emqx_connection:stop(CPid),
ok. ok.
with_client(TestFun, _Options) ->
ClientId = <<"t_conn">>,
{ok, C} = emqtt:start_link([{clientid, ClientId}]),
{ok, _} = emqtt:connect(C),
timer:sleep(50),
case emqx_cm:lookup_channels(ClientId) of
[] -> ct:fail({client_not_started, ClientId});
[ChanPid] ->
TestFun(ChanPid),
emqtt:stop(C)
end.
trap_exit(Pid, Reason) -> trap_exit(Pid, Reason) ->
receive receive
{'EXIT', Pid, Reason} -> ok; {'EXIT', Pid, Reason} -> ok;