emqx_persistent_session_ds_router_SUITE.erl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2023-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_persistent_session_ds_router_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("emqx/src/emqx_persistent_session_ds/emqx_ps_ds_int.hrl").
  20. -include_lib("eunit/include/eunit.hrl").
  21. -include_lib("common_test/include/ct.hrl").
  22. -define(R, emqx_persistent_session_ds_router).
  23. -define(DEF_DS_SESSION_ID, <<"some-client-id">>).
  24. %%------------------------------------------------------------------------------
  25. %% CT boilerplate
  26. %%------------------------------------------------------------------------------
  27. all() ->
  28. emqx_common_test_helpers:all(?MODULE).
  29. init_per_suite(Config) ->
  30. WorkDir = filename:join([?config(priv_dir, Config), ?MODULE]),
  31. AppSpecs = [
  32. emqx_durable_storage,
  33. {emqx, #{
  34. config => #{durable_sessions => #{enable => true}},
  35. override_env => [{boot_modules, [broker]}]
  36. }}
  37. ],
  38. Apps = emqx_cth_suite:start(AppSpecs, #{work_dir => WorkDir}),
  39. [{apps, Apps} | Config].
  40. end_per_suite(Config) ->
  41. ok = emqx_cth_suite:stop(?config(apps, Config)),
  42. ok.
  43. init_per_testcase(_TestCase, Config) ->
  44. clear_tables(),
  45. Config.
  46. end_per_testcase(_TestCase, _Config) ->
  47. clear_tables().
  48. %%------------------------------------------------------------------------------
  49. %% Helper fns
  50. %%------------------------------------------------------------------------------
  51. clear_tables() ->
  52. lists:foreach(
  53. fun mnesia:clear_table/1,
  54. [?PS_ROUTER_TAB, ?PS_FILTERS_TAB]
  55. ).
  56. add_route(TopicFilter) ->
  57. ?R:do_add_route(TopicFilter, ?DEF_DS_SESSION_ID).
  58. delete_route(TopicFilter) ->
  59. ?R:do_delete_route(TopicFilter, ?DEF_DS_SESSION_ID).
  60. %%------------------------------------------------------------------------------
  61. %% Testcases
  62. %%------------------------------------------------------------------------------
  63. % t_lookup_routes(_) ->
  64. % error('TODO').
  65. t_add_delete(_) ->
  66. ?assertNot(?R:has_any_route(<<"a/b/c">>)),
  67. add_route(<<"a/b/c">>),
  68. ?assert(?R:has_any_route(<<"a/b/c">>)),
  69. add_route(<<"a/b/c">>),
  70. ?assert(?R:has_any_route(<<"a/b/c">>)),
  71. add_route(<<"a/+/b">>),
  72. ?assert(?R:has_any_route(<<"a/b/c">>)),
  73. ?assert(?R:has_any_route(<<"a/c/b">>)),
  74. ?assertEqual([<<"a/+/b">>, <<"a/b/c">>], lists:sort(?R:topics())),
  75. delete_route(<<"a/b/c">>),
  76. ?assertNot(?R:has_any_route(<<"a/b/c">>)),
  77. ?assert(?R:has_any_route(<<"a/c/b">>)),
  78. delete_route(<<"a/+/b">>),
  79. ?assertNot(?R:has_any_route(<<"a/b/c">>)),
  80. ?assertNot(?R:has_any_route(<<"a/c/b">>)),
  81. ?assertEqual([], ?R:topics()).
  82. t_add_delete_incremental(_) ->
  83. add_route(<<"a/b/c">>),
  84. add_route(<<"a/+/c">>),
  85. add_route(<<"a/+/+">>),
  86. add_route(<<"a/b/#">>),
  87. add_route(<<"#">>),
  88. ?assert(?R:has_any_route(<<"any/topic">>)),
  89. ?assertEqual(
  90. [
  91. #ps_route{topic = <<"#">>, dest = ?DEF_DS_SESSION_ID},
  92. #ps_route{topic = <<"a/+/+">>, dest = ?DEF_DS_SESSION_ID},
  93. #ps_route{topic = <<"a/+/c">>, dest = ?DEF_DS_SESSION_ID},
  94. #ps_route{topic = <<"a/b/#">>, dest = ?DEF_DS_SESSION_ID},
  95. #ps_route{topic = <<"a/b/c">>, dest = ?DEF_DS_SESSION_ID}
  96. ],
  97. lists:sort(?R:match_routes(<<"a/b/c">>))
  98. ),
  99. delete_route(<<"a/+/c">>),
  100. ?assertEqual(
  101. [
  102. #ps_route{topic = <<"#">>, dest = ?DEF_DS_SESSION_ID},
  103. #ps_route{topic = <<"a/+/+">>, dest = ?DEF_DS_SESSION_ID},
  104. #ps_route{topic = <<"a/b/#">>, dest = ?DEF_DS_SESSION_ID},
  105. #ps_route{topic = <<"a/b/c">>, dest = ?DEF_DS_SESSION_ID}
  106. ],
  107. lists:sort(?R:match_routes(<<"a/b/c">>))
  108. ),
  109. delete_route(<<"a/+/+">>),
  110. ?assertEqual(
  111. [
  112. #ps_route{topic = <<"#">>, dest = ?DEF_DS_SESSION_ID},
  113. #ps_route{topic = <<"a/b/#">>, dest = ?DEF_DS_SESSION_ID},
  114. #ps_route{topic = <<"a/b/c">>, dest = ?DEF_DS_SESSION_ID}
  115. ],
  116. lists:sort(?R:match_routes(<<"a/b/c">>))
  117. ),
  118. delete_route(<<"a/b/#">>),
  119. ?assertEqual(
  120. [
  121. #ps_route{topic = <<"#">>, dest = ?DEF_DS_SESSION_ID},
  122. #ps_route{topic = <<"a/b/c">>, dest = ?DEF_DS_SESSION_ID}
  123. ],
  124. lists:sort(?R:match_routes(<<"a/b/c">>))
  125. ),
  126. delete_route(<<"a/b/c">>),
  127. ?assertEqual(
  128. [#ps_route{topic = <<"#">>, dest = ?DEF_DS_SESSION_ID}],
  129. lists:sort(?R:match_routes(<<"a/b/c">>))
  130. ).
  131. t_do_add_delete(_) ->
  132. add_route(<<"a/b/c">>),
  133. add_route(<<"a/b/c">>),
  134. add_route(<<"a/+/b">>),
  135. ?assertEqual([<<"a/+/b">>, <<"a/b/c">>], lists:sort(?R:topics())),
  136. delete_route(<<"a/b/c">>),
  137. delete_route(<<"a/+/b">>),
  138. ?assertEqual([], ?R:topics()).
  139. t_match_routes(_) ->
  140. add_route(<<"a/b/c">>),
  141. add_route(<<"a/+/c">>),
  142. add_route(<<"a/b/#">>),
  143. add_route(<<"#">>),
  144. ?assertEqual(
  145. [
  146. #ps_route{topic = <<"#">>, dest = ?DEF_DS_SESSION_ID},
  147. #ps_route{topic = <<"a/+/c">>, dest = ?DEF_DS_SESSION_ID},
  148. #ps_route{topic = <<"a/b/#">>, dest = ?DEF_DS_SESSION_ID},
  149. #ps_route{topic = <<"a/b/c">>, dest = ?DEF_DS_SESSION_ID}
  150. ],
  151. lists:sort(?R:match_routes(<<"a/b/c">>))
  152. ),
  153. delete_route(<<"a/b/c">>),
  154. delete_route(<<"a/+/c">>),
  155. delete_route(<<"a/b/#">>),
  156. delete_route(<<"#">>),
  157. ?assertEqual([], lists:sort(?R:match_routes(<<"a/b/c">>))).
  158. t_print_routes(_) ->
  159. add_route(<<"+/#">>),
  160. add_route(<<"+/+">>),
  161. ?R:print_routes(<<"a/b">>).
  162. t_has_route(_) ->
  163. add_route(<<"devices/+/messages">>),
  164. ?assert(?R:has_route(<<"devices/+/messages">>, ?DEF_DS_SESSION_ID)),
  165. delete_route(<<"devices/+/messages">>).