emqx_pool_SUITE.erl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_pool_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. all() ->
  21. [
  22. {group, submit},
  23. {group, async_submit},
  24. t_unexpected
  25. ].
  26. groups() ->
  27. [
  28. {submit, [sequence], [
  29. t_submit_mfa,
  30. t_submit_fa
  31. ]},
  32. {async_submit, [sequence], [
  33. t_async_submit_mfa,
  34. t_async_submit_crash
  35. ]}
  36. ].
  37. init_per_suite(Config) ->
  38. _ = application:stop(grpoc),
  39. ok = emqx_logger:set_log_level(emergency),
  40. application:ensure_all_started(gproc),
  41. Config.
  42. end_per_suite(_Config) ->
  43. ok.
  44. init_per_testcase(_, Config) ->
  45. {ok, Sup} = emqx_pool_sup:start_link(),
  46. [{pool_sup, Sup} | Config].
  47. end_per_testcase(_, Config) ->
  48. Sup = proplists:get_value(pool_sup, Config),
  49. %% ???
  50. exit(Sup, normal).
  51. t_submit_mfa(_Config) ->
  52. Result = emqx_pool:submit({?MODULE, test_mfa, []}),
  53. ?assertEqual(15, Result).
  54. t_submit_fa(_Config) ->
  55. Fun = fun(X) ->
  56. case X rem 2 of
  57. 0 -> {true, X div 2};
  58. _ -> false
  59. end
  60. end,
  61. Result = emqx_pool:submit(Fun, [2]),
  62. ?assertEqual({true, 1}, Result).
  63. t_async_submit_mfa(_Config) ->
  64. emqx_pool:async_submit({?MODULE, test_mfa, []}),
  65. emqx_pool:async_submit(fun ?MODULE:test_mfa/0, []).
  66. t_async_submit_crash(_) ->
  67. emqx_pool:async_submit(fun() -> error(unexpected_error) end).
  68. t_unexpected(_) ->
  69. Pid = emqx_pool:worker(),
  70. ?assertEqual(ignored, gen_server:call(Pid, bad_request)),
  71. ?assertEqual(ok, gen_server:cast(Pid, bad_msg)),
  72. Pid ! bad_info,
  73. ok = gen_server:stop(Pid).
  74. test_mfa() ->
  75. lists:foldl(fun(X, Sum) -> X + Sum end, 0, [1, 2, 3, 4, 5]).
  76. % t_async_submit(_) ->
  77. % error('TODO').