From 62f0f0ccbcfed2100cc9a5cbc73af43a3035a5e2 Mon Sep 17 00:00:00 2001 From: Feng Lee Date: Wed, 28 Aug 2019 11:36:59 +0800 Subject: [PATCH] Add more test cases for 'emqx_misc' module --- src/emqx_misc.erl | 2 +- test/emqx_misc_SUITE.erl | 86 +++++++++++++++++++++++++++------------- 2 files changed, 60 insertions(+), 28 deletions(-) diff --git a/src/emqx_misc.erl b/src/emqx_misc.erl index 249d14005..421f881e3 100644 --- a/src/emqx_misc.erl +++ b/src/emqx_misc.erl @@ -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) -> diff --git a/test/emqx_misc_SUITE.erl b/test/emqx_misc_SUITE.erl index a8337c060..677b25c17 100644 --- a/test/emqx_misc_SUITE.erl +++ b/test/emqx_misc_SUITE.erl @@ -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)