syn_groups_SUITE.erl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2019 Roberto Ostinelli <roberto@ostinelli.net> and Neato Robotics, Inc.
  7. %%
  8. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  9. %% of this software and associated documentation files (the "Software"), to deal
  10. %% in the Software without restriction, including without limitation the rights
  11. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. %% copies of the Software, and to permit persons to whom the Software is
  13. %% furnished to do so, subject to the following conditions:
  14. %%
  15. %% The above copyright notice and this permission notice shall be included in
  16. %% all copies or substantial portions of the Software.
  17. %%
  18. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. %% THE SOFTWARE.
  25. %% ==========================================================================================================
  26. -module(syn_groups_SUITE).
  27. %% callbacks
  28. -export([all/0]).
  29. -export([init_per_suite/1, end_per_suite/1]).
  30. -export([groups/0, init_per_group/2, end_per_group/2]).
  31. -export([init_per_testcase/2, end_per_testcase/2]).
  32. %% tests
  33. -export([
  34. single_node_join_and_monitor/1,
  35. single_node_join_and_leave/1,
  36. single_node_join_errors/1
  37. ]).
  38. %% include
  39. -include_lib("common_test/include/ct.hrl").
  40. %% ===================================================================
  41. %% Callbacks
  42. %% ===================================================================
  43. %% -------------------------------------------------------------------
  44. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  45. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  46. %% GroupName = atom()
  47. %% TestCase = atom()
  48. %% Reason = any()
  49. %% -------------------------------------------------------------------
  50. all() ->
  51. [
  52. {group, single_node_groups}
  53. ].
  54. %% -------------------------------------------------------------------
  55. %% Function: groups() -> [Group]
  56. %% Group = {GroupName,Properties,GroupsAndTestCases}
  57. %% GroupName = atom()
  58. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  59. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  60. %% TestCase = atom()
  61. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  62. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  63. %% repeat_until_any_ok | repeat_until_any_fail
  64. %% N = integer() | forever
  65. %% -------------------------------------------------------------------
  66. groups() ->
  67. [
  68. {single_node_groups, [shuffle], [
  69. single_node_join_and_monitor,
  70. single_node_join_and_leave,
  71. single_node_join_errors
  72. ]}
  73. ].
  74. %% -------------------------------------------------------------------
  75. %% Function: init_per_suite(Config0) ->
  76. %% Config1 | {skip,Reason} |
  77. %% {skip_and_save,Reason,Config1}
  78. %% Config0 = Config1 = [tuple()]
  79. %% Reason = any()
  80. %% -------------------------------------------------------------------
  81. init_per_suite(Config) ->
  82. Config.
  83. %% -------------------------------------------------------------------
  84. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  85. %% Config0 = Config1 = [tuple()]
  86. %% -------------------------------------------------------------------
  87. end_per_suite(_Config) ->
  88. ok.
  89. %% -------------------------------------------------------------------
  90. %% Function: init_per_group(GroupName, Config0) ->
  91. %% Config1 | {skip,Reason} |
  92. %% {skip_and_save,Reason,Config1}
  93. %% GroupName = atom()
  94. %% Config0 = Config1 = [tuple()]
  95. %% Reason = any()
  96. %% -------------------------------------------------------------------
  97. init_per_group(two_nodes_process_registration, Config) ->
  98. %% start slave
  99. {ok, SlaveNode} = syn_test_suite_helper:start_slave(syn_slave),
  100. %% config
  101. [{slave_node, SlaveNode} | Config];
  102. init_per_group(three_nodes_process_registration, Config) ->
  103. %% start slave
  104. {ok, SlaveNode1} = syn_test_suite_helper:start_slave(syn_slave_1),
  105. {ok, SlaveNode2} = syn_test_suite_helper:start_slave(syn_slave_2),
  106. %% config
  107. [{slave_node_1, SlaveNode1}, {slave_node_2, SlaveNode2} | Config];
  108. init_per_group(_GroupName, Config) ->
  109. Config.
  110. %% -------------------------------------------------------------------
  111. %% Function: end_per_group(GroupName, Config0) ->
  112. %% void() | {save_config,Config1}
  113. %% GroupName = atom()
  114. %% Config0 = Config1 = [tuple()]
  115. %% -------------------------------------------------------------------
  116. end_per_group(two_nodes_process_registration, Config) ->
  117. SlaveNode = proplists:get_value(slave_node, Config),
  118. syn_test_suite_helper:connect_node(SlaveNode),
  119. syn_test_suite_helper:stop_slave(syn_slave),
  120. timer:sleep(1000);
  121. end_per_group(three_nodes_process_registration, Config) ->
  122. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  123. syn_test_suite_helper:connect_node(SlaveNode1),
  124. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  125. syn_test_suite_helper:connect_node(SlaveNode2),
  126. syn_test_suite_helper:stop_slave(syn_slave_1),
  127. syn_test_suite_helper:stop_slave(syn_slave_2),
  128. timer:sleep(1000);
  129. end_per_group(_GroupName, _Config) ->
  130. ok.
  131. %% -------------------------------------------------------------------
  132. %% Function: init_per_testcase(TestCase, Config0) ->
  133. %% Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  134. %% TestCase = atom()
  135. %% Config0 = Config1 = [tuple()]
  136. %% Reason = any()
  137. %% -------------------------------------------------------------------
  138. init_per_testcase(_TestCase, Config) ->
  139. Config.
  140. %% -------------------------------------------------------------------
  141. %% Function: end_per_testcase(TestCase, Config0) ->
  142. %% void() | {save_config,Config1} | {fail,Reason}
  143. %% TestCase = atom()
  144. %% Config0 = Config1 = [tuple()]
  145. %% Reason = any()
  146. %% -------------------------------------------------------------------
  147. end_per_testcase(_, _Config) ->
  148. syn_test_suite_helper:clean_after_test().
  149. %% ===================================================================
  150. %% Tests
  151. %% ===================================================================
  152. single_node_join_and_monitor(_Config) ->
  153. GroupName = "my group",
  154. %% start
  155. ok = syn:start(),
  156. %% start processes
  157. Pid = syn_test_suite_helper:start_process(),
  158. PidWithMeta = syn_test_suite_helper:start_process(),
  159. PidOther = syn_test_suite_helper:start_process(),
  160. %% retrieve
  161. [] = syn:get_members(GroupName),
  162. [] = syn:get_members(GroupName, with_meta),
  163. false = syn:member(Pid, GroupName),
  164. false = syn:member(PidWithMeta, GroupName),
  165. false = syn:member(PidOther, GroupName),
  166. %% join
  167. ok = syn:join(GroupName, Pid),
  168. ok = syn:join(GroupName, PidWithMeta, {with, meta}),
  169. ok = syn:join("other-group", PidOther),
  170. %% retrieve
  171. true = syn:member(Pid, GroupName),
  172. true = syn:member(PidWithMeta, GroupName),
  173. false = syn:member(PidOther, GroupName),
  174. true = lists:sort([Pid, PidWithMeta]) =:= lists:sort(syn:get_members(GroupName)),
  175. true = lists:sort([{Pid, undefined}, {PidWithMeta, {with, meta}}]) =:= lists:sort(syn:get_members(GroupName, with_meta)),
  176. %% re-join
  177. ok = syn:join(GroupName, PidWithMeta, {with2, meta2}),
  178. true = lists:sort([{Pid, undefined}, {PidWithMeta, {with2, meta2}}]) =:= lists:sort(syn:get_members(GroupName, with_meta)),
  179. %% kill process
  180. syn_test_suite_helper:kill_process(Pid),
  181. syn_test_suite_helper:kill_process(PidWithMeta),
  182. syn_test_suite_helper:kill_process(PidOther),
  183. timer:sleep(100),
  184. %% retrieve
  185. [] = syn:get_members(GroupName),
  186. [] = syn:get_members(GroupName, with_meta),
  187. false = syn:member(Pid, GroupName),
  188. false = syn:member(PidWithMeta, GroupName).
  189. single_node_join_and_leave(_Config) ->
  190. GroupName = "my group",
  191. %% start
  192. ok = syn:start(),
  193. %% start processes
  194. Pid = syn_test_suite_helper:start_process(),
  195. PidWithMeta = syn_test_suite_helper:start_process(),
  196. %% retrieve
  197. [] = syn:get_members(GroupName),
  198. [] = syn:get_members(GroupName, with_meta),
  199. false = syn:member(Pid, GroupName),
  200. false = syn:member(PidWithMeta, GroupName),
  201. %% join
  202. ok = syn:join(GroupName, Pid),
  203. ok = syn:join(GroupName, PidWithMeta, {with, meta}),
  204. %% retrieve
  205. true = syn:member(Pid, GroupName),
  206. true = syn:member(PidWithMeta, GroupName),
  207. true = lists:sort([Pid, PidWithMeta]) =:= lists:sort(syn:get_members(GroupName)),
  208. true = lists:sort([{Pid, undefined}, {PidWithMeta, {with, meta}}]) =:= lists:sort(syn:get_members(GroupName, with_meta)),
  209. %% leave
  210. ok = syn:leave(GroupName, Pid),
  211. ok = syn:leave(GroupName, PidWithMeta),
  212. timer:sleep(100),
  213. %% retrieve
  214. [] = syn:get_members(GroupName),
  215. [] = syn:get_members(GroupName, with_meta),
  216. false = syn:member(Pid, GroupName),
  217. false = syn:member(PidWithMeta, GroupName),
  218. %% kill processes
  219. syn_test_suite_helper:kill_process(Pid),
  220. syn_test_suite_helper:kill_process(PidWithMeta).
  221. single_node_join_errors(_Config) ->
  222. GroupName = "my group",
  223. %% start
  224. ok = syn:start(),
  225. %% start processes
  226. Pid = syn_test_suite_helper:start_process(),
  227. Pid2 = syn_test_suite_helper:start_process(),
  228. %% join
  229. ok = syn:join(GroupName, Pid),
  230. ok = syn:join(GroupName, Pid2),
  231. true = syn:member(Pid, GroupName),
  232. true = syn:member(Pid2, GroupName),
  233. %% leave
  234. ok = syn:leave(GroupName, Pid),
  235. {error, not_in_group} = syn:leave(GroupName, Pid),
  236. %% kill
  237. syn_test_suite_helper:kill_process(Pid2),
  238. timer:sleep(200),
  239. {error, not_in_group} = syn:leave(GroupName, Pid2),
  240. {error, not_alive} = syn:join(GroupName, Pid2),
  241. %% kill processes
  242. syn_test_suite_helper:kill_process(Pid).