emqttd_mqueue_tests.erl 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. %%%-----------------------------------------------------------------------------
  2. %%% @Copyright (C) 2012-2015, 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(QM, 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 = ?QM:new(<<"testQ">>, Opts, alarm_fun()),
  31. ?assertEqual(true, ?QM:is_empty(Q)),
  32. Q1 = ?QM:in(#mqtt_message{}, Q),
  33. ?assertEqual(1, ?QM:len(Q1)),
  34. Q2 = ?QM:in(#mqtt_message{qos = 1}, Q1),
  35. ?assertEqual(2, ?QM:len(Q2)),
  36. Q3 = ?QM:in(#mqtt_message{qos = 2}, Q2),
  37. Q4 = ?QM:in(#mqtt_message{}, Q3),
  38. Q5 = ?QM:in(#mqtt_message{}, Q4),
  39. ?assertEqual(true, ?QM:is_full(Q5)).
  40. in_qos0_test() ->
  41. Opts = [{max_length, 5},
  42. {queue_qos0, false}],
  43. Q = ?QM:new(<<"testQ">>, Opts, alarm_fun()),
  44. Q1 = ?QM:in(#mqtt_message{}, Q),
  45. ?assertEqual(true, ?QM:is_empty(Q1)),
  46. Q2 = ?QM:in(#mqtt_message{qos = 0}, Q1),
  47. ?assertEqual(true, ?QM:is_empty(Q2)).
  48. out_test() ->
  49. Opts = [{max_length, 5},
  50. {queue_qos0, true}],
  51. Q = ?QM:new(<<"testQ">>, Opts, alarm_fun()),
  52. ?assertMatch({empty, Q}, ?QM:out(Q)),
  53. Q1 = ?QM:in(#mqtt_message{}, Q),
  54. {Value, Q2} = ?QM:out(Q1),
  55. ?assertEqual(0, ?QM:len(Q2)),
  56. ?assertMatch({value, #mqtt_message{}}, Value).
  57. alarm_fun() ->
  58. fun(_, _) -> alarm_fun() end.
  59. -endif.