emqx_SUITE.erl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("emqx/include/emqx.hrl").
  20. -include_lib("eunit/include/eunit.hrl").
  21. -include_lib("common_test/include/ct.hrl").
  22. all() -> emqx_common_test_helpers:all(?MODULE).
  23. init_per_suite(Config) ->
  24. Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
  25. [{apps, Apps} | Config].
  26. end_per_suite(Config) ->
  27. emqx_cth_suite:stop(?config(apps, Config)).
  28. t_emqx_pubsub_api(_) ->
  29. true = emqx:is_running(node()),
  30. {ok, C} = emqtt:start_link([{host, "localhost"}, {clientid, "myclient"}]),
  31. {ok, _} = emqtt:connect(C),
  32. ClientId = <<"myclient">>,
  33. Payload = <<"Hello World">>,
  34. Topic = <<"mytopic">>,
  35. Topic1 = <<"mytopic1">>,
  36. Topic2 = <<"mytopic2">>,
  37. Topic3 = <<"mytopic3">>,
  38. emqx:subscribe(Topic, ClientId),
  39. emqx:subscribe(Topic1, ClientId, #{qos => 1}),
  40. emqx:subscribe(Topic2, ClientId, #{qos => 2}),
  41. ct:sleep(100),
  42. ?assertEqual([Topic2, Topic1, Topic], emqx:topics()),
  43. ?assertEqual([self()], emqx:subscribers(Topic)),
  44. ?assertEqual([self()], emqx:subscribers(Topic1)),
  45. ?assertEqual([self()], emqx:subscribers(Topic2)),
  46. ?assertEqual(
  47. [
  48. {Topic, #{nl => 0, qos => 0, rap => 0, rh => 0, subid => ClientId}},
  49. {Topic1, #{nl => 0, qos => 1, rap => 0, rh => 0, subid => ClientId}},
  50. {Topic2, #{nl => 0, qos => 2, rap => 0, rh => 0, subid => ClientId}}
  51. ],
  52. emqx:subscriptions(self())
  53. ),
  54. ?assertEqual(true, emqx:subscribed(self(), Topic)),
  55. ?assertEqual(true, emqx:subscribed(ClientId, Topic)),
  56. ?assertEqual(true, emqx:subscribed(self(), Topic1)),
  57. ?assertEqual(true, emqx:subscribed(ClientId, Topic1)),
  58. ?assertEqual(true, emqx:subscribed(self(), Topic2)),
  59. ?assertEqual(true, emqx:subscribed(ClientId, Topic2)),
  60. ?assertEqual(false, emqx:subscribed(self(), Topic3)),
  61. ?assertEqual(false, emqx:subscribed(ClientId, Topic3)),
  62. emqx:publish(emqx_message:make(Topic, Payload)),
  63. receive
  64. {deliver, Topic, #message{payload = Payload}} ->
  65. ok
  66. after 100 ->
  67. ct:fail("no_message")
  68. end,
  69. emqx:publish(emqx_message:make(Topic1, Payload)),
  70. receive
  71. {deliver, Topic1, #message{payload = Payload}} ->
  72. ok
  73. after 100 ->
  74. ct:fail("no_message")
  75. end,
  76. emqx:publish(emqx_message:make(Topic2, Payload)),
  77. receive
  78. {deliver, Topic2, #message{payload = Payload}} ->
  79. ok
  80. after 100 ->
  81. ct:fail("no_message")
  82. end,
  83. emqx:unsubscribe(Topic),
  84. emqx:unsubscribe(Topic1),
  85. emqx:unsubscribe(Topic2),
  86. ct:sleep(20),
  87. ?assertEqual([], emqx:topics()).
  88. t_cluster_nodes(_) ->
  89. Expected = [node()],
  90. ?assertEqual(Expected, emqx:running_nodes()),
  91. ?assertEqual(Expected, emqx:cluster_nodes(running)),
  92. ?assertEqual(Expected, emqx:cluster_nodes(all)),
  93. ?assertEqual(Expected, emqx:cluster_nodes(cores)),
  94. ?assertEqual([], emqx:cluster_nodes(stopped)).
  95. t_get_config(_) ->
  96. ?assertEqual(false, emqx:get_config([overload_protection, enable])),
  97. ?assertEqual(false, emqx:get_config(["overload_protection", <<"enable">>])).
  98. t_get_config_default_1(_) ->
  99. ?assertEqual(false, emqx:get_config([overload_protection, enable], undefined)),
  100. ?assertEqual(false, emqx:get_config(["overload_protection", <<"enable">>], undefined)).
  101. t_get_config_default_2(_) ->
  102. AtomPathRes = emqx:get_config([overload_protection, <<"_!no_@exist_">>], undefined),
  103. NonAtomPathRes = emqx:get_config(["doesnotexist", <<"db_backend">>], undefined),
  104. ?assertEqual(undefined, NonAtomPathRes),
  105. ?assertEqual(undefined, AtomPathRes).