emqttd_mqueue_tests.erl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. %%%-----------------------------------------------------------------------------
  2. %%% @Copyright (C) 2012-2016, Feng Lee <feng@emqtt.io>
  3. %%%
  4. %%% Permission is hereby granted, free of charge, to any person obtaining a copy
  5. %%% of this software and associated documentation files (the "Software"), to deal
  6. %%% in the Software without restriction, including without limitation the rights
  7. %%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. %%% copies of the Software, and to permit persons to whom the Software is
  9. %%% furnished to do so, subject to the following conditions:
  10. %%%
  11. %%% The above copyright notice and this permission notice shall be included in all
  12. %%% copies or substantial portions of the Software.
  13. %%%
  14. %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. %%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. %%% SOFTWARE.
  21. %%%-----------------------------------------------------------------------------
  22. -module(emqttd_mqueue_tests).
  23. -include("emqttd.hrl").
  24. -define(Q, emqttd_mqueue).
  25. -ifdef(TEST).
  26. -include_lib("eunit/include/eunit.hrl").
  27. in_test() ->
  28. Opts = [{max_length, 5},
  29. {queue_qos0, true}],
  30. Q = ?Q:new(<<"testQ">>, Opts, alarm_fun()),
  31. ?assertEqual(true, ?Q:is_empty(Q)),
  32. Q1 = ?Q:in(#mqtt_message{}, Q),
  33. ?assertEqual(1, ?Q:len(Q1)),
  34. Q2 = ?Q:in(#mqtt_message{qos = 1}, Q1),
  35. ?assertEqual(2, ?Q:len(Q2)),
  36. Q3 = ?Q:in(#mqtt_message{qos = 2}, Q2),
  37. Q4 = ?Q:in(#mqtt_message{}, Q3),
  38. Q5 = ?Q:in(#mqtt_message{}, Q4),
  39. ?assertEqual(5, ?Q:len(Q5)).
  40. in_qos0_test() ->
  41. Opts = [{max_length, 5},
  42. {queue_qos0, false}],
  43. Q = ?Q:new(<<"testQ">>, Opts, alarm_fun()),
  44. Q1 = ?Q:in(#mqtt_message{}, Q),
  45. ?assertEqual(true, ?Q:is_empty(Q1)),
  46. Q2 = ?Q:in(#mqtt_message{qos = 0}, Q1),
  47. ?assertEqual(true, ?Q:is_empty(Q2)).
  48. out_test() ->
  49. Opts = [{max_length, 5},
  50. {queue_qos0, true}],
  51. Q = ?Q:new(<<"testQ">>, Opts, alarm_fun()),
  52. ?assertMatch({empty, Q}, ?Q:out(Q)),
  53. Q1 = ?Q:in(#mqtt_message{}, Q),
  54. {Value, Q2} = ?Q:out(Q1),
  55. ?assertEqual(0, ?Q:len(Q2)),
  56. ?assertMatch({value, #mqtt_message{}}, Value).
  57. simple_mqueue_test() ->
  58. Opts = [{type, simple},
  59. {max_length, 3},
  60. {low_watermark, 0.2},
  61. {high_watermark, 0.6},
  62. {queue_qos0, false}],
  63. Q = ?Q:new("simple_queue", Opts, alarm_fun()),
  64. ?assertEqual(simple, ?Q:type(Q)),
  65. ?assertEqual(3, ?Q:max_len(Q)),
  66. ?assertEqual(<<"simple_queue">>, ?Q:name(Q)),
  67. ?assert(?Q:is_empty(Q)),
  68. Q1 = ?Q:in(#mqtt_message{qos = 1, payload = <<"1">>}, Q),
  69. Q2 = ?Q:in(#mqtt_message{qos = 1, payload = <<"2">>}, Q1),
  70. Q3 = ?Q:in(#mqtt_message{qos = 1, payload = <<"3">>}, Q2),
  71. Q4 = ?Q:in(#mqtt_message{qos = 1, payload = <<"4">>}, Q3),
  72. ?assertEqual(3, ?Q:len(Q4)),
  73. {{value, Msg}, Q5} = ?Q:out(Q4),
  74. ?assertMatch(<<"2">>, Msg#mqtt_message.payload),
  75. ?assertEqual([{len, 2}, {max_len, 3}, {dropped, 1}], ?Q:stats(Q5)).
  76. infinity_simple_mqueue_test() ->
  77. Opts = [{type, simple},
  78. {max_length, infinity},
  79. {low_watermark, 0.2},
  80. {high_watermark, 0.6},
  81. {queue_qos0, false}],
  82. Q = ?Q:new("infinity_simple_queue", Opts, alarm_fun()),
  83. ?assert(?Q:is_empty(Q)),
  84. ?assertEqual(infinity, ?Q:max_len(Q)),
  85. Qx = lists:foldl(fun(I, AccQ) ->
  86. ?Q:in(#mqtt_message{qos = 1, payload = iolist_to_binary([I])}, AccQ)
  87. end, Q, lists:seq(1, 255)),
  88. ?assertEqual(255, ?Q:len(Qx)),
  89. ?assertEqual([{len, 255}, {max_len, infinity}, {dropped, 0}], ?Q:stats(Qx)),
  90. {{value, V}, Qy} = ?Q:out(Qx),
  91. ?assertEqual(<<1>>, V#mqtt_message.payload).
  92. priority_mqueue_test() ->
  93. Opts = [{type, priority},
  94. {priority, [{<<"t">>, 10}]},
  95. {max_length, 3},
  96. {low_watermark, 0.2},
  97. {high_watermark, 0.6},
  98. {queue_qos0, false}],
  99. Q = ?Q:new("priority_queue", Opts, alarm_fun()),
  100. ?assertEqual(priority, ?Q:type(Q)),
  101. ?assertEqual(3, ?Q:max_len(Q)),
  102. ?assertEqual(<<"priority_queue">>, ?Q:name(Q)),
  103. ?assert(?Q:is_empty(Q)),
  104. Q1 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t1">>}, Q),
  105. Q2 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t">>}, Q1),
  106. Q3 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t2">>}, Q2),
  107. ?assertEqual(3, ?Q:len(Q3)),
  108. Q4 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t1">>}, Q3),
  109. ?assertEqual(4, ?Q:len(Q4)),
  110. Q5 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t1">>}, Q4),
  111. ?assertEqual(5, ?Q:len(Q5)),
  112. Q6 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t1">>}, Q5),
  113. ?assertEqual(5, ?Q:len(Q6)),
  114. {{value, Msg}, Q7} = ?Q:out(Q6),
  115. ?assertMatch(<<"t">>, Msg#mqtt_message.topic).
  116. infinity_priority_mqueue_test() ->
  117. Opts = [{type, priority},
  118. {priority, [{<<"t1">>, 10}, {<<"t2">>, 8}]},
  119. {max_length, infinity},
  120. {queue_qos0, false}],
  121. Q = ?Q:new("infinity_priority_queue", Opts, alarm_fun()),
  122. ?assertEqual(infinity, ?Q:max_len(Q)),
  123. Qx = lists:foldl(fun(I, AccQ) ->
  124. AccQ1 =
  125. ?Q:in(#mqtt_message{topic = <<"t1">>, qos = 1, payload = iolist_to_binary([I])}, AccQ),
  126. ?Q:in(#mqtt_message{topic = <<"t">>, qos = 1, payload = iolist_to_binary([I])}, AccQ1)
  127. end, Q, lists:seq(1, 255)),
  128. ?assertEqual(510, ?Q:len(Qx)),
  129. ?assertEqual([{len, 510}, {max_len, infinity}, {dropped, 0}], ?Q:stats(Qx)).
  130. alarm_fun() -> fun(_, _) -> alarm_fun() end.
  131. -endif.