emqx_misc_tests.erl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. %% Copyright (c) 2013-2019 EMQ Technologies Co., Ltd. All Rights Reserved.
  2. %%
  3. %% Licensed under the Apache License, Version 2.0 (the "License");
  4. %% you may not use this file except in compliance with the License.
  5. %% You may obtain a copy of the License at
  6. %%
  7. %% http://www.apache.org/licenses/LICENSE-2.0
  8. %%
  9. %% Unless required by applicable law or agreed to in writing, software
  10. %% distributed under the License is distributed on an "AS IS" BASIS,
  11. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. %% See the License for the specific language governing permissions and
  13. %% limitations under the License.
  14. -module(emqx_misc_tests).
  15. -include_lib("eunit/include/eunit.hrl").
  16. -define(SOCKOPTS, [binary,
  17. {packet, raw},
  18. {reuseaddr, true},
  19. {backlog, 512},
  20. {nodelay, true}]).
  21. t_merge_opts_test() ->
  22. Opts = emqx_misc:merge_opts(?SOCKOPTS, [raw,
  23. binary,
  24. {backlog, 1024},
  25. {nodelay, false},
  26. {max_clients, 1024},
  27. {acceptors, 16}]),
  28. ?assertEqual(1024, proplists:get_value(backlog, Opts)),
  29. ?assertEqual(1024, proplists:get_value(max_clients, Opts)),
  30. [binary, raw,
  31. {acceptors, 16},
  32. {backlog, 1024},
  33. {max_clients, 1024},
  34. {nodelay, false},
  35. {packet, raw},
  36. {reuseaddr, true}] = lists:sort(Opts).
  37. timer_cancel_flush_test() ->
  38. Timer = emqx_misc:start_timer(0, foo),
  39. ok = emqx_misc:cancel_timer(Timer),
  40. receive {timeout, Timer, foo} -> error(unexpected)
  41. after 0 -> ok
  42. end.
  43. shutdown_disabled_test() ->
  44. ok = drain(),
  45. self() ! foo,
  46. ?assertEqual(continue, conn_proc_mng_policy(0)),
  47. receive foo -> ok end,
  48. ?assertEqual(hibernate, conn_proc_mng_policy(0)).
  49. message_queue_too_long_test() ->
  50. ok = drain(),
  51. self() ! foo,
  52. self() ! bar,
  53. ?assertEqual({shutdown, message_queue_too_long},
  54. conn_proc_mng_policy(1)),
  55. receive foo -> ok end,
  56. ?assertEqual(continue, conn_proc_mng_policy(1)),
  57. receive bar -> ok end.
  58. conn_proc_mng_policy(L) ->
  59. emqx_misc:conn_proc_mng_policy(#{message_queue_len => L}).
  60. %% drain self() msg queue for deterministic test behavior
  61. drain() ->
  62. _ = drain([]), % maybe log
  63. ok.
  64. drain(Acc) ->
  65. receive
  66. Msg ->
  67. drain([Msg | Acc])
  68. after
  69. 0 ->
  70. lists:reverse(Acc)
  71. end.