syn_groups_SUITE.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2016 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_leave/1,
  35. single_node_kill/1,
  36. single_node_publish/1,
  37. single_node_multi_call/1
  38. ]).
  39. -export([
  40. two_nodes_kill/1,
  41. two_nodes_publish/1,
  42. two_nodes_multi_call/1
  43. ]).
  44. %% internal
  45. -export([recipient_loop/1]).
  46. -export([called_loop/1]).
  47. %% include
  48. -include_lib("common_test/include/ct.hrl").
  49. %% ===================================================================
  50. %% Callbacks
  51. %% ===================================================================
  52. %% -------------------------------------------------------------------
  53. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  54. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  55. %% GroupName = atom()
  56. %% TestCase = atom()
  57. %% Reason = term()
  58. %% -------------------------------------------------------------------
  59. all() ->
  60. [
  61. {group, single_node_process_groups},
  62. {group, two_nodes_process_groups}
  63. ].
  64. %% -------------------------------------------------------------------
  65. %% Function: groups() -> [Group]
  66. %% Group = {GroupName,Properties,GroupsAndTestCases}
  67. %% GroupName = atom()
  68. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  69. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  70. %% TestCase = atom()
  71. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  72. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  73. %% repeat_until_any_ok | repeat_until_any_fail
  74. %% N = integer() | forever
  75. %% -------------------------------------------------------------------
  76. groups() ->
  77. [
  78. {single_node_process_groups, [shuffle], [
  79. single_node_leave,
  80. single_node_kill,
  81. single_node_publish,
  82. single_node_multi_call
  83. ]},
  84. {two_nodes_process_groups, [shuffle], [
  85. two_nodes_kill,
  86. two_nodes_publish,
  87. two_nodes_multi_call
  88. ]}
  89. ].
  90. %% -------------------------------------------------------------------
  91. %% Function: init_per_suite(Config0) ->
  92. %% Config1 | {skip,Reason} |
  93. %% {skip_and_save,Reason,Config1}
  94. %% Config0 = Config1 = [tuple()]
  95. %% Reason = term()
  96. %% -------------------------------------------------------------------
  97. init_per_suite(Config) ->
  98. %% config
  99. [
  100. {slave_node_short_name, syn_slave}
  101. | Config
  102. ].
  103. %% -------------------------------------------------------------------
  104. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  105. %% Config0 = Config1 = [tuple()]
  106. %% -------------------------------------------------------------------
  107. end_per_suite(_Config) -> ok.
  108. %% -------------------------------------------------------------------
  109. %% Function: init_per_group(GroupName, Config0) ->
  110. %% Config1 | {skip,Reason} |
  111. %% {skip_and_save,Reason,Config1}
  112. %% GroupName = atom()
  113. %% Config0 = Config1 = [tuple()]
  114. %% Reason = term()
  115. %% -------------------------------------------------------------------
  116. init_per_group(two_nodes_process_groups, Config) ->
  117. %% start slave
  118. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  119. {ok, SlaveNode} = syn_test_suite_helper:start_slave(SlaveNodeShortName),
  120. %% config
  121. [
  122. {slave_node, SlaveNode}
  123. | Config
  124. ];
  125. init_per_group(_GroupName, Config) -> Config.
  126. %% -------------------------------------------------------------------
  127. %% Function: end_per_group(GroupName, Config0) ->
  128. %% void() | {save_config,Config1}
  129. %% GroupName = atom()
  130. %% Config0 = Config1 = [tuple()]
  131. %% -------------------------------------------------------------------
  132. end_per_group(two_nodes_process_groups, Config) ->
  133. %% get slave node name
  134. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  135. %% stop slave
  136. syn_test_suite_helper:stop_slave(SlaveNodeShortName);
  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 = term()
  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 = term()
  154. % ----------------------------------------------------------------------------------------------------------
  155. end_per_testcase(_TestCase, Config) ->
  156. %% get slave
  157. SlaveNode = proplists:get_value(slave_node, Config),
  158. syn_test_suite_helper:clean_after_test(SlaveNode).
  159. %% ===================================================================
  160. %% Tests
  161. %% ===================================================================
  162. single_node_leave(_Config) ->
  163. %% set schema location
  164. application:set_env(mnesia, schema_location, ram),
  165. %% start
  166. ok = syn:start(),
  167. ok = syn:init(),
  168. %% start process
  169. Pid = syn_test_suite_helper:start_process(),
  170. %% retrieve
  171. [] = syn:get_members(<<"my group">>),
  172. false = syn:member(Pid, <<"my group">>),
  173. %% join
  174. ok = syn:join(<<"my group">>, Pid),
  175. %% retrieve
  176. [Pid] = syn:get_members(<<"my group">>),
  177. true = syn:member(Pid, <<"my group">>),
  178. %% leave
  179. ok = syn:leave(<<"my group">>, Pid),
  180. %% retrieve
  181. [] = syn:get_members(<<"my group">>),
  182. false = syn:member(Pid, <<"my group">>),
  183. %% kill process
  184. syn_test_suite_helper:kill_process(Pid).
  185. single_node_kill(_Config) ->
  186. %% set schema location
  187. application:set_env(mnesia, schema_location, ram),
  188. %% start
  189. ok = syn:start(),
  190. ok = syn:init(),
  191. %% start process
  192. Pid = syn_test_suite_helper:start_process(),
  193. %% retrieve
  194. [] = syn:get_members(<<"my group">>),
  195. false = syn:member(Pid, <<"my group">>),
  196. %% join
  197. ok = syn:join(<<"my group">>, Pid),
  198. %% retrieve
  199. [Pid] = syn:get_members(<<"my group">>),
  200. true = syn:member(Pid, <<"my group">>),
  201. %% kill process
  202. syn_test_suite_helper:kill_process(Pid),
  203. timer:sleep(100),
  204. %% retrieve
  205. [] = syn:get_members(<<"my group">>),
  206. false = syn:member(Pid, <<"my group">>).
  207. single_node_publish(_Config) ->
  208. %% set schema location
  209. application:set_env(mnesia, schema_location, ram),
  210. %% start
  211. ok = syn:start(),
  212. ok = syn:init(),
  213. %% start processes
  214. ResultPid = self(),
  215. F = fun() -> recipient_loop(ResultPid) end,
  216. Pid1 = syn_test_suite_helper:start_process(F),
  217. Pid2 = syn_test_suite_helper:start_process(F),
  218. %% join
  219. ok = syn:join(<<"my group">>, Pid1),
  220. ok = syn:join(<<"my group">>, Pid2),
  221. %% publish
  222. syn:publish(<<"my group">>, {test, message}),
  223. %% check publish was received
  224. receive
  225. {received, Pid1, {test, message}} -> ok
  226. after 2000 ->
  227. ok = published_message_was_not_received_by_pid1
  228. end,
  229. receive
  230. {received, Pid2, {test, message}} -> ok
  231. after 2000 ->
  232. ok = published_message_was_not_received_by_pid2
  233. end,
  234. %% kill processes
  235. syn_test_suite_helper:kill_process(Pid1),
  236. syn_test_suite_helper:kill_process(Pid2).
  237. single_node_multi_call(_Config) ->
  238. %% set schema location
  239. application:set_env(mnesia, schema_location, ram),
  240. %% start
  241. ok = syn:start(),
  242. ok = syn:init(),
  243. %% start processes
  244. Pid1 = syn_test_suite_helper:start_process(fun() -> called_loop(pid1) end),
  245. Pid2 = syn_test_suite_helper:start_process(fun() -> called_loop(pid2) end),
  246. PidUnresponsive = syn_test_suite_helper:start_process(),
  247. %% register
  248. ok = syn:join(<<"my group">>, Pid1),
  249. ok = syn:join(<<"my group">>, Pid2),
  250. ok = syn:join(<<"my group">>, PidUnresponsive),
  251. %% call
  252. {Replies, BadPids} = syn:multi_call(<<"my group">>, get_pid_name),
  253. %% check responses
  254. 2 = length(Replies),
  255. pid1 = proplists:get_value(Pid1, Replies),
  256. pid2 = proplists:get_value(Pid2, Replies),
  257. [PidUnresponsive] = BadPids,
  258. %% kill processes
  259. syn_test_suite_helper:kill_process(Pid1),
  260. syn_test_suite_helper:kill_process(Pid2),
  261. syn_test_suite_helper:kill_process(PidUnresponsive).
  262. two_nodes_kill(Config) ->
  263. %% get slave
  264. SlaveNode = proplists:get_value(slave_node, Config),
  265. %% set schema location
  266. application:set_env(mnesia, schema_location, ram),
  267. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  268. %% start
  269. ok = syn:start(),
  270. ok = syn:init(),
  271. ok = rpc:call(SlaveNode, syn, start, []),
  272. ok = rpc:call(SlaveNode, syn, init, []),
  273. timer:sleep(100),
  274. %% start processes
  275. PidLocal = syn_test_suite_helper:start_process(),
  276. PidSlave = syn_test_suite_helper:start_process(SlaveNode),
  277. %% retrieve
  278. [] = syn:get_members(<<"my group">>),
  279. false = syn:member(PidLocal, <<"my group">>),
  280. false = syn:member(PidSlave, <<"my group">>),
  281. [] = rpc:call(SlaveNode, syn, get_members, [<<"my group">>]),
  282. false = rpc:call(SlaveNode, syn, member, [PidLocal, <<"my group">>]),
  283. false = rpc:call(SlaveNode, syn, member, [PidSlave, <<"my group">>]),
  284. %% register
  285. ok = syn:join(<<"my group">>, PidSlave),
  286. ok = rpc:call(SlaveNode, syn, join, [<<"my group">>, PidLocal]),
  287. %% retrieve, pid should have the same order in all nodes
  288. [PidSlave, PidLocal] = syn:get_members(<<"my group">>),
  289. [PidSlave, PidLocal] = rpc:call(SlaveNode, syn, get_members, [<<"my group">>]),
  290. %% kill processes
  291. syn_test_suite_helper:kill_process(PidLocal),
  292. syn_test_suite_helper:kill_process(PidSlave),
  293. timer:sleep(100),
  294. %% retrieve
  295. [] = syn:get_members(<<"my group">>),
  296. false = syn:member(PidLocal, <<"my group">>),
  297. false = syn:member(PidSlave, <<"my group">>),
  298. [] = rpc:call(SlaveNode, syn, get_members, [<<"my group">>]),
  299. false = rpc:call(SlaveNode, syn, member, [PidLocal, <<"my group">>]),
  300. false = rpc:call(SlaveNode, syn, member, [PidSlave, <<"my group">>]).
  301. two_nodes_publish(Config) ->
  302. %% get slave
  303. SlaveNode = proplists:get_value(slave_node, Config),
  304. %% set schema location
  305. application:set_env(mnesia, schema_location, ram),
  306. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  307. %% start
  308. ok = syn:start(),
  309. ok = syn:init(),
  310. ok = rpc:call(SlaveNode, syn, start, []),
  311. ok = rpc:call(SlaveNode, syn, init, []),
  312. timer:sleep(100),
  313. %% start process
  314. ResultPid = self(),
  315. F = fun() -> recipient_loop(ResultPid) end,
  316. PidLocal = syn_test_suite_helper:start_process(F),
  317. PidSlave = syn_test_suite_helper:start_process(SlaveNode, F),
  318. %% register
  319. ok = syn:join(<<"my group">>, PidSlave),
  320. ok = rpc:call(SlaveNode, syn, join, [<<"my group">>, PidLocal]),
  321. %% publish
  322. syn:publish(<<"my group">>, {test, message}),
  323. %% check publish was received
  324. receive
  325. {received, PidLocal, {test, message}} -> ok
  326. after 2000 ->
  327. ok = published_message_was_not_received_by_pidlocal
  328. end,
  329. receive
  330. {received, PidSlave, {test, message}} -> ok
  331. after 2000 ->
  332. ok = published_message_was_not_received_by_pidslave
  333. end,
  334. %% kill processes
  335. syn_test_suite_helper:kill_process(PidLocal),
  336. syn_test_suite_helper:kill_process(PidSlave).
  337. two_nodes_multi_call(Config) ->
  338. %% get slave
  339. SlaveNode = proplists:get_value(slave_node, Config),
  340. %% set schema location
  341. application:set_env(mnesia, schema_location, ram),
  342. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  343. %% start
  344. ok = syn:start(),
  345. ok = syn:init(),
  346. ok = rpc:call(SlaveNode, syn, start, []),
  347. ok = rpc:call(SlaveNode, syn, init, []),
  348. timer:sleep(100),
  349. %% start processes
  350. PidLocal = syn_test_suite_helper:start_process(fun() -> called_loop(pid1) end),
  351. PidSlave = syn_test_suite_helper:start_process(SlaveNode, fun() -> called_loop(pid2) end),
  352. PidUnresponsive = syn_test_suite_helper:start_process(),
  353. %% register
  354. ok = syn:join(<<"my group">>, PidLocal),
  355. ok = syn:join(<<"my group">>, PidSlave),
  356. ok = syn:join(<<"my group">>, PidUnresponsive),
  357. timer:sleep(100),
  358. %% call
  359. {Replies, BadPids} = syn:multi_call(<<"my group">>, get_pid_name),
  360. %% check responses
  361. 2 = length(Replies),
  362. pid1 = proplists:get_value(PidLocal, Replies),
  363. pid2 = proplists:get_value(PidSlave, Replies),
  364. [PidUnresponsive] = BadPids,
  365. %% kill processes
  366. syn_test_suite_helper:kill_process(PidLocal),
  367. syn_test_suite_helper:kill_process(PidSlave),
  368. syn_test_suite_helper:kill_process(PidUnresponsive).
  369. %% ===================================================================
  370. %% Internal
  371. %% ===================================================================
  372. recipient_loop(Pid) ->
  373. receive
  374. Message -> Pid ! {received, self(), Message}
  375. end.
  376. called_loop(PidName) ->
  377. receive
  378. {syn_multi_call, CallerPid, get_pid_name} -> syn:multi_call_reply(CallerPid, PidName)
  379. end.