emqx_stats_SUITE.erl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2018-2024 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_stats_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. all() -> emqx_common_test_helpers:all(?MODULE).
  21. t_cast_useless_msg(_) ->
  22. emqx_stats:setstat('notExis', 1),
  23. with_proc(fun() ->
  24. emqx_stats ! useless,
  25. ?assertEqual(ok, gen_server:cast(emqx_stats, useless))
  26. end).
  27. t_get_error_state(_) ->
  28. Conns = emqx_stats:getstats(),
  29. ?assertEqual([], Conns).
  30. t_get_state(_) ->
  31. with_proc(fun() ->
  32. ?assertEqual(0, emqx_stats:getstat('notExist')),
  33. SetConnsCount = emqx_stats:statsfun('connections.count'),
  34. SetConnsCount(1),
  35. ?assertEqual(1, emqx_stats:getstat('connections.count')),
  36. emqx_stats:setstat('connections.count', 2),
  37. ?assertEqual(2, emqx_stats:getstat('connections.count')),
  38. emqx_stats:setstat('connections.count', 'connections.max', 3),
  39. timer:sleep(100),
  40. ?assertEqual(3, emqx_stats:getstat('connections.count')),
  41. ?assertEqual(3, emqx_stats:getstat('connections.max')),
  42. emqx_stats:setstat('connections.count', 'connections.max', 2),
  43. timer:sleep(100),
  44. ?assertEqual(2, emqx_stats:getstat('connections.count')),
  45. ?assertEqual(3, emqx_stats:getstat('connections.max')),
  46. SetConns = emqx_stats:statsfun('connections.count', 'connections.max'),
  47. SetConns(4),
  48. timer:sleep(100),
  49. ?assertEqual(4, emqx_stats:getstat('connections.count')),
  50. ?assertEqual(4, emqx_stats:getstat('connections.max')),
  51. Conns = emqx_stats:getstats(),
  52. ?assertEqual(4, proplists:get_value('connections.count', Conns)),
  53. ?assertEqual(4, proplists:get_value('connections.max', Conns))
  54. end).
  55. t_update_interval(_) ->
  56. TickMs = 200,
  57. with_proc(
  58. fun() ->
  59. %% sleep for 2.5 ticks
  60. SleepMs = TickMs * 2 + TickMs div 2,
  61. emqx_stats:cancel_update(cm_stats),
  62. UpdFun = fun() -> emqx_stats:setstat('connections.count', 1) end,
  63. ok = emqx_stats:update_interval(stats_test, UpdFun),
  64. timer:sleep(SleepMs),
  65. ok = emqx_stats:update_interval(stats_test, UpdFun),
  66. timer:sleep(SleepMs),
  67. ?assertEqual(1, emqx_stats:getstat('connections.count'))
  68. end,
  69. TickMs
  70. ).
  71. t_helper(_) ->
  72. TickMs = 200,
  73. TestF =
  74. fun(CbModule, CbFun) ->
  75. %% sleep for 1.5 ticks
  76. SleepMs = TickMs + TickMs div 2,
  77. Ref = make_ref(),
  78. Tester = self(),
  79. UpdFun =
  80. fun() ->
  81. CbModule:CbFun(),
  82. Tester ! Ref,
  83. ok
  84. end,
  85. ok = emqx_stats:update_interval(stats_test, UpdFun),
  86. timer:sleep(SleepMs),
  87. receive
  88. Ref -> ok
  89. after 2000 -> error(timeout)
  90. end
  91. end,
  92. MkTestFun =
  93. fun(CbModule, CbFun) ->
  94. fun() ->
  95. with_proc(fun() -> TestF(CbModule, CbFun) end, TickMs)
  96. end
  97. end,
  98. [
  99. {"emqx_broker_helper", MkTestFun(emqx_broker_helper, stats_fun)},
  100. {"emqx_router_helper", MkTestFun(emqx_router_helper, stats_fun)},
  101. {"emqx_cm", MkTestFun(emqx_cm, stats_fun)},
  102. {"emqx_retainer", MkTestFun(emqx_retainer, stats_fun)}
  103. ].
  104. with_proc(F) ->
  105. {ok, _Pid} = emqx_stats:start_link(),
  106. with_stop(F).
  107. with_proc(F, TickMs) ->
  108. {ok, _Pid} = emqx_stats:start_link(#{tick_ms => TickMs}),
  109. with_stop(F).
  110. with_stop(F) ->
  111. try
  112. %% make a synced call to the gen_server so we know it has
  113. %% started running, hence it is safe to continue with less risk of race condition
  114. ?assertEqual(ignored, gen_server:call(emqx_stats, ignored)),
  115. F()
  116. after
  117. ok = emqx_stats:stop()
  118. end.