Add more test cases for 'emqx_misc' module

This commit is contained in:
Feng Lee 2019-08-28 11:36:59 +08:00
parent 08ab350fec
commit 62f0f0ccbc
2 changed files with 60 additions and 28 deletions

View File

@ -41,7 +41,7 @@
]}).
%% @doc Merge options
-spec(merge_opts(list(), list()) -> list()).
-spec(merge_opts(Opts, Opts) -> Opts when Opts :: proplists:proplist()).
merge_opts(Defaults, Options) ->
lists:foldl(
fun({Opt, Val}, Acc) ->

View File

@ -22,60 +22,92 @@
-include_lib("eunit/include/eunit.hrl").
-define(SOCKOPTS, [binary,
{packet, raw},
{packet, raw},
{reuseaddr, true},
{backlog, 512},
{nodelay, true}]).
{backlog, 512},
{nodelay, true}
]).
all() -> emqx_ct:all(?MODULE).
t_merge_opts() ->
t_merge_opts(_) ->
Opts = emqx_misc:merge_opts(?SOCKOPTS, [raw,
binary,
{backlog, 1024},
{nodelay, false},
{max_clients, 1024},
{acceptors, 16}]),
{acceptors, 16}
]),
?assertEqual(1024, proplists:get_value(backlog, Opts)),
?assertEqual(1024, proplists:get_value(max_clients, Opts)),
[binary, raw,
{acceptors, 16},
{backlog, 1024},
{max_clients, 1024},
{nodelay, false},
{packet, raw},
{reuseaddr, true}] = lists:sort(Opts).
?assertEqual([binary, raw,
{acceptors, 16},
{backlog, 1024},
{max_clients, 1024},
{nodelay, false},
{packet, raw},
{reuseaddr, true}], lists:sort(Opts)).
t_timer_cancel_flush() ->
t_maybe_apply(_) ->
?assertEqual(undefined, emqx_misc:maybe_apply(fun(A) -> A end, undefined)),
?assertEqual(a, emqx_misc:maybe_apply(fun(A) -> A end, a)).
t_run_fold(_) ->
?assertEqual(1, emqx_misc:run_fold([], 1, state)),
Add = fun(I, St) -> I+St end,
Mul = fun(I, St) -> I*St end,
?assertEqual(6, emqx_misc:run_fold([Add, Mul], 1, 2)).
t_pipeline(_) ->
?assertEqual({ok, input, state}, emqx_misc:pipeline([], input, state)),
Funs = [fun(_I, _St) -> ok end,
fun(_I, St) -> {ok, St+1} end,
fun(I, St) -> {ok, I+1, St+1} end,
fun(I, St) -> {ok, I*2, St*2} end],
?assertEqual({ok, 4, 6}, emqx_misc:pipeline(Funs, 1, 1)).
t_start_timer(_) ->
TRef = emqx_misc:start_timer(1, tmsg),
timer:sleep(2),
?assertEqual([{timeout, TRef, tmsg}], drain()),
ok = emqx_misc:cancel_timer(TRef).
t_cancel_timer(_) ->
Timer = emqx_misc:start_timer(0, foo),
ok = emqx_misc:cancel_timer(Timer),
receive
{timeout, Timer, foo} ->
error(unexpected)
after 0 -> ok
end.
?assertEqual([], drain()).
t_proc_name(_) ->
'TODO'.
?assertEqual(emqx_pool_1, emqx_misc:proc_name(emqx_pool, 1)).
t_proc_stats(_) ->
'TODO'.
Pid1 = spawn(fun() -> exit(normal) end),
timer:sleep(10),
?assertEqual([], emqx_misc:proc_stats(Pid1)),
Pid2 = spawn(fun() -> timer:sleep(100) end),
Pid2 ! msg,
timer:sleep(10),
?assertMatch([{mailbox_len, 1}|_], emqx_misc:proc_stats(Pid2)).
t_drain_deliver(_) ->
'TODO'.
self() ! {deliver, t1, m1},
self() ! {deliver, t2, m2},
?assertEqual([{deliver, t1, m1},
{deliver, t2, m2}
], emqx_misc:drain_deliver()).
t_drain_down(_) ->
'TODO'.
{Pid1, _Ref1} = erlang:spawn_monitor(fun() -> ok end),
{Pid2, _Ref2} = erlang:spawn_monitor(fun() -> ok end),
timer:sleep(100),
?assertEqual([Pid1, Pid2], emqx_misc:drain_down(2)).
%% drain self() msg queue for deterministic test behavior
drain() ->
_ = drain([]), % maybe log
ok.
drain([]).
drain(Acc) ->
receive
Msg ->
drain([Msg | Acc])
Msg -> drain([Msg|Acc])
after
0 ->
lists:reverse(Acc)