syn_groups_SUITE.erl 16 KB

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