Add more test cases for emqx

This commit is contained in:
zhanghongtong 2019-11-01 16:37:20 +08:00
parent 48f944a9c8
commit 0e81924034
1 changed files with 117 additions and 75 deletions

View File

@ -25,7 +25,6 @@
all() -> emqx_ct:all(?MODULE). all() -> emqx_ct:all(?MODULE).
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_ct_helpers:start_apps([]), emqx_ct_helpers:start_apps([]),
Config. Config.
@ -33,60 +32,20 @@ init_per_suite(Config) ->
end_per_suite(_Config) -> end_per_suite(_Config) ->
emqx_ct_helpers:stop_apps([]). emqx_ct_helpers:stop_apps([]).
t_stop_start(_) ->
t_start(_) -> emqx:stop(),
error('TODO'). false = emqx:is_running(node()),
emqx:start(),
t_restart(_) -> true = emqx:is_running(node()),
error('TODO'). ok = emqx:shutdown(),
false = emqx:is_running(node()),
t_stop(_) -> ok = emqx:reboot(),
error('TODO'). true = emqx:is_running(node()),
ok = emqx:shutdown(for_test),
t_is_running(_) -> false = emqx:is_running(node()).
error('TODO').
t_subscribe(_) ->
error('TODO').
t_publish(_) ->
error('TODO').
t_unsubscribe(_) ->
error('TODO').
t_topics(_) ->
error('TODO').
t_subscribers(_) ->
error('TODO').
t_subscriptions(_) ->
error('TODO').
t_subscribed(_) ->
error('TODO').
t_hook(_) ->
error('TODO').
t_unhook(_) ->
error('TODO').
t_run_hook(_) ->
error('TODO').
t_run_fold_hook(_) ->
error('TODO').
t_shutdown(_) ->
error('TODO').
t_reboot(_) ->
error('TODO').
t_get_env(_) -> t_get_env(_) ->
emqx:start(),
?assertEqual(undefined, emqx:get_env(undefined_key)), ?assertEqual(undefined, emqx:get_env(undefined_key)),
?assertEqual(default_value, emqx:get_env(undefined_key, default_value)), ?assertEqual(default_value, emqx:get_env(undefined_key, default_value)),
application:set_env(emqx, undefined_key, hello), application:set_env(emqx, undefined_key, hello),
@ -95,23 +54,32 @@ t_get_env(_) ->
application:unset_env(emqx, undefined_key). application:unset_env(emqx, undefined_key).
t_emqx_pubsub_api(_) -> t_emqx_pubsub_api(_) ->
emqx:start(),
true = emqx:is_running(node()), true = emqx:is_running(node()),
{ok, C} = emqtt:start_link([{host, "localhost"}, {clientid, "myclient"}]), {ok, C} = emqtt:start_link([{host, "localhost"}, {clientid, "myclient"}]),
{ok, _} = emqtt:connect(C), {ok, _} = emqtt:connect(C),
ClientId = <<"myclient">>, ClientId = <<"myclient">>,
Topic = <<"mytopic">>,
Payload = <<"Hello World">>, Payload = <<"Hello World">>,
Topic = <<"mytopic">>,
Topic1 = <<"mytopic1">>, Topic1 = <<"mytopic1">>,
Topic2 = <<"mytopic2">>,
Topic3 = <<"mytopic3">>,
emqx:subscribe(Topic, ClientId), emqx:subscribe(Topic, ClientId),
emqx:subscribe(Topic1, ClientId, #{qos => 1}),
emqx:subscribe(Topic2, ClientId, #{qos => 2}),
ct:sleep(100), ct:sleep(100),
?assertEqual([Topic], emqx:topics()), ?assertEqual([Topic2, Topic1, Topic], emqx:topics()),
?assertEqual([self()], emqx:subscribers(Topic)), ?assertEqual([self()], emqx:subscribers(Topic)),
?assertEqual([{Topic,#{qos => 0,subid => ClientId}}], emqx:subscriptions(self())), ?assertEqual([self()], emqx:subscribers(Topic1)),
?assertEqual([self()], emqx:subscribers(Topic2)),
?assertEqual([{Topic,#{qos => 0,subid => ClientId}}, {Topic1,#{qos => 1,subid => ClientId}}, {Topic2,#{qos => 2,subid => ClientId}}], emqx:subscriptions(self())),
?assertEqual(true, emqx:subscribed(self(), Topic)), ?assertEqual(true, emqx:subscribed(self(), Topic)),
?assertEqual(true, emqx:subscribed(ClientId, Topic)), ?assertEqual(true, emqx:subscribed(ClientId, Topic)),
?assertEqual(false, emqx:subscribed(self(), Topic1)), ?assertEqual(true, emqx:subscribed(self(), Topic1)),
?assertEqual(false, emqx:subscribed(ClientId, Topic1)), ?assertEqual(true, emqx:subscribed(ClientId, Topic1)),
?assertEqual(true, emqx:subscribed(self(), Topic2)),
?assertEqual(true, emqx:subscribed(ClientId, Topic2)),
?assertEqual(false, emqx:subscribed(self(), Topic3)),
?assertEqual(false, emqx:subscribed(ClientId, Topic3)),
emqx:publish(emqx_message:make(Topic, Payload)), emqx:publish(emqx_message:make(Topic, Payload)),
receive receive
{deliver, Topic, #message{payload = Payload}} -> {deliver, Topic, #message{payload = Payload}} ->
@ -119,26 +87,100 @@ t_emqx_pubsub_api(_) ->
after 100 -> after 100 ->
ct:fail("no_message") ct:fail("no_message")
end, end,
emqx:publish(emqx_message:make(Topic1, Payload)),
receive
{deliver, Topic1, #message{payload = Payload}} ->
ok
after 100 ->
ct:fail("no_message")
end,
emqx:publish(emqx_message:make(Topic2, Payload)),
receive
{deliver, Topic2, #message{payload = Payload}} ->
ok
after 100 ->
ct:fail("no_message")
end,
emqx:unsubscribe(Topic), emqx:unsubscribe(Topic),
emqx:unsubscribe(Topic1),
emqx:unsubscribe(Topic2),
ct:sleep(20), ct:sleep(20),
?assertEqual([], emqx:topics()). ?assertEqual([], emqx:topics()).
t_emqx_hook_api(_) -> t_hook_unhook(_) ->
InitArgs = ['arg2', 'arg3'], ok = emqx:hook(test_hook, fun ?MODULE:hook_fun1/1, []),
emqx:hook('hook.run', fun run/3, InitArgs), ok = emqx:hook(test_hook, fun ?MODULE:hook_fun2/1, []),
ok = emqx:run_hook('hook.run', ['arg1']), ?assertEqual({error, already_exists},
emqx:unhook('hook.run', fun run/3), emqx:hook(test_hook, fun ?MODULE:hook_fun2/1, [])),
ok = emqx:unhook(test_hook, fun ?MODULE:hook_fun1/1),
ok = emqx:unhook(test_hook, fun ?MODULE:hook_fun2/1),
emqx:hook('hook.run_fold', fun add1/1), ok = emqx:hook(emqx_hook, {?MODULE, hook_fun8, []}, 8),
emqx:hook('hook.run_fold', fun add2/1), ok = emqx:hook(emqx_hook, {?MODULE, hook_fun2, []}, 2),
4 = emqx:run_fold_hook('hook.run_fold', [], 1), ok = emqx:hook(emqx_hook, {?MODULE, hook_fun10, []}, 10),
emqx:unhook('hook.run_fold', fun add1/1), ok = emqx:hook(emqx_hook, {?MODULE, hook_fun9, []}, 9),
emqx:unhook('hook.run_fold', fun add2/1). ok = emqx:unhook(emqx_hook, {?MODULE, hook_fun2, []}),
ok = emqx:unhook(emqx_hook, {?MODULE, hook_fun8, []}),
ok = emqx:unhook(emqx_hook, {?MODULE, hook_fun9, []}),
ok = emqx:unhook(emqx_hook, {?MODULE, hook_fun10, []}).
run('arg1', 'arg2', 'arg3') -> t_run_hook(_) ->
ok; ok = emqx:hook(foldl_hook, fun ?MODULE:hook_fun3/4, [init]),
run(_, _, _) -> ok = emqx:hook(foldl_hook, {?MODULE, hook_fun3, [init]}),
ct:fail("no_match"). ok = emqx:hook(foldl_hook, fun ?MODULE:hook_fun4/4, [init]),
ok = emqx:hook(foldl_hook, fun ?MODULE:hook_fun5/4, [init]),
[r5,r4] = emqx:run_fold_hook(foldl_hook, [arg1, arg2], []),
[] = emqx:run_fold_hook(unknown_hook, [], []),
add1(N) -> {ok, N + 1}. ok = emqx:hook(foldl_hook2, fun ?MODULE:hook_fun9/2),
add2(N) -> {ok, N + 2}. ok = emqx:hook(foldl_hook2, {?MODULE, hook_fun10, []}),
[r9] = emqx:run_fold_hook(foldl_hook2, [arg], []),
ok = emqx:hook(foreach_hook, fun ?MODULE:hook_fun6/2, [initArg]),
{error, already_exists} = emqx:hook(foreach_hook, fun ?MODULE:hook_fun6/2, [initArg]),
ok = emqx:hook(foreach_hook, fun ?MODULE:hook_fun7/2, [initArg]),
ok = emqx:hook(foreach_hook, fun ?MODULE:hook_fun8/2, [initArg]),
ok = emqx:run_hook(foreach_hook, [arg]),
ok = emqx:hook(foreach_filter1_hook, {?MODULE, hook_fun1, []}, {?MODULE, hook_filter1, []}, 0),
?assertEqual(ok, emqx:run_hook(foreach_filter1_hook, [arg])), %% filter passed
?assertEqual(ok, emqx:run_hook(foreach_filter1_hook, [arg1])), %% filter failed
ok = emqx:hook(foldl_filter2_hook, {?MODULE, hook_fun2, []}, {?MODULE, hook_filter2, [init_arg]}),
ok = emqx:hook(foldl_filter2_hook, {?MODULE, hook_fun2_1, []}, {?MODULE, hook_filter2_1, [init_arg]}),
?assertEqual(3, emqx:run_fold_hook(foldl_filter2_hook, [arg], 1)),
?assertEqual(2, emqx:run_fold_hook(foldl_filter2_hook, [arg1], 1)).
%%--------------------------------------------------------------------
%% Hook fun
%%--------------------------------------------------------------------
hook_fun1(arg) -> ok;
hook_fun1(_) -> error.
hook_fun2(arg) -> ok;
hook_fun2(_) -> error.
hook_fun2(_, Acc) -> {ok, Acc + 1}.
hook_fun2_1(_, Acc) -> {ok, Acc + 1}.
hook_fun3(arg1, arg2, _Acc, init) -> ok.
hook_fun4(arg1, arg2, Acc, init) -> {ok, [r4 | Acc]}.
hook_fun5(arg1, arg2, Acc, init) -> {ok, [r5 | Acc]}.
hook_fun6(arg, initArg) -> ok.
hook_fun7(arg, initArg) -> ok.
hook_fun8(arg, initArg) -> ok.
hook_fun9(arg, Acc) -> {stop, [r9 | Acc]}.
hook_fun10(arg, Acc) -> {stop, [r10 | Acc]}.
hook_filter1(arg) -> true;
hook_filter1(_) -> false.
hook_filter2(arg, _Acc, init_arg) -> true;
hook_filter2(_, _Acc, _IntArg) -> false.
hook_filter2_1(arg, _Acc, init_arg) -> true;
hook_filter2_1(arg1, _Acc, init_arg) -> true;
hook_filter2_1(_, _Acc, _IntArg) -> false.