syn_groups_SUITE.erl 16 KB

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