emqttd_mqueue_SUITE.erl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2012-2016 Feng Lee <feng@emqtt.io>.
  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(emqttd_mqueue_SUITE).
  17. -compile(export_all).
  18. -include("emqttd.hrl").
  19. -define(Q, emqttd_mqueue).
  20. all() -> [t_in, t_in_qos0, t_out, t_simple_mqueue, t_priority_mqueue,
  21. t_priority_mqueue2, t_infinity_priority_mqueue,
  22. t_infinity_simple_mqueue].
  23. t_in(_) ->
  24. Opts = [{max_length, 5},
  25. {queue_qos0, true}],
  26. Q = ?Q:new(<<"testQ">>, Opts, alarm_fun()),
  27. true = ?Q:is_empty(Q),
  28. Q1 = ?Q:in(#mqtt_message{}, Q),
  29. 1 = ?Q:len(Q1),
  30. Q2 = ?Q:in(#mqtt_message{qos = 1}, Q1),
  31. 2 = ?Q:len(Q2),
  32. Q3 = ?Q:in(#mqtt_message{qos = 2}, Q2),
  33. Q4 = ?Q:in(#mqtt_message{}, Q3),
  34. Q5 = ?Q:in(#mqtt_message{}, Q4),
  35. 5 = ?Q:len(Q5).
  36. t_in_qos0(_) ->
  37. Opts = [{max_length, 5},
  38. {queue_qos0, false}],
  39. Q = ?Q:new(<<"testQ">>, Opts, alarm_fun()),
  40. Q1 = ?Q:in(#mqtt_message{}, Q),
  41. true = ?Q:is_empty(Q1),
  42. Q2 = ?Q:in(#mqtt_message{qos = 0}, Q1),
  43. true = ?Q:is_empty(Q2).
  44. t_out(_) ->
  45. Opts = [{max_length, 5},
  46. {queue_qos0, true}],
  47. Q = ?Q:new(<<"testQ">>, Opts, alarm_fun()),
  48. {empty, Q} = ?Q:out(Q),
  49. Q1 = ?Q:in(#mqtt_message{}, Q),
  50. {Value, Q2} = ?Q:out(Q1),
  51. 0 = ?Q:len(Q2),
  52. {value, #mqtt_message{}} = Value.
  53. t_simple_mqueue(_) ->
  54. Opts = [{type, simple},
  55. {max_length, 3},
  56. {low_watermark, 0.2},
  57. {high_watermark, 0.6},
  58. {queue_qos0, false}],
  59. Q = ?Q:new("simple_queue", Opts, alarm_fun()),
  60. simple = ?Q:type(Q),
  61. 3 = ?Q:max_len(Q),
  62. <<"simple_queue">> = ?Q:name(Q),
  63. true = ?Q:is_empty(Q),
  64. Q1 = ?Q:in(#mqtt_message{qos = 1, payload = <<"1">>}, Q),
  65. Q2 = ?Q:in(#mqtt_message{qos = 1, payload = <<"2">>}, Q1),
  66. Q3 = ?Q:in(#mqtt_message{qos = 1, payload = <<"3">>}, Q2),
  67. Q4 = ?Q:in(#mqtt_message{qos = 1, payload = <<"4">>}, Q3),
  68. 3 = ?Q:len(Q4),
  69. {{value, Msg}, Q5} = ?Q:out(Q4),
  70. <<"2">> = Msg#mqtt_message.payload,
  71. [{len, 2}, {max_len, 3}, {dropped, 1}] = ?Q:stats(Q5).
  72. t_infinity_simple_mqueue(_) ->
  73. Opts = [{type, simple},
  74. {max_length, infinity},
  75. {low_watermark, 0.2},
  76. {high_watermark, 0.6},
  77. {queue_qos0, false}],
  78. Q = ?Q:new("infinity_simple_queue", Opts, alarm_fun()),
  79. true = ?Q:is_empty(Q),
  80. infinity = ?Q:max_len(Q),
  81. Qx = lists:foldl(fun(I, AccQ) ->
  82. ?Q:in(#mqtt_message{qos = 1, payload = iolist_to_binary([I])}, AccQ)
  83. end, Q, lists:seq(1, 255)),
  84. 255 = ?Q:len(Qx),
  85. [{len, 255}, {max_len, infinity}, {dropped, 0}] = ?Q:stats(Qx),
  86. {{value, V}, _Qy} = ?Q:out(Qx),
  87. <<1>> = V#mqtt_message.payload.
  88. t_priority_mqueue(_) ->
  89. Opts = [{type, priority},
  90. {priority, [{<<"t">>, 10}]},
  91. {max_length, 3},
  92. {low_watermark, 0.2},
  93. {high_watermark, 0.6},
  94. {queue_qos0, false}],
  95. Q = ?Q:new("priority_queue", Opts, alarm_fun()),
  96. priority = ?Q:type(Q),
  97. 3 = ?Q:max_len(Q),
  98. <<"priority_queue">> = ?Q:name(Q),
  99. true = ?Q:is_empty(Q),
  100. Q1 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t1">>}, Q),
  101. Q2 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t">>}, Q1),
  102. Q3 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t2">>}, Q2),
  103. 3 = ?Q:len(Q3),
  104. Q4 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t1">>}, Q3),
  105. 4 = ?Q:len(Q4),
  106. Q5 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t1">>}, Q4),
  107. 5 = ?Q:len(Q5),
  108. Q6 = ?Q:in(#mqtt_message{qos = 1, topic = <<"t1">>}, Q5),
  109. 5 = ?Q:len(Q6),
  110. {{value, Msg}, _Q7} = ?Q:out(Q6),
  111. <<"t">> = Msg#mqtt_message.topic.
  112. t_infinity_priority_mqueue(_) ->
  113. Opts = [{type, priority},
  114. {priority, [{<<"t1">>, 10}, {<<"t2">>, 8}]},
  115. {max_length, infinity},
  116. {queue_qos0, false}],
  117. Q = ?Q:new("infinity_priority_queue", Opts, alarm_fun()),
  118. infinity = ?Q:max_len(Q),
  119. Qx = lists:foldl(fun(I, AccQ) ->
  120. AccQ1 =
  121. ?Q:in(#mqtt_message{topic = <<"t1">>, qos = 1, payload = iolist_to_binary([I])}, AccQ),
  122. ?Q:in(#mqtt_message{topic = <<"t">>, qos = 1, payload = iolist_to_binary([I])}, AccQ1)
  123. end, Q, lists:seq(1, 255)),
  124. 510 = ?Q:len(Qx),
  125. [{len, 510}, {max_len, infinity}, {dropped, 0}] = ?Q:stats(Qx).
  126. t_priority_mqueue2(_) ->
  127. Opts = [{type, priority},
  128. {max_length, 2},
  129. {low_watermark, 0.2},
  130. {high_watermark, 0.6},
  131. {queue_qos0, false}],
  132. Q = ?Q:new("priority_queue2_test", Opts, alarm_fun()),
  133. 2 = ?Q:max_len(Q),
  134. Q1 = ?Q:in(#mqtt_message{topic = <<"x">>, qos = 1, payload = <<1>>}, Q),
  135. Q2 = ?Q:in(#mqtt_message{topic = <<"x">>, qos = 1, payload = <<2>>}, Q1),
  136. Q3 = ?Q:in(#mqtt_message{topic = <<"y">>, qos = 1, payload = <<3>>}, Q2),
  137. Q4 = ?Q:in(#mqtt_message{topic = <<"y">>, qos = 1, payload = <<4>>}, Q3),
  138. 4 = ?Q:len(Q4),
  139. {{value, _Val}, Q5} = ?Q:out(Q4),
  140. 3 = ?Q:len(Q5).
  141. alarm_fun() -> fun(_, _) -> alarm_fun() end.