emqx_mod_delayed_SUITE.erl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2021 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_mod_delayed_SUITE).
  17. -import(emqx_mod_delayed, [on_message_publish/1]).
  18. -compile(export_all).
  19. -compile(nowarn_export_all).
  20. -record(delayed_message, {key, msg}).
  21. -include_lib("common_test/include/ct.hrl").
  22. -include_lib("eunit/include/eunit.hrl").
  23. -include_lib("emqx/include/emqx.hrl").
  24. %%--------------------------------------------------------------------
  25. %% Setups
  26. %%--------------------------------------------------------------------
  27. all() ->
  28. emqx_ct:all(?MODULE).
  29. init_per_suite(Config) ->
  30. emqx_ct_helpers:start_apps([emqx_modules], fun set_special_configs/1),
  31. Config.
  32. end_per_suite(_) ->
  33. emqx_ct_helpers:stop_apps([emqx_modules]).
  34. set_special_configs(emqx) ->
  35. application:set_env(emqx, modules, [{emqx_mod_delayed, []}]),
  36. application:set_env(emqx, allow_anonymous, false),
  37. application:set_env(emqx, enable_acl_cache, false);
  38. set_special_configs(_App) ->
  39. ok.
  40. %%--------------------------------------------------------------------
  41. %% Test cases
  42. %%--------------------------------------------------------------------
  43. t_load_case(_) ->
  44. UnHooks = emqx_hooks:lookup('message.publish'),
  45. ?assertEqual([], UnHooks),
  46. ok = emqx_mod_delayed:load([]),
  47. Hooks = emqx_hooks:lookup('message.publish'),
  48. ?assertEqual(1, length(Hooks)),
  49. ok.
  50. t_delayed_message(_) ->
  51. ok = emqx_mod_delayed:load([]),
  52. DelayedMsg = emqx_message:make(?MODULE, 1, <<"$delayed/1/publish">>, <<"delayed_m">>),
  53. ?assertEqual({stop, DelayedMsg#message{topic = <<"publish">>, headers = #{allow_publish => false}}}, on_message_publish(DelayedMsg)),
  54. Msg = emqx_message:make(?MODULE, 1, <<"no_delayed_msg">>, <<"no_delayed">>),
  55. ?assertEqual({ok, Msg}, on_message_publish(Msg)),
  56. [Key] = mnesia:dirty_all_keys(emqx_mod_delayed),
  57. [#delayed_message{msg = #message{payload = Payload}}] = mnesia:dirty_read({emqx_mod_delayed, Key}),
  58. ?assertEqual(<<"delayed_m">>, Payload),
  59. timer:sleep(5000),
  60. EmptyKey = mnesia:dirty_all_keys(emqx_mod_delayed),
  61. ?assertEqual([], EmptyKey),
  62. ok = emqx_mod_delayed:unload([]).