syn_groups_SUITE.erl 17 KB

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