emqx_delayed_SUITE.erl 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2022 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_delayed_SUITE).
  17. -import(emqx_delayed, [on_message_publish/1]).
  18. -compile(export_all).
  19. -compile(nowarn_export_all).
  20. -record(delayed_message, {key, delayed, 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_common_test_helpers:all(?MODULE).
  29. init_per_suite(Config) ->
  30. mria:start(),
  31. ok = emqx_delayed:mnesia(boot),
  32. emqx_common_test_helpers:start_apps([emqx_modules]),
  33. Config.
  34. end_per_suite(_) ->
  35. emqx_common_test_helpers:stop_apps([emqx_modules]).
  36. %%--------------------------------------------------------------------
  37. %% Test cases
  38. %%--------------------------------------------------------------------
  39. t_load_case(_) ->
  40. Hooks = emqx_hooks:lookup('message.publish'),
  41. MFA = {emqx_delayed,on_message_publish,[]},
  42. ?assertEqual(false, lists:keyfind(MFA, 2, Hooks)),
  43. ok = emqx_delayed:enable(),
  44. Hooks1 = emqx_hooks:lookup('message.publish'),
  45. ?assertNotEqual(false, lists:keyfind(MFA, 2, Hooks1)),
  46. ok.
  47. t_delayed_message(_) ->
  48. ok = emqx_delayed:enable(),
  49. DelayedMsg = emqx_message:make(?MODULE, 1, <<"$delayed/1/publish">>, <<"delayed_m">>),
  50. ?assertEqual({stop, DelayedMsg#message{topic = <<"publish">>, headers = #{allow_publish => false}}}, on_message_publish(DelayedMsg)),
  51. Msg = emqx_message:make(?MODULE, 1, <<"no_delayed_msg">>, <<"no_delayed">>),
  52. ?assertEqual({ok, Msg}, on_message_publish(Msg)),
  53. [Key] = mnesia:dirty_all_keys(emqx_delayed),
  54. [#delayed_message{msg = #message{payload = Payload}}] = mnesia:dirty_read({emqx_delayed, Key}),
  55. ?assertEqual(<<"delayed_m">>, Payload),
  56. timer:sleep(5000),
  57. EmptyKey = mnesia:dirty_all_keys(emqx_delayed),
  58. ?assertEqual([], EmptyKey),
  59. ok = emqx_delayed:disable().