emqx_mqueue_SUITE.erl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2017-2023 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_mqueue_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("emqx/include/emqx.hrl").
  20. -include_lib("emqx/include/emqx_mqtt.hrl").
  21. -include_lib("proper/include/proper.hrl").
  22. -include_lib("eunit/include/eunit.hrl").
  23. -define(Q, emqx_mqueue).
  24. all() -> emqx_common_test_helpers:all(?MODULE).
  25. t_info(_) ->
  26. Q = ?Q:init(#{max_len => 5, store_qos0 => true}),
  27. true = ?Q:info(store_qos0, Q),
  28. 5 = ?Q:info(max_len, Q),
  29. 0 = ?Q:info(len, Q),
  30. 0 = ?Q:info(dropped, Q),
  31. #{
  32. store_qos0 := true,
  33. max_len := 5,
  34. len := 0,
  35. dropped := 0
  36. } = ?Q:info(Q).
  37. t_in(_) ->
  38. Opts = #{max_len => 5, store_qos0 => true},
  39. Q = ?Q:init(Opts),
  40. ?assert(?Q:is_empty(Q)),
  41. {_, Q1} = ?Q:in(#message{}, Q),
  42. ?assertEqual(1, ?Q:len(Q1)),
  43. {_, Q2} = ?Q:in(#message{qos = 1}, Q1),
  44. ?assertEqual(2, ?Q:len(Q2)),
  45. {_, Q3} = ?Q:in(#message{qos = 2}, Q2),
  46. {_, Q4} = ?Q:in(#message{}, Q3),
  47. {_, Q5} = ?Q:in(#message{}, Q4),
  48. ?assertEqual(5, ?Q:len(Q5)).
  49. t_in_qos0(_) ->
  50. Opts = #{max_len => 5, store_qos0 => false},
  51. Q = ?Q:init(Opts),
  52. {_, Q1} = ?Q:in(#message{qos = 0}, Q),
  53. ?assert(?Q:is_empty(Q1)),
  54. {_, Q2} = ?Q:in(#message{qos = 0}, Q1),
  55. ?assert(?Q:is_empty(Q2)).
  56. t_out(_) ->
  57. Opts = #{max_len => 5, store_qos0 => true},
  58. Q = ?Q:init(Opts),
  59. {empty, Q} = ?Q:out(Q),
  60. {_, Q1} = ?Q:in(#message{}, Q),
  61. {Value, Q2} = ?Q:out(Q1),
  62. ?assertEqual(0, ?Q:len(Q2)),
  63. ?assertEqual({value, #message{}}, Value).
  64. t_simple_mqueue(_) ->
  65. Opts = #{max_len => 3, store_qos0 => false},
  66. Q = ?Q:init(Opts),
  67. ?assertEqual(3, ?Q:max_len(Q)),
  68. ?assert(?Q:is_empty(Q)),
  69. {_, Q1} = ?Q:in(#message{qos = 1, payload = <<"1">>}, Q),
  70. {_, Q2} = ?Q:in(#message{qos = 1, payload = <<"2">>}, Q1),
  71. {_, Q3} = ?Q:in(#message{qos = 1, payload = <<"3">>}, Q2),
  72. {_, Q4} = ?Q:in(#message{qos = 1, payload = <<"4">>}, Q3),
  73. ?assertEqual(3, ?Q:len(Q4)),
  74. {{value, Msg}, Q5} = ?Q:out(Q4),
  75. ?assertEqual(<<"2">>, Msg#message.payload),
  76. ?assertEqual([{len, 2}, {max_len, 3}, {dropped, 1}], ?Q:stats(Q5)).
  77. t_infinity_simple_mqueue(_) ->
  78. Opts = #{max_len => 0, store_qos0 => false},
  79. Q = ?Q:init(Opts),
  80. ?assert(?Q:is_empty(Q)),
  81. ?assertEqual(0, ?Q:max_len(Q)),
  82. Qx = lists:foldl(
  83. fun(I, AccQ) ->
  84. {_, NewQ} = ?Q:in(#message{qos = 1, payload = iolist_to_binary([I])}, AccQ),
  85. NewQ
  86. end,
  87. Q,
  88. lists:seq(1, 255)
  89. ),
  90. ?assertEqual(255, ?Q:len(Qx)),
  91. ?assertEqual([{len, 255}, {max_len, 0}, {dropped, 0}], ?Q:stats(Qx)),
  92. {{value, V}, _Qy} = ?Q:out(Qx),
  93. ?assertEqual(<<1>>, V#message.payload).
  94. t_priority_mqueue(_) ->
  95. Opts = #{
  96. max_len => 3,
  97. priorities =>
  98. #{
  99. <<"t1">> => 1,
  100. <<"t2">> => 2,
  101. <<"t3">> => 3
  102. },
  103. store_qos0 => false
  104. },
  105. Q = ?Q:init(Opts),
  106. ?assertEqual(3, ?Q:max_len(Q)),
  107. ?assert(?Q:is_empty(Q)),
  108. {_, Q1} = ?Q:in(#message{qos = 1, topic = <<"t2">>}, Q),
  109. {_, Q2} = ?Q:in(#message{qos = 1, topic = <<"t1">>}, Q1),
  110. {_, Q3} = ?Q:in(#message{qos = 1, topic = <<"t3">>}, Q2),
  111. ?assertEqual(3, ?Q:len(Q3)),
  112. {_, Q4} = ?Q:in(#message{qos = 1, topic = <<"t2">>}, Q3),
  113. ?assertEqual(4, ?Q:len(Q4)),
  114. {_, Q5} = ?Q:in(#message{qos = 1, topic = <<"t2">>}, Q4),
  115. ?assertEqual(5, ?Q:len(Q5)),
  116. {_, Q6} = ?Q:in(#message{qos = 1, topic = <<"t2">>}, Q5),
  117. ?assertEqual(5, ?Q:len(Q6)),
  118. {{value, _Msg}, Q7} = ?Q:out(Q6),
  119. ?assertEqual(4, ?Q:len(Q7)).
  120. t_priority_mqueue_conservation(_) ->
  121. true = proper:quickcheck(conservation_prop()).
  122. t_priority_order(_) ->
  123. Opts = #{
  124. max_len => 5,
  125. shift_multiplier => 1,
  126. priorities =>
  127. #{
  128. <<"t1">> => 0,
  129. <<"t2">> => 1,
  130. <<"t3">> => 2
  131. },
  132. store_qos0 => false
  133. },
  134. Messages = [
  135. {Topic, Message}
  136. || Topic <- [<<"t1">>, <<"t2">>, <<"t3">>],
  137. Message <- lists:seq(1, 10)
  138. ],
  139. Q = lists:foldl(
  140. fun({Topic, Message}, Q) ->
  141. element(2, ?Q:in(#message{topic = Topic, qos = 1, payload = Message}, Q))
  142. end,
  143. ?Q:init(Opts),
  144. Messages
  145. ),
  146. ?assertMatch(
  147. [
  148. {<<"t3">>, 6},
  149. {<<"t3">>, 7},
  150. {<<"t3">>, 8},
  151. {<<"t2">>, 6},
  152. {<<"t2">>, 7},
  153. {<<"t1">>, 6},
  154. {<<"t3">>, 9},
  155. {<<"t3">>, 10},
  156. {<<"t2">>, 8},
  157. %% Note: for performance reasons we don't reset the
  158. %% counter when we run out of messages with the
  159. %% current prio, so next is t1:
  160. {<<"t1">>, 7},
  161. {<<"t2">>, 9},
  162. {<<"t2">>, 10},
  163. {<<"t1">>, 8},
  164. {<<"t1">>, 9},
  165. {<<"t1">>, 10}
  166. ],
  167. drain(Q)
  168. ).
  169. t_priority_order2(_) ->
  170. Opts = #{
  171. max_len => 5,
  172. shift_multiplier => 2,
  173. priorities =>
  174. #{
  175. <<"t1">> => 0,
  176. <<"t2">> => 1
  177. },
  178. store_qos0 => false
  179. },
  180. Messages = [
  181. {Topic, Message}
  182. || Topic <- [<<"t1">>, <<"t2">>],
  183. Message <- lists:seq(1, 10)
  184. ],
  185. Q = lists:foldl(
  186. fun({Topic, Message}, Q) ->
  187. element(2, ?Q:in(#message{topic = Topic, qos = 1, payload = Message}, Q))
  188. end,
  189. ?Q:init(Opts),
  190. Messages
  191. ),
  192. ?assertMatch(
  193. [
  194. {<<"t2">>, 6},
  195. {<<"t2">>, 7},
  196. {<<"t2">>, 8},
  197. {<<"t2">>, 9},
  198. {<<"t1">>, 6},
  199. {<<"t1">>, 7},
  200. {<<"t2">>, 10},
  201. {<<"t1">>, 8},
  202. {<<"t1">>, 9},
  203. {<<"t1">>, 10}
  204. ],
  205. drain(Q)
  206. ).
  207. t_infinity_priority_mqueue(_) ->
  208. Opts = #{
  209. max_len => 0,
  210. priorities =>
  211. #{
  212. <<"t">> => 1,
  213. <<"t1">> => 2
  214. },
  215. store_qos0 => false
  216. },
  217. Q = ?Q:init(Opts),
  218. ?assertEqual(0, ?Q:max_len(Q)),
  219. Qx = lists:foldl(
  220. fun(I, AccQ) ->
  221. {undefined, AccQ1} = ?Q:in(
  222. #message{topic = <<"t1">>, qos = 1, payload = iolist_to_binary([I])}, AccQ
  223. ),
  224. {undefined, AccQ2} = ?Q:in(
  225. #message{topic = <<"t">>, qos = 1, payload = iolist_to_binary([I])}, AccQ1
  226. ),
  227. AccQ2
  228. end,
  229. Q,
  230. lists:seq(1, 255)
  231. ),
  232. ?assertEqual(510, ?Q:len(Qx)),
  233. ?assertEqual([{len, 510}, {max_len, 0}, {dropped, 0}], ?Q:stats(Qx)).
  234. %%TODO: fixme later
  235. t_length_priority_mqueue(_) ->
  236. Opts = #{
  237. max_len => 2,
  238. store_qos0 => false
  239. },
  240. Q = ?Q:init(Opts),
  241. 2 = ?Q:max_len(Q),
  242. {_, Q1} = ?Q:in(#message{topic = <<"x">>, qos = 1, payload = <<1>>}, Q),
  243. {_, Q2} = ?Q:in(#message{topic = <<"x">>, qos = 1, payload = <<2>>}, Q1),
  244. {_, Q3} = ?Q:in(#message{topic = <<"y">>, qos = 1, payload = <<3>>}, Q2),
  245. {_, Q4} = ?Q:in(#message{topic = <<"y">>, qos = 1, payload = <<4>>}, Q3),
  246. ?assertEqual(2, ?Q:len(Q4)),
  247. {{value, _Val}, Q5} = ?Q:out(Q4),
  248. ?assertEqual(1, ?Q:len(Q5)).
  249. t_dropped(_) ->
  250. Q = ?Q:init(#{max_len => 1, store_qos0 => true}),
  251. Msg = emqx_message:make(<<"t">>, <<"payload">>),
  252. {undefined, Q1} = ?Q:in(Msg, Q),
  253. {Msg, Q2} = ?Q:in(Msg, Q1),
  254. ?assertEqual(1, ?Q:dropped(Q2)).
  255. conservation_prop() ->
  256. ?FORALL(
  257. {Priorities, Messages},
  258. ?LET(
  259. Priorities,
  260. topic_priorities(),
  261. {Priorities, messages(Priorities)}
  262. ),
  263. try
  264. Opts = #{
  265. max_len => 0,
  266. priorities => maps:from_list(Priorities),
  267. store_qos0 => false
  268. },
  269. %% Put messages in
  270. Q1 = lists:foldl(
  271. fun({Topic, Message}, Q) ->
  272. element(2, ?Q:in(#message{topic = Topic, qos = 1, payload = Message}, Q))
  273. end,
  274. ?Q:init(Opts),
  275. Messages
  276. ),
  277. %% Collect messages
  278. Got = lists:sort(drain(Q1)),
  279. Expected = lists:sort(Messages),
  280. case Expected =:= Got of
  281. true ->
  282. true;
  283. false ->
  284. ct:pal("Mismatch: expected ~p~nGot ~p~n", [Expected, Got]),
  285. false
  286. end
  287. catch
  288. EC:Err:Stack ->
  289. ct:pal("Error: ~p", [{EC, Err, Stack}]),
  290. false
  291. end
  292. ).
  293. %% Proper generators:
  294. topic(Priorities) ->
  295. {Topics, _} = lists:unzip(Priorities),
  296. oneof(Topics).
  297. topic_priorities() ->
  298. non_empty(list({binary(), priority()})).
  299. priority() ->
  300. oneof([integer(), infinity]).
  301. messages(Topics) ->
  302. list({topic(Topics), binary()}).
  303. %% Internal functions:
  304. drain(Q) ->
  305. case ?Q:out(Q) of
  306. {empty, _} ->
  307. [];
  308. {{value, #message{topic = T, payload = P}}, Q1} ->
  309. [{T, P} | drain(Q1)]
  310. end.