syn_groups_SUITE.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. %% ==========================================================================================================
  2. %% Syn - A global process registry.
  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. ]).
  38. -export([
  39. two_nodes_kill/1,
  40. two_nodes_publish/1
  41. ]).
  42. %% internal
  43. -export([recipient_loop/1]).
  44. %% include
  45. -include_lib("common_test/include/ct.hrl").
  46. %% ===================================================================
  47. %% Callbacks
  48. %% ===================================================================
  49. %% -------------------------------------------------------------------
  50. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  51. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  52. %% GroupName = atom()
  53. %% TestCase = atom()
  54. %% Reason = term()
  55. %% -------------------------------------------------------------------
  56. all() ->
  57. [
  58. {group, single_node_process_groups},
  59. {group, two_nodes_process_groups}
  60. ].
  61. %% -------------------------------------------------------------------
  62. %% Function: groups() -> [Group]
  63. %% Group = {GroupName,Properties,GroupsAndTestCases}
  64. %% GroupName = atom()
  65. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  66. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  67. %% TestCase = atom()
  68. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  69. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  70. %% repeat_until_any_ok | repeat_until_any_fail
  71. %% N = integer() | forever
  72. %% -------------------------------------------------------------------
  73. groups() ->
  74. [
  75. {single_node_process_groups, [shuffle], [
  76. single_node_leave,
  77. single_node_kill,
  78. single_node_publish
  79. ]},
  80. {two_nodes_process_groups, [shuffle], [
  81. two_nodes_kill,
  82. two_nodes_publish
  83. ]}
  84. ].
  85. %% -------------------------------------------------------------------
  86. %% Function: init_per_suite(Config0) ->
  87. %% Config1 | {skip,Reason} |
  88. %% {skip_and_save,Reason,Config1}
  89. %% Config0 = Config1 = [tuple()]
  90. %% Reason = term()
  91. %% -------------------------------------------------------------------
  92. init_per_suite(Config) ->
  93. %% config
  94. [
  95. {slave_node_short_name, syn_slave}
  96. | Config
  97. ].
  98. %% -------------------------------------------------------------------
  99. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  100. %% Config0 = Config1 = [tuple()]
  101. %% -------------------------------------------------------------------
  102. end_per_suite(_Config) -> ok.
  103. %% -------------------------------------------------------------------
  104. %% Function: init_per_group(GroupName, Config0) ->
  105. %% Config1 | {skip,Reason} |
  106. %% {skip_and_save,Reason,Config1}
  107. %% GroupName = atom()
  108. %% Config0 = Config1 = [tuple()]
  109. %% Reason = term()
  110. %% -------------------------------------------------------------------
  111. init_per_group(two_nodes_process_groups, Config) ->
  112. %% start slave
  113. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  114. {ok, SlaveNode} = syn_test_suite_helper:start_slave(SlaveNodeShortName),
  115. %% config
  116. [
  117. {slave_node, SlaveNode}
  118. | Config
  119. ];
  120. init_per_group(_GroupName, Config) -> Config.
  121. %% -------------------------------------------------------------------
  122. %% Function: end_per_group(GroupName, Config0) ->
  123. %% void() | {save_config,Config1}
  124. %% GroupName = atom()
  125. %% Config0 = Config1 = [tuple()]
  126. %% -------------------------------------------------------------------
  127. end_per_group(two_nodes_process_groups, Config) ->
  128. %% get slave node name
  129. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  130. %% stop slave
  131. syn_test_suite_helper:stop_slave(SlaveNodeShortName);
  132. end_per_group(_GroupName, _Config) ->
  133. ok.
  134. % ----------------------------------------------------------------------------------------------------------
  135. % Function: init_per_testcase(TestCase, Config0) ->
  136. % Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  137. % TestCase = atom()
  138. % Config0 = Config1 = [tuple()]
  139. % Reason = term()
  140. % ----------------------------------------------------------------------------------------------------------
  141. init_per_testcase(_TestCase, Config) ->
  142. Config.
  143. % ----------------------------------------------------------------------------------------------------------
  144. % Function: end_per_testcase(TestCase, Config0) ->
  145. % void() | {save_config,Config1} | {fail,Reason}
  146. % TestCase = atom()
  147. % Config0 = Config1 = [tuple()]
  148. % Reason = term()
  149. % ----------------------------------------------------------------------------------------------------------
  150. end_per_testcase(_TestCase, Config) ->
  151. %% get slave
  152. SlaveNode = proplists:get_value(slave_node, Config),
  153. syn_test_suite_helper:clean_after_test(SlaveNode).
  154. %% ===================================================================
  155. %% Tests
  156. %% ===================================================================
  157. single_node_leave(_Config) ->
  158. %% set schema location
  159. application:set_env(mnesia, schema_location, ram),
  160. %% start
  161. ok = syn:start(),
  162. ok = syn:init(),
  163. %% start process
  164. Pid = syn_test_suite_helper:start_process(),
  165. %% retrieve
  166. [] = syn:get_members(<<"my pg">>),
  167. false = syn:member(Pid, <<"my pg">>),
  168. %% join
  169. ok = syn:join(<<"my pg">>, Pid),
  170. %% retrieve
  171. [Pid] = syn:get_members(<<"my pg">>),
  172. true = syn:member(Pid, <<"my pg">>),
  173. %% leave
  174. ok = syn:leave(<<"my pg">>, Pid),
  175. %% retrieve
  176. [] = syn:get_members(<<"my pg">>),
  177. false = syn:member(Pid, <<"my pg">>),
  178. %% kill process
  179. syn_test_suite_helper:kill_process(Pid).
  180. single_node_kill(_Config) ->
  181. %% set schema location
  182. application:set_env(mnesia, schema_location, ram),
  183. %% start
  184. ok = syn:start(),
  185. ok = syn:init(),
  186. %% start process
  187. Pid = syn_test_suite_helper:start_process(),
  188. %% retrieve
  189. [] = syn:get_members(<<"my pg">>),
  190. false = syn:member(Pid, <<"my pg">>),
  191. %% join
  192. ok = syn:join(<<"my pg">>, Pid),
  193. %% retrieve
  194. [Pid] = syn:get_members(<<"my pg">>),
  195. true = syn:member(Pid, <<"my pg">>),
  196. %% kill process
  197. syn_test_suite_helper:kill_process(Pid),
  198. timer:sleep(100),
  199. %% retrieve
  200. [] = syn:get_members(<<"my pg">>),
  201. false = syn:member(Pid, <<"my pg">>).
  202. single_node_publish(_Config) ->
  203. %% set schema location
  204. application:set_env(mnesia, schema_location, ram),
  205. %% start
  206. ok = syn:start(),
  207. ok = syn:init(),
  208. %% start process
  209. ResultPid = self(),
  210. F = fun() -> recipient_loop(ResultPid) end,
  211. Pid1 = syn_test_suite_helper:start_process(F),
  212. Pid2 = syn_test_suite_helper:start_process(F),
  213. %% join
  214. ok = syn:join(<<"my pg">>, Pid1),
  215. ok = syn:join(<<"my pg">>, Pid2),
  216. %% publish
  217. syn:publish(<<"my pg">>, {test, message}),
  218. %% check publish was received
  219. receive
  220. {received, Pid1, {test, message}} -> ok
  221. after 2000 ->
  222. ok = published_message_was_not_received_by_pid1
  223. end,
  224. receive
  225. {received, Pid2, {test, message}} -> ok
  226. after 2000 ->
  227. ok = published_message_was_not_received_by_pid2
  228. end,
  229. %% kill processes
  230. syn_test_suite_helper:kill_process(Pid1),
  231. syn_test_suite_helper:kill_process(Pid2).
  232. two_nodes_kill(Config) ->
  233. %% get slave
  234. SlaveNode = proplists:get_value(slave_node, Config),
  235. %% set schema location
  236. application:set_env(mnesia, schema_location, ram),
  237. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  238. %% start
  239. ok = syn:start(),
  240. ok = syn:init(),
  241. ok = rpc:call(SlaveNode, syn, start, []),
  242. ok = rpc:call(SlaveNode, syn, init, []),
  243. timer:sleep(100),
  244. %% start processes
  245. PidLocal = syn_test_suite_helper:start_process(),
  246. PidSlave = syn_test_suite_helper:start_process(SlaveNode),
  247. %% retrieve
  248. [] = syn:get_members(<<"my pg">>),
  249. false = syn:member(PidLocal, <<"my pg">>),
  250. false = syn:member(PidSlave, <<"my pg">>),
  251. [] = rpc:call(SlaveNode, syn, get_members, [<<"my pg">>]),
  252. false = rpc:call(SlaveNode, syn, member, [PidLocal, <<"my pg">>]),
  253. false = rpc:call(SlaveNode, syn, member, [PidSlave, <<"my pg">>]),
  254. %% register
  255. ok = syn:join(<<"my pg">>, PidSlave),
  256. ok = rpc:call(SlaveNode, syn, join, [<<"my pg">>, PidLocal]),
  257. %% retrieve
  258. 2 = length(syn:get_members(<<"my pg">>)),
  259. true = syn:member(PidLocal, <<"my pg">>),
  260. true = syn:member(PidSlave, <<"my pg">>),
  261. 2 = length(rpc:call(SlaveNode, syn, get_members, [<<"my pg">>])),
  262. true = rpc:call(SlaveNode, syn, member, [PidLocal, <<"my pg">>]),
  263. true = rpc:call(SlaveNode, syn, member, [PidSlave, <<"my pg">>]),
  264. %% kill processes
  265. syn_test_suite_helper:kill_process(PidLocal),
  266. syn_test_suite_helper:kill_process(PidSlave),
  267. timer:sleep(100),
  268. %% retrieve
  269. [] = syn:get_members(<<"my pg">>),
  270. false = syn:member(PidLocal, <<"my pg">>),
  271. false = syn:member(PidSlave, <<"my pg">>),
  272. [] = rpc:call(SlaveNode, syn, get_members, [<<"my pg">>]),
  273. false = rpc:call(SlaveNode, syn, member, [PidLocal, <<"my pg">>]),
  274. false = rpc:call(SlaveNode, syn, member, [PidSlave, <<"my pg">>]).
  275. two_nodes_publish(Config) ->
  276. %% get slave
  277. SlaveNode = proplists:get_value(slave_node, Config),
  278. %% set schema location
  279. application:set_env(mnesia, schema_location, ram),
  280. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  281. %% start
  282. ok = syn:start(),
  283. ok = syn:init(),
  284. ok = rpc:call(SlaveNode, syn, start, []),
  285. ok = rpc:call(SlaveNode, syn, init, []),
  286. timer:sleep(100),
  287. %% start process
  288. ResultPid = self(),
  289. F = fun() -> recipient_loop(ResultPid) end,
  290. PidLocal = syn_test_suite_helper:start_process(F),
  291. PidSlave = syn_test_suite_helper:start_process(SlaveNode, F),
  292. %% register
  293. ok = syn:join(<<"my pg">>, PidSlave),
  294. ok = rpc:call(SlaveNode, syn, join, [<<"my pg">>, PidLocal]),
  295. %% publish
  296. syn:publish(<<"my pg">>, {test, message}),
  297. %% check publish was received
  298. receive
  299. {received, PidLocal, {test, message}} -> ok
  300. after 2000 ->
  301. ok = published_message_was_not_received_by_pidlocal
  302. end,
  303. receive
  304. {received, PidSlave, {test, message}} -> ok
  305. after 2000 ->
  306. ok = published_message_was_not_received_by_pidslave
  307. end,
  308. %% kill processes
  309. syn_test_suite_helper:kill_process(PidLocal),
  310. syn_test_suite_helper:kill_process(PidSlave).
  311. %% ===================================================================
  312. %% Internal
  313. %% ===================================================================
  314. recipient_loop(Pid) ->
  315. receive
  316. Message -> Pid ! {received, self(), Message}
  317. end.