syn_groups_SUITE.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. -export([
  39. two_nodes_join_monitor_and_unregister/1
  40. ]).
  41. %% include
  42. -include_lib("common_test/include/ct.hrl").
  43. %% ===================================================================
  44. %% Callbacks
  45. %% ===================================================================
  46. %% -------------------------------------------------------------------
  47. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  48. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  49. %% GroupName = atom()
  50. %% TestCase = atom()
  51. %% Reason = any()
  52. %% -------------------------------------------------------------------
  53. all() ->
  54. [
  55. {group, single_node_groups},
  56. {group, two_nodes_groups}
  57. ].
  58. %% -------------------------------------------------------------------
  59. %% Function: groups() -> [Group]
  60. %% Group = {GroupName,Properties,GroupsAndTestCases}
  61. %% GroupName = atom()
  62. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  63. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  64. %% TestCase = atom()
  65. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  66. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  67. %% repeat_until_any_ok | repeat_until_any_fail
  68. %% N = integer() | forever
  69. %% -------------------------------------------------------------------
  70. groups() ->
  71. [
  72. {single_node_groups, [shuffle], [
  73. single_node_join_and_monitor,
  74. single_node_join_and_leave,
  75. single_node_join_errors
  76. ]},
  77. {two_nodes_groups, [shuffle], [
  78. two_nodes_join_monitor_and_unregister
  79. %% two_nodes_local_members
  80. ]}
  81. ].
  82. %% -------------------------------------------------------------------
  83. %% Function: init_per_suite(Config0) ->
  84. %% Config1 | {skip,Reason} |
  85. %% {skip_and_save,Reason,Config1}
  86. %% Config0 = Config1 = [tuple()]
  87. %% Reason = any()
  88. %% -------------------------------------------------------------------
  89. init_per_suite(Config) ->
  90. Config.
  91. %% -------------------------------------------------------------------
  92. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  93. %% Config0 = Config1 = [tuple()]
  94. %% -------------------------------------------------------------------
  95. end_per_suite(_Config) ->
  96. ok.
  97. %% -------------------------------------------------------------------
  98. %% Function: init_per_group(GroupName, Config0) ->
  99. %% Config1 | {skip,Reason} |
  100. %% {skip_and_save,Reason,Config1}
  101. %% GroupName = atom()
  102. %% Config0 = Config1 = [tuple()]
  103. %% Reason = any()
  104. %% -------------------------------------------------------------------
  105. init_per_group(two_nodes_groups, Config) ->
  106. %% start slave
  107. {ok, SlaveNode} = syn_test_suite_helper:start_slave(syn_slave),
  108. %% config
  109. [{slave_node, SlaveNode} | Config];
  110. init_per_group(three_nodes_groups, Config) ->
  111. %% start slave
  112. {ok, SlaveNode1} = syn_test_suite_helper:start_slave(syn_slave_1),
  113. {ok, SlaveNode2} = syn_test_suite_helper:start_slave(syn_slave_2),
  114. %% config
  115. [{slave_node_1, SlaveNode1}, {slave_node_2, SlaveNode2} | Config];
  116. init_per_group(_GroupName, Config) ->
  117. Config.
  118. %% -------------------------------------------------------------------
  119. %% Function: end_per_group(GroupName, Config0) ->
  120. %% void() | {save_config,Config1}
  121. %% GroupName = atom()
  122. %% Config0 = Config1 = [tuple()]
  123. %% -------------------------------------------------------------------
  124. end_per_group(two_nodes_groups, Config) ->
  125. SlaveNode = proplists:get_value(slave_node, Config),
  126. syn_test_suite_helper:connect_node(SlaveNode),
  127. syn_test_suite_helper:stop_slave(syn_slave),
  128. timer:sleep(1000);
  129. end_per_group(three_nodes_groups, Config) ->
  130. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  131. syn_test_suite_helper:connect_node(SlaveNode1),
  132. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  133. syn_test_suite_helper:connect_node(SlaveNode2),
  134. syn_test_suite_helper:stop_slave(syn_slave_1),
  135. syn_test_suite_helper:stop_slave(syn_slave_2),
  136. timer:sleep(1000);
  137. end_per_group(_GroupName, _Config) ->
  138. ok.
  139. %% -------------------------------------------------------------------
  140. %% Function: init_per_testcase(TestCase, Config0) ->
  141. %% Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  142. %% TestCase = atom()
  143. %% Config0 = Config1 = [tuple()]
  144. %% Reason = any()
  145. %% -------------------------------------------------------------------
  146. init_per_testcase(_TestCase, Config) ->
  147. Config.
  148. %% -------------------------------------------------------------------
  149. %% Function: end_per_testcase(TestCase, Config0) ->
  150. %% void() | {save_config,Config1} | {fail,Reason}
  151. %% TestCase = atom()
  152. %% Config0 = Config1 = [tuple()]
  153. %% Reason = any()
  154. %% -------------------------------------------------------------------
  155. end_per_testcase(_, _Config) ->
  156. syn_test_suite_helper:clean_after_test().
  157. %% ===================================================================
  158. %% Tests
  159. %% ===================================================================
  160. single_node_join_and_monitor(_Config) ->
  161. GroupName = "my group",
  162. %% start
  163. ok = syn:start(),
  164. %% start processes
  165. Pid = syn_test_suite_helper:start_process(),
  166. PidWithMeta = syn_test_suite_helper:start_process(),
  167. PidOther = syn_test_suite_helper:start_process(),
  168. %% retrieve
  169. [] = syn:get_members(GroupName),
  170. [] = syn:get_members(GroupName, with_meta),
  171. false = syn:member(Pid, GroupName),
  172. false = syn:member(PidWithMeta, GroupName),
  173. false = syn:member(PidOther, GroupName),
  174. %% join
  175. ok = syn:join(GroupName, Pid),
  176. ok = syn:join(GroupName, PidWithMeta, {with, meta}),
  177. ok = syn:join("other-group", PidOther),
  178. %% retrieve
  179. true = syn:member(Pid, GroupName),
  180. true = syn:member(PidWithMeta, GroupName),
  181. false = syn:member(PidOther, GroupName),
  182. true = lists:sort([Pid, PidWithMeta]) =:= lists:sort(syn:get_members(GroupName)),
  183. true = lists:sort([{Pid, undefined}, {PidWithMeta, {with, meta}}]) =:= lists:sort(syn:get_members(GroupName, with_meta)),
  184. %% re-join
  185. ok = syn:join(GroupName, PidWithMeta, {with2, meta2}),
  186. true = lists:sort([{Pid, undefined}, {PidWithMeta, {with2, meta2}}]) =:= lists:sort(syn:get_members(GroupName, with_meta)),
  187. %% kill process
  188. syn_test_suite_helper:kill_process(Pid),
  189. syn_test_suite_helper:kill_process(PidWithMeta),
  190. syn_test_suite_helper:kill_process(PidOther),
  191. timer:sleep(100),
  192. %% retrieve
  193. [] = syn:get_members(GroupName),
  194. [] = syn:get_members(GroupName, with_meta),
  195. false = syn:member(Pid, GroupName),
  196. false = syn:member(PidWithMeta, GroupName).
  197. single_node_join_and_leave(_Config) ->
  198. GroupName = "my group",
  199. %% start
  200. ok = syn:start(),
  201. %% start processes
  202. Pid = syn_test_suite_helper:start_process(),
  203. PidWithMeta = syn_test_suite_helper:start_process(),
  204. %% retrieve
  205. [] = syn:get_members(GroupName),
  206. [] = syn:get_members(GroupName, with_meta),
  207. false = syn:member(Pid, GroupName),
  208. false = syn:member(PidWithMeta, GroupName),
  209. %% join
  210. ok = syn:join(GroupName, Pid),
  211. ok = syn:join(GroupName, PidWithMeta, {with, meta}),
  212. %% retrieve
  213. true = syn:member(Pid, GroupName),
  214. true = syn:member(PidWithMeta, GroupName),
  215. true = lists:sort([Pid, PidWithMeta]) =:= lists:sort(syn:get_members(GroupName)),
  216. true = lists:sort([{Pid, undefined}, {PidWithMeta, {with, meta}}]) =:= lists:sort(syn:get_members(GroupName, with_meta)),
  217. %% leave
  218. ok = syn:leave(GroupName, Pid),
  219. ok = syn:leave(GroupName, PidWithMeta),
  220. timer:sleep(100),
  221. %% retrieve
  222. [] = syn:get_members(GroupName),
  223. [] = syn:get_members(GroupName, with_meta),
  224. false = syn:member(Pid, GroupName),
  225. false = syn:member(PidWithMeta, GroupName),
  226. %% kill processes
  227. syn_test_suite_helper:kill_process(Pid),
  228. syn_test_suite_helper:kill_process(PidWithMeta).
  229. single_node_join_errors(_Config) ->
  230. GroupName = "my group",
  231. %% start
  232. ok = syn:start(),
  233. %% start processes
  234. Pid = syn_test_suite_helper:start_process(),
  235. Pid2 = syn_test_suite_helper:start_process(),
  236. %% join
  237. ok = syn:join(GroupName, Pid),
  238. ok = syn:join(GroupName, Pid2),
  239. true = syn:member(Pid, GroupName),
  240. true = syn:member(Pid2, GroupName),
  241. %% leave
  242. ok = syn:leave(GroupName, Pid),
  243. {error, not_in_group} = syn:leave(GroupName, Pid),
  244. %% kill
  245. syn_test_suite_helper:kill_process(Pid2),
  246. timer:sleep(200),
  247. {error, not_in_group} = syn:leave(GroupName, Pid2),
  248. {error, not_alive} = syn:join(GroupName, Pid2),
  249. %% kill processes
  250. syn_test_suite_helper:kill_process(Pid).
  251. two_nodes_join_monitor_and_unregister(Config) ->
  252. GroupName = "my group",
  253. %% get slave
  254. SlaveNode = proplists:get_value(slave_node, Config),
  255. %% start
  256. ok = syn:start(),
  257. ok = rpc:call(SlaveNode, syn, start, []),
  258. timer:sleep(100),
  259. %% start processes
  260. LocalPid = syn_test_suite_helper:start_process(),
  261. RemotePid = syn_test_suite_helper:start_process(SlaveNode),
  262. RemotePidJoinRemote = syn_test_suite_helper:start_process(SlaveNode),
  263. OtherPid = syn_test_suite_helper:start_process(),
  264. %% retrieve
  265. [] = syn:get_members("group-1"),
  266. [] = syn:get_members(GroupName, with_meta),
  267. false = syn:member(LocalPid, GroupName),
  268. false = syn:member(RemotePid, GroupName),
  269. false = syn:member(RemotePidJoinRemote, GroupName),
  270. false = syn:member(OtherPid, GroupName),
  271. %% join
  272. ok = syn:join(GroupName, LocalPid),
  273. ok = syn:join(GroupName, RemotePid),
  274. ok = rpc:call(SlaveNode, syn, join, [GroupName, RemotePidJoinRemote]),
  275. ok = syn:join("other-group", OtherPid),
  276. timer:sleep(200),
  277. %% retrieve
  278. true = syn:member(LocalPid, GroupName),
  279. true = syn:member(RemotePid, GroupName),
  280. true = syn:member(RemotePidJoinRemote, GroupName),
  281. false = syn:member(OtherPid, GroupName),
  282. true = rpc:call(SlaveNode, syn, member, [LocalPid, GroupName]),
  283. true = rpc:call(SlaveNode, syn, member, [RemotePid, GroupName]),
  284. true = rpc:call(SlaveNode, syn, member, [RemotePidJoinRemote, GroupName]),
  285. false = rpc:call(SlaveNode, syn, member, [OtherPid, GroupName]),
  286. %% leave & kill
  287. ok = rpc:call(SlaveNode, syn, leave, [GroupName, LocalPid]),
  288. ok = syn:leave(GroupName, RemotePid),
  289. syn_test_suite_helper:kill_process(RemotePidJoinRemote),
  290. syn_test_suite_helper:kill_process(OtherPid),
  291. timer:sleep(200),
  292. %% retrieve
  293. [] = syn:get_members("group-1"),
  294. [] = syn:get_members(GroupName, with_meta),
  295. false = syn:member(LocalPid, GroupName),
  296. false = syn:member(RemotePid, GroupName),
  297. false = syn:member(RemotePidJoinRemote, GroupName),
  298. false = syn:member(OtherPid, GroupName),
  299. %% kill processes
  300. syn_test_suite_helper:kill_process(LocalPid),
  301. syn_test_suite_helper:kill_process(RemotePid).