emqx/test/emqx_router_SUITE.erl
2018-05-22 13:01:19 +08:00

106 lines
3.4 KiB
Erlang

%%%===================================================================
%%% Copyright (c) 2013-2018 EMQ Inc. All rights reserved.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%===================================================================
-module(emqx_router_SUITE).
-include("emqx.hrl").
-include_lib("eunit/include/eunit.hrl").
-compile(export_all).
-compile(nowarn_export_all).
-define(R, emqx_router).
-define(TABS, [emqx_route, emqx_trie, emqx_trie_node]).
all() ->
[{group, route}].
groups() ->
[{route, [sequence],
[add_del_route,
match_routes]}].
init_per_suite(Config) ->
emqx_ct_broker_helpers:run_setup_steps(),
Config.
end_per_suite(_Config) ->
emqx_ct_broker_helpers:run_teardown_steps().
init_per_testcase(_TestCase, Config) ->
Config.
end_per_testcase(_TestCase, _Config) ->
clear_tables().
add_del_route(_) ->
From = {self(), make_ref()},
?R:add_route(From, <<"a/b/c">>, node()),
?R:add_route(From, <<"a/b/c">>, node()),
?R:add_route(From, <<"a/+/b">>, node()),
?assertEqual([<<"a/+/b">>, <<"a/b/c">>], lists:sort(?R:topics())),
?R:del_route(From, <<"a/b/c">>, node()),
?R:del_route(From, <<"a/+/b">>, node()),
?assertEqual([], lists:sort(?R:topics())).
match_routes(_) ->
From = {self(), make_ref()},
?R:add_route(From, <<"a/b/c">>, node()),
?R:add_route(From, <<"a/+/c">>, node()),
?R:add_route(From, <<"a/b/#">>, node()),
?R:add_route(From, <<"#">>, node()),
?assertEqual([#route{topic = <<"#">>, dest = node()},
#route{topic = <<"a/+/c">>, dest = node()},
#route{topic = <<"a/b/#">>, dest = node()},
#route{topic = <<"a/b/c">>, dest = node()}],
lists:sort(?R:match_routes(<<"a/b/c">>))).
has_routes(_) ->
From = {self(), make_ref()},
?R:add_route(From, <<"devices/+/messages">>, node()),
?assert(?R:has_routes(<<"devices/+/messages">>)).
clear_tables() ->
lists:foreach(fun mnesia:clear_table/1, ?TABS).
router_add_del(_) ->
?R:add_route(<<"#">>),
?R:add_route(<<"a/b/c">>),
?R:add_route(<<"+/#">>),
Routes = [R1, R2 | _] = [
#route{topic = <<"#">>, dest = node()},
#route{topic = <<"+/#">>, dest = node()},
#route{topic = <<"a/b/c">>, dest = node()}],
?assertEqual(Routes, lists:sort(?R:match_routes(<<"a/b/c">>))),
%% Batch Add
lists:foreach(fun(R) -> ?R:add_route(R) end, Routes),
?assertEqual(Routes, lists:sort(?R:match_routes(<<"a/b/c">>))),
%% Del
?R:del_route(<<"a/b/c">>),
[R1, R2] = lists:sort(?R:match(<<"a/b/c">>)),
{atomic, []} = mnesia:transaction(fun emqx_trie:lookup/1, [<<"a/b/c">>]),
%% Batch Del
R3 = #route{topic = <<"#">>, dest = 'a@127.0.0.1'},
?R:add_route(R3),
?R:del_route(R1),
?R:del_route(R2),
?R:del_route(R3),
[] = lists:sort(?R:match(<<"a/b/c">>)).