syn_groups_SUITE.erl 18 KB

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