emqx_vm_SUITE.erl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2017-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_vm_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. all() ->
  21. emqx_common_test_helpers:all(?MODULE).
  22. t_load(_Config) ->
  23. lists:foreach(
  24. fun({Avg, LoadKey, Int}) ->
  25. emqx_common_test_helpers:with_mock(
  26. cpu_sup,
  27. Avg,
  28. fun() -> Int end,
  29. fun() ->
  30. Load = proplists:get_value(LoadKey, emqx_vm:loads()),
  31. ?assertEqual(Int / 256, Load)
  32. end
  33. )
  34. end,
  35. [{avg1, load1, 0}, {avg5, load5, 128}, {avg15, load15, 256}]
  36. ),
  37. ?assertMatch(
  38. [{load1, _}, {load5, _}, {load15, _}],
  39. emqx_vm:loads()
  40. ).
  41. t_systeminfo(_Config) ->
  42. ?assertEqual(
  43. emqx_vm:system_info_keys(),
  44. [Key || {Key, _} <- emqx_vm:get_system_info()]
  45. ),
  46. ?assertEqual(undefined, emqx_vm:get_system_info(undefined)).
  47. t_process_info(_Config) ->
  48. ProcessInfo = emqx_vm:get_process_info(),
  49. ?assertEqual(emqx_vm:process_info_keys(), [K || {K, _V} <- ProcessInfo]).
  50. t_process_gc(_Config) ->
  51. GcInfo = emqx_vm:get_process_gc_info(),
  52. ?assertEqual(emqx_vm:process_gc_info_keys(), [K || {K, _V} <- GcInfo]).
  53. t_get_ets_list(_Config) ->
  54. ets:new(test, [named_table]),
  55. Ets = emqx_vm:get_ets_list(),
  56. true = lists:member(test, Ets).
  57. t_get_ets_info(_Config) ->
  58. ets:new(test, [named_table]),
  59. [] = emqx_vm:get_ets_info(test1),
  60. EtsInfo = emqx_vm:get_ets_info(test),
  61. test = proplists:get_value(name, EtsInfo),
  62. Tid = proplists:get_value(id, EtsInfo),
  63. EtsInfos = emqx_vm:get_ets_info(),
  64. ?assertEqual(
  65. true,
  66. lists:foldl(
  67. fun(Info, Acc) ->
  68. case proplists:get_value(id, Info) of
  69. Tid -> true;
  70. _ -> Acc
  71. end
  72. end,
  73. false,
  74. EtsInfos
  75. )
  76. ).
  77. t_scheduler_usage(_Config) ->
  78. emqx_vm:scheduler_usage(5000).
  79. t_get_memory(_Config) ->
  80. emqx_vm:get_memory().
  81. t_schedulers(_Config) ->
  82. emqx_vm:schedulers().
  83. t_get_process_limit(_Config) ->
  84. emqx_vm:get_process_limit().
  85. t_cpu_util(_Config) ->
  86. ?assertMatch(Val when is_number(Val), emqx_vm:cpu_util()).
  87. easy_server() ->
  88. {ok, LSock} = gen_tcp:listen(5678, [binary, {packet, 0}, {active, false}]),
  89. {ok, Sock} = gen_tcp:accept(LSock),
  90. ok = do_recv(Sock),
  91. ok = gen_tcp:close(Sock),
  92. ok = gen_tcp:close(LSock).
  93. do_recv(Sock) ->
  94. case gen_tcp:recv(Sock, 0) of
  95. {ok, _} ->
  96. do_recv(Sock);
  97. {error, closed} ->
  98. ok
  99. end.