syn_registry_SUITE.erl 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2019 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_registry_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. three_nodes_discover_default_scope/1,
  35. three_nodes_discover_custom_scope/1,
  36. three_nodes_register_unregister_and_monitor_default_scope/1,
  37. three_nodes_register_unregister_and_monitor_custom_scope/1,
  38. three_nodes_cluster_changes/1,
  39. three_nodes_cluster_conflicts/1,
  40. three_nodes_custom_event_handler/1
  41. ]).
  42. %% include
  43. -include_lib("common_test/include/ct.hrl").
  44. -include_lib("../src/syn.hrl").
  45. %% ===================================================================
  46. %% Callbacks
  47. %% ===================================================================
  48. %% -------------------------------------------------------------------
  49. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  50. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  51. %% GroupName = atom()
  52. %% TestCase = atom()
  53. %% Reason = any()
  54. %% -------------------------------------------------------------------
  55. all() ->
  56. [
  57. {group, three_nodes_process_registration}
  58. ].
  59. %% -------------------------------------------------------------------
  60. %% Function: groups() -> [Group]
  61. %% Group = {GroupName,Properties,GroupsAndTestCases}
  62. %% GroupName = atom()
  63. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  64. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  65. %% TestCase = atom()
  66. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  67. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  68. %% repeat_until_any_ok | repeat_until_any_fail
  69. %% N = integer() | forever
  70. %% -------------------------------------------------------------------
  71. groups() ->
  72. [
  73. {three_nodes_process_registration, [shuffle], [
  74. three_nodes_discover_default_scope,
  75. three_nodes_discover_custom_scope,
  76. three_nodes_register_unregister_and_monitor_default_scope,
  77. three_nodes_register_unregister_and_monitor_custom_scope,
  78. three_nodes_cluster_changes,
  79. three_nodes_cluster_conflicts,
  80. three_nodes_custom_event_handler
  81. ]}
  82. ].
  83. %% -------------------------------------------------------------------
  84. %% Function: init_per_suite(Config0) ->
  85. %% Config1 | {skip,Reason} |
  86. %% {skip_and_save,Reason,Config1}
  87. %% Config0 = Config1 = [tuple()]
  88. %% Reason = any()
  89. %% -------------------------------------------------------------------
  90. init_per_suite(Config) ->
  91. Config.
  92. %% -------------------------------------------------------------------
  93. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  94. %% Config0 = Config1 = [tuple()]
  95. %% -------------------------------------------------------------------
  96. end_per_suite(_Config) ->
  97. ok.
  98. %% -------------------------------------------------------------------
  99. %% Function: init_per_group(GroupName, Config0) ->
  100. %% Config1 | {skip,Reason} |
  101. %% {skip_and_save,Reason,Config1}
  102. %% GroupName = atom()
  103. %% Config0 = Config1 = [tuple()]
  104. %% Reason = any()
  105. %% -------------------------------------------------------------------
  106. init_per_group(three_nodes_process_registration, Config) ->
  107. %% start slave
  108. {ok, SlaveNode1} = syn_test_suite_helper:start_slave(syn_slave_1),
  109. {ok, SlaveNode2} = syn_test_suite_helper:start_slave(syn_slave_2),
  110. %% wait full cluster
  111. case syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1, SlaveNode2]) of
  112. ok ->
  113. %% config
  114. [{slave_node_1, SlaveNode1}, {slave_node_2, SlaveNode2} | Config];
  115. Other ->
  116. ct:pal("*********** Could not get full cluster, skipping"),
  117. end_per_group(three_nodes_process_registration, Config),
  118. {skip, Other}
  119. end;
  120. init_per_group(_GroupName, Config) ->
  121. Config.
  122. %% -------------------------------------------------------------------
  123. %% Function: end_per_group(GroupName, Config0) ->
  124. %% void() | {save_config,Config1}
  125. %% GroupName = atom()
  126. %% Config0 = Config1 = [tuple()]
  127. %% -------------------------------------------------------------------
  128. end_per_group(three_nodes_process_registration, Config) ->
  129. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  130. syn_test_suite_helper:connect_node(SlaveNode1),
  131. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  132. syn_test_suite_helper:connect_node(SlaveNode2),
  133. syn_test_suite_helper:clean_after_test(),
  134. syn_test_suite_helper:stop_slave(syn_slave_1),
  135. syn_test_suite_helper:stop_slave(syn_slave_2),
  136. timer:sleep(1000);
  137. end_per_group(_GroupName, _Config) ->
  138. syn_test_suite_helper:clean_after_test().
  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 = any()
  145. %% -------------------------------------------------------------------
  146. init_per_testcase(TestCase, Config) ->
  147. ct:pal("Starting test: ~p", [TestCase]),
  148. Config.
  149. %% -------------------------------------------------------------------
  150. %% Function: end_per_testcase(TestCase, Config0) ->
  151. %% void() | {save_config,Config1} | {fail,Reason}
  152. %% TestCase = atom()
  153. %% Config0 = Config1 = [tuple()]
  154. %% Reason = any()
  155. %% -------------------------------------------------------------------
  156. end_per_testcase(_, _Config) ->
  157. syn_test_suite_helper:clean_after_test().
  158. %% ===================================================================
  159. %% Tests
  160. %% ===================================================================
  161. three_nodes_discover_default_scope(Config) ->
  162. %% get slaves
  163. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  164. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  165. %% start syn on nodes
  166. ok = syn:start(),
  167. ok = rpc:call(SlaveNode1, syn, start, []),
  168. ok = rpc:call(SlaveNode2, syn, start, []),
  169. timer:sleep(250),
  170. %% check
  171. assert_scope_subcluster(node(), default, [SlaveNode1, SlaveNode2]),
  172. assert_scope_subcluster(SlaveNode1, default, [node(), SlaveNode2]),
  173. assert_scope_subcluster(SlaveNode2, default, [node(), SlaveNode1]),
  174. %% simulate full netsplit
  175. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  176. syn_test_suite_helper:disconnect_node(SlaveNode1),
  177. syn_test_suite_helper:disconnect_node(SlaveNode2),
  178. timer:sleep(250),
  179. %% check
  180. assert_scope_subcluster(node(), default, []),
  181. %% reconnect slave 1
  182. syn_test_suite_helper:connect_node(SlaveNode1),
  183. ok = syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1]),
  184. %% check
  185. assert_scope_subcluster(node(), default, [SlaveNode1]),
  186. assert_scope_subcluster(SlaveNode1, default, [node()]),
  187. %% reconnect all
  188. syn_test_suite_helper:connect_node(SlaveNode2),
  189. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  190. ok = syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1, SlaveNode2]),
  191. %% check
  192. assert_scope_subcluster(node(), default, [SlaveNode1, SlaveNode2]),
  193. assert_scope_subcluster(SlaveNode1, default, [node(), SlaveNode2]),
  194. assert_scope_subcluster(SlaveNode2, default, [node(), SlaveNode1]),
  195. %% crash the scope process on local
  196. syn_test_suite_helper:kill_process(syn_registry_default),
  197. timer:sleep(250),
  198. %% check, it should have rebuilt after supervisor restarts it
  199. assert_scope_subcluster(node(), default, [SlaveNode1, SlaveNode2]),
  200. assert_scope_subcluster(SlaveNode1, default, [node(), SlaveNode2]),
  201. assert_scope_subcluster(SlaveNode2, default, [node(), SlaveNode1]),
  202. %% crash scopes supervisor on local
  203. syn_test_suite_helper:kill_process(syn_scopes_sup),
  204. timer:sleep(250),
  205. %% check
  206. assert_scope_subcluster(node(), default, [SlaveNode1, SlaveNode2]),
  207. assert_scope_subcluster(SlaveNode1, default, [node(), SlaveNode2]),
  208. assert_scope_subcluster(SlaveNode2, default, [node(), SlaveNode1]).
  209. three_nodes_discover_custom_scope(Config) ->
  210. %% get slaves
  211. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  212. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  213. %% start syn on nodes
  214. ok = syn:start(),
  215. ok = rpc:call(SlaveNode1, syn, start, []),
  216. ok = rpc:call(SlaveNode2, syn, start, []),
  217. timer:sleep(250),
  218. %% add custom scopes
  219. ok = syn:add_node_to_scope(custom_scope_ab),
  220. ok = syn:add_node_to_scope(custom_scope_all),
  221. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[custom_scope_ab, custom_scope_bc, custom_scope_all]]),
  222. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[custom_scope_bc, custom_scope_c, custom_scope_all]]),
  223. timer:sleep(250),
  224. %% check
  225. assert_scope_subcluster(node(), custom_scope_ab, [SlaveNode1]),
  226. assert_scope_subcluster(node(), custom_scope_all, [SlaveNode1, SlaveNode2]),
  227. assert_scope_subcluster(SlaveNode1, custom_scope_ab, [node()]),
  228. assert_scope_subcluster(SlaveNode1, custom_scope_bc, [SlaveNode2]),
  229. assert_scope_subcluster(SlaveNode1, custom_scope_all, [node(), SlaveNode2]),
  230. assert_scope_subcluster(SlaveNode2, custom_scope_bc, [SlaveNode1]),
  231. assert_scope_subcluster(SlaveNode2, custom_scope_c, []),
  232. assert_scope_subcluster(SlaveNode2, custom_scope_all, [node(), SlaveNode1]),
  233. %% check default
  234. assert_scope_subcluster(node(), default, [SlaveNode1, SlaveNode2]),
  235. assert_scope_subcluster(SlaveNode1, default, [node(), SlaveNode2]),
  236. assert_scope_subcluster(SlaveNode2, default, [node(), SlaveNode1]),
  237. %% disconnect node 2 (node 1 can still see node 2)
  238. syn_test_suite_helper:disconnect_node(SlaveNode2),
  239. timer:sleep(250),
  240. %% check
  241. assert_scope_subcluster(node(), custom_scope_ab, [SlaveNode1]),
  242. assert_scope_subcluster(node(), custom_scope_all, [SlaveNode1]),
  243. assert_scope_subcluster(SlaveNode1, custom_scope_ab, [node()]),
  244. assert_scope_subcluster(SlaveNode1, custom_scope_bc, [SlaveNode2]),
  245. assert_scope_subcluster(SlaveNode1, custom_scope_all, [node(), SlaveNode2]),
  246. %% reconnect node 2
  247. syn_test_suite_helper:connect_node(SlaveNode2),
  248. ok = syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1, SlaveNode2]),
  249. timer:sleep(250),
  250. %% check
  251. assert_scope_subcluster(node(), custom_scope_ab, [SlaveNode1]),
  252. assert_scope_subcluster(node(), custom_scope_all, [SlaveNode1, SlaveNode2]),
  253. assert_scope_subcluster(SlaveNode1, custom_scope_ab, [node()]),
  254. assert_scope_subcluster(SlaveNode1, custom_scope_bc, [SlaveNode2]),
  255. assert_scope_subcluster(SlaveNode1, custom_scope_all, [node(), SlaveNode2]),
  256. assert_scope_subcluster(SlaveNode2, custom_scope_bc, [SlaveNode1]),
  257. assert_scope_subcluster(SlaveNode2, custom_scope_c, []),
  258. assert_scope_subcluster(SlaveNode2, custom_scope_all, [node(), SlaveNode1]),
  259. %% crash a scope process on 2
  260. rpc:call(SlaveNode2, syn_test_suite_helper, kill_process, [syn_registry_custom_scope_bc]),
  261. timer:sleep(250),
  262. %% check
  263. assert_scope_subcluster(node(), custom_scope_ab, [SlaveNode1]),
  264. assert_scope_subcluster(node(), custom_scope_all, [SlaveNode1, SlaveNode2]),
  265. assert_scope_subcluster(SlaveNode1, custom_scope_ab, [node()]),
  266. assert_scope_subcluster(SlaveNode1, custom_scope_bc, [SlaveNode2]),
  267. assert_scope_subcluster(SlaveNode1, custom_scope_all, [node(), SlaveNode2]),
  268. assert_scope_subcluster(SlaveNode2, custom_scope_bc, [SlaveNode1]),
  269. assert_scope_subcluster(SlaveNode2, custom_scope_c, []),
  270. assert_scope_subcluster(SlaveNode2, custom_scope_all, [node(), SlaveNode1]),
  271. %% crash scopes supervisor on local
  272. syn_test_suite_helper:kill_process(syn_scopes_sup),
  273. timer:sleep(250),
  274. %% check
  275. assert_scope_subcluster(node(), custom_scope_ab, [SlaveNode1]),
  276. assert_scope_subcluster(node(), custom_scope_all, [SlaveNode1, SlaveNode2]),
  277. assert_scope_subcluster(SlaveNode1, custom_scope_ab, [node()]),
  278. assert_scope_subcluster(SlaveNode1, custom_scope_bc, [SlaveNode2]),
  279. assert_scope_subcluster(SlaveNode1, custom_scope_all, [node(), SlaveNode2]),
  280. assert_scope_subcluster(SlaveNode2, custom_scope_bc, [SlaveNode1]),
  281. assert_scope_subcluster(SlaveNode2, custom_scope_c, []),
  282. assert_scope_subcluster(SlaveNode2, custom_scope_all, [node(), SlaveNode1]).
  283. three_nodes_register_unregister_and_monitor_default_scope(Config) ->
  284. %% get slaves
  285. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  286. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  287. %% start syn on nodes
  288. ok = syn:start(),
  289. ok = rpc:call(SlaveNode1, syn, start, []),
  290. ok = rpc:call(SlaveNode2, syn, start, []),
  291. timer:sleep(250),
  292. %% start processes
  293. Pid = syn_test_suite_helper:start_process(),
  294. PidWithMeta = syn_test_suite_helper:start_process(),
  295. PidRemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  296. %% retrieve
  297. undefined = syn:lookup(<<"my proc">>),
  298. undefined = rpc:call(SlaveNode1, syn, lookup, [<<"my proc">>]),
  299. undefined = rpc:call(SlaveNode2, syn, lookup, [<<"my proc">>]),
  300. undefined = syn:lookup({"my proc alias"}),
  301. undefined = rpc:call(SlaveNode1, syn, lookup, [{"my proc alias"}]),
  302. undefined = rpc:call(SlaveNode2, syn, lookup, [{"my proc alias"}]),
  303. undefined = syn:lookup(<<"my proc with meta">>),
  304. undefined = rpc:call(SlaveNode1, syn, lookup, [<<"my proc with meta">>]),
  305. undefined = rpc:call(SlaveNode2, syn, lookup, [<<"my proc with meta">>]),
  306. undefined = syn:lookup({remote_pid_on, slave_1}),
  307. undefined = rpc:call(SlaveNode1, syn, lookup, [{remote_pid_on, slave_1}]),
  308. undefined = rpc:call(SlaveNode2, syn, lookup, [{remote_pid_on, slave_1}]),
  309. 0 = syn:registry_count(default),
  310. 0 = syn:registry_count(default, node()),
  311. 0 = syn:registry_count(default, SlaveNode1),
  312. 0 = syn:registry_count(default, SlaveNode2),
  313. %% register
  314. ok = syn:register(<<"my proc">>, Pid),
  315. ok = syn:register({"my proc alias"}, Pid), %% same pid, different name
  316. ok = syn:register(<<"my proc with meta">>, PidWithMeta, {meta, <<"meta">>}), %% pid with meta
  317. ok = syn:register({remote_pid_on, slave_1}, PidRemoteOn1), %% remote on slave 1
  318. timer:sleep(250),
  319. %% errors
  320. {error, taken} = syn:register(<<"my proc">>, PidRemoteOn1),
  321. {error, not_alive} = syn:register({"pid not alive"}, list_to_pid("<0.9999.0>")),
  322. %% retrieve
  323. {Pid, undefined} = syn:lookup(<<"my proc">>),
  324. {Pid, undefined} = rpc:call(SlaveNode1, syn, lookup, [<<"my proc">>]),
  325. {Pid, undefined} = rpc:call(SlaveNode2, syn, lookup, [<<"my proc">>]),
  326. {Pid, undefined} = syn:lookup({"my proc alias"}),
  327. {Pid, undefined} = rpc:call(SlaveNode1, syn, lookup, [{"my proc alias"}]),
  328. {Pid, undefined} = rpc:call(SlaveNode2, syn, lookup, [{"my proc alias"}]),
  329. {PidWithMeta, {meta, <<"meta">>}} = syn:lookup(<<"my proc with meta">>),
  330. {PidWithMeta, {meta, <<"meta">>}} = rpc:call(SlaveNode1, syn, lookup, [<<"my proc with meta">>]),
  331. {PidWithMeta, {meta, <<"meta">>}} = rpc:call(SlaveNode2, syn, lookup, [<<"my proc with meta">>]),
  332. {PidRemoteOn1, undefined} = syn:lookup({remote_pid_on, slave_1}),
  333. {PidRemoteOn1, undefined} = rpc:call(SlaveNode1, syn, lookup, [{remote_pid_on, slave_1}]),
  334. {PidRemoteOn1, undefined} = rpc:call(SlaveNode2, syn, lookup, [{remote_pid_on, slave_1}]),
  335. 4 = syn:registry_count(default),
  336. 3 = syn:registry_count(default, node()),
  337. 1 = syn:registry_count(default, SlaveNode1),
  338. 0 = syn:registry_count(default, SlaveNode2),
  339. %% re-register to edit meta
  340. ok = syn:register(<<"my proc with meta">>, PidWithMeta, {meta2, <<"meta2">>}),
  341. ok = rpc:call(SlaveNode2, syn, register, [{remote_pid_on, slave_1}, PidRemoteOn1, added_meta]), %% updated on slave 2
  342. timer:sleep(250),
  343. %% retrieve
  344. {PidWithMeta, {meta2, <<"meta2">>}} = syn:lookup(<<"my proc with meta">>),
  345. {PidWithMeta, {meta2, <<"meta2">>}} = rpc:call(SlaveNode1, syn, lookup, [<<"my proc with meta">>]),
  346. {PidWithMeta, {meta2, <<"meta2">>}} = rpc:call(SlaveNode2, syn, lookup, [<<"my proc with meta">>]),
  347. {PidRemoteOn1, added_meta} = syn:lookup({remote_pid_on, slave_1}),
  348. {PidRemoteOn1, added_meta} = rpc:call(SlaveNode1, syn, lookup, [{remote_pid_on, slave_1}]),
  349. {PidRemoteOn1, added_meta} = rpc:call(SlaveNode2, syn, lookup, [{remote_pid_on, slave_1}]),
  350. 4 = syn:registry_count(default),
  351. 3 = syn:registry_count(default, node()),
  352. 1 = syn:registry_count(default, SlaveNode1),
  353. 0 = syn:registry_count(default, SlaveNode2),
  354. %% crash scope process to ensure that monitors get recreated
  355. exit(whereis(syn_registry_default), kill),
  356. timer:sleep(250), %$ wait for sup to restart it
  357. %% kill process
  358. syn_test_suite_helper:kill_process(Pid),
  359. syn_test_suite_helper:kill_process(PidRemoteOn1),
  360. %% unregister process
  361. ok = syn:unregister(<<"my proc with meta">>),
  362. timer:sleep(250),
  363. %% retrieve
  364. undefined = syn:lookup(<<"my proc">>),
  365. undefined = rpc:call(SlaveNode1, syn, lookup, [<<"my proc">>]),
  366. undefined = rpc:call(SlaveNode2, syn, lookup, [<<"my proc">>]),
  367. undefined = syn:lookup({"my proc alias"}),
  368. undefined = rpc:call(SlaveNode1, syn, lookup, [{"my proc alias"}]),
  369. undefined = rpc:call(SlaveNode2, syn, lookup, [{"my proc alias"}]),
  370. undefined = syn:lookup(<<"my proc with meta">>),
  371. undefined = rpc:call(SlaveNode1, syn, lookup, [<<"my proc with meta">>]),
  372. undefined = rpc:call(SlaveNode2, syn, lookup, [<<"my proc with meta">>]),
  373. undefined = syn:lookup({remote_pid_on, slave_1}),
  374. undefined = rpc:call(SlaveNode1, syn, lookup, [{remote_pid_on, slave_1}]),
  375. undefined = rpc:call(SlaveNode2, syn, lookup, [{remote_pid_on, slave_1}]),
  376. 0 = syn:registry_count(default),
  377. 0 = syn:registry_count(default, node()),
  378. 0 = syn:registry_count(default, SlaveNode1),
  379. 0 = syn:registry_count(default, SlaveNode2),
  380. %% errors
  381. {error, undefined} = syn:unregister({invalid_name}),
  382. %% (simulate race condition)
  383. Pid1 = syn_test_suite_helper:start_process(),
  384. Pid2 = syn_test_suite_helper:start_process(),
  385. ok = syn:register(<<"my proc">>, Pid1),
  386. timer:sleep(250),
  387. syn_registry:remove_from_local_table(default, <<"my proc">>, Pid1),
  388. syn_registry:add_to_local_table(default, <<"my proc">>, Pid2, undefined, 0, undefined),
  389. {error, race_condition} = rpc:call(SlaveNode1, syn, unregister, [<<"my proc">>]).
  390. three_nodes_register_unregister_and_monitor_custom_scope(Config) ->
  391. %% get slaves
  392. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  393. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  394. %% start syn on nodes
  395. ok = syn:start(),
  396. ok = rpc:call(SlaveNode1, syn, start, []),
  397. ok = rpc:call(SlaveNode2, syn, start, []),
  398. timer:sleep(250),
  399. %% add custom scopes
  400. ok = syn:add_node_to_scope(custom_scope_ab),
  401. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[custom_scope_ab, custom_scope_bc]]),
  402. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[custom_scope_bc]]),
  403. timer:sleep(250),
  404. %% start processes
  405. Pid = syn_test_suite_helper:start_process(),
  406. PidWithMeta = syn_test_suite_helper:start_process(),
  407. PidRemoteWithMetaOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  408. %% retrieve
  409. undefined = syn:lookup("scope_a"),
  410. undefined = syn:lookup("scope_a"),
  411. undefined = rpc:call(SlaveNode1, syn, lookup, ["scope_a"]),
  412. undefined = syn:lookup(custom_scope_ab, "scope_a"),
  413. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a"]),
  414. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a"]),
  415. undefined = syn:lookup(custom_scope_ab, "scope_a_alias"),
  416. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  417. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  418. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, {remote_scoped_bc}),
  419. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  420. undefined = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  421. 0 = syn:registry_count(custom_scope_ab),
  422. 0 = syn:registry_count(custom_scope_ab, node()),
  423. 0 = syn:registry_count(custom_scope_ab, SlaveNode1),
  424. 0 = syn:registry_count(custom_scope_ab, SlaveNode2),
  425. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  426. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, node()),
  427. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode1),
  428. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode2),
  429. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab]),
  430. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, node()]),
  431. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  432. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  433. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  434. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  435. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  436. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  437. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab]),
  438. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, node()]),
  439. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  440. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  441. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  442. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  443. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  444. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  445. %% register
  446. ok = syn:register(custom_scope_ab, "scope_a", Pid),
  447. ok = syn:register(custom_scope_ab, "scope_a_alias", PidWithMeta, <<"with_meta">>),
  448. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:register(custom_scope_bc, "scope_a", Pid),
  449. {'EXIT', {{invalid_scope, non_existent_scope}, _}} = catch syn:register(non_existent_scope, "scope_a", Pid),
  450. ok = rpc:call(SlaveNode2, syn, register, [custom_scope_bc, {remote_scoped_bc}, PidRemoteWithMetaOn1, <<"with_meta 1">>]),
  451. timer:sleep(250),
  452. %% errors
  453. {error, taken} = syn:register(custom_scope_ab, "scope_a", PidWithMeta),
  454. {error, not_alive} = syn:register(custom_scope_ab, {"pid not alive"}, list_to_pid("<0.9999.0>")),
  455. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:register(custom_scope_bc, "scope_a_noscope", Pid),
  456. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:unregister(custom_scope_bc, "scope_a_noscope"),
  457. %% retrieve
  458. undefined = syn:lookup("scope_a"),
  459. undefined = syn:lookup("scope_a"),
  460. undefined = rpc:call(SlaveNode1, syn, lookup, ["scope_a"]),
  461. {Pid, undefined} = syn:lookup(custom_scope_ab, "scope_a"),
  462. {Pid, undefined} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a"]),
  463. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a"]),
  464. {PidWithMeta, <<"with_meta">>} = syn:lookup(custom_scope_ab, "scope_a_alias"),
  465. {PidWithMeta, <<"with_meta">>} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  466. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  467. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, {remote_scoped_bc}),
  468. {PidRemoteWithMetaOn1, <<"with_meta 1">>} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  469. {PidRemoteWithMetaOn1, <<"with_meta 1">>} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  470. 2 = syn:registry_count(custom_scope_ab),
  471. 2 = syn:registry_count(custom_scope_ab, node()),
  472. 0 = syn:registry_count(custom_scope_ab, SlaveNode1),
  473. 0 = syn:registry_count(custom_scope_ab, SlaveNode2),
  474. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  475. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, node()),
  476. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode1),
  477. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode2),
  478. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab]),
  479. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, node()]),
  480. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  481. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  482. 1 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  483. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  484. 1 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  485. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  486. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab]),
  487. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, node()]),
  488. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  489. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  490. 1 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  491. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  492. 1 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  493. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  494. %% re-register to edit meta
  495. ok = syn:register(custom_scope_ab, "scope_a_alias", PidWithMeta, <<"with_meta_updated">>),
  496. timer:sleep(250),
  497. {PidWithMeta, <<"with_meta_updated">>} = syn:lookup(custom_scope_ab, "scope_a_alias"),
  498. {PidWithMeta, <<"with_meta_updated">>} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  499. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  500. %% crash scope process to ensure that monitors get recreated
  501. exit(whereis(syn_registry_custom_scope_ab), kill),
  502. timer:sleep(250), %$ wait for sup to restart it
  503. %% kill process
  504. syn_test_suite_helper:kill_process(Pid),
  505. syn_test_suite_helper:kill_process(PidWithMeta),
  506. %% unregister processes
  507. {error, undefined} = catch syn:unregister(<<"my proc with meta">>),
  508. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:unregister(custom_scope_bc, <<"my proc with meta">>),
  509. ok = rpc:call(SlaveNode1, syn, unregister, [custom_scope_bc, {remote_scoped_bc}]),
  510. timer:sleep(250),
  511. %% retrieve
  512. undefined = syn:lookup("scope_a"),
  513. undefined = syn:lookup("scope_a"),
  514. undefined = rpc:call(SlaveNode1, syn, lookup, ["scope_a"]),
  515. undefined = syn:lookup(custom_scope_ab, "scope_a"),
  516. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a"]),
  517. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a"]),
  518. undefined = syn:lookup(custom_scope_ab, "scope_a_alias"),
  519. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  520. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  521. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, {remote_scoped_bc}),
  522. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  523. undefined = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  524. 0 = syn:registry_count(custom_scope_ab),
  525. 0 = syn:registry_count(custom_scope_ab, node()),
  526. 0 = syn:registry_count(custom_scope_ab, SlaveNode1),
  527. 0 = syn:registry_count(custom_scope_ab, SlaveNode2),
  528. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  529. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, node()),
  530. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode1),
  531. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode2),
  532. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab]),
  533. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, node()]),
  534. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  535. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  536. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  537. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  538. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  539. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  540. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab]),
  541. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, node()]),
  542. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  543. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  544. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  545. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  546. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  547. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  548. %% errors
  549. {error, undefined} = syn:unregister(custom_scope_ab, {invalid_name}),
  550. %% (simulate race condition)
  551. Pid1 = syn_test_suite_helper:start_process(),
  552. Pid2 = syn_test_suite_helper:start_process(),
  553. ok = syn:register(custom_scope_ab, <<"my proc">>, Pid1),
  554. timer:sleep(250),
  555. syn_registry:remove_from_local_table(custom_scope_ab, <<"my proc">>, Pid1),
  556. syn_registry:add_to_local_table(custom_scope_ab, <<"my proc">>, Pid2, undefined, 0, undefined),
  557. {error, race_condition} = rpc:call(SlaveNode1, syn, unregister, [custom_scope_ab, <<"my proc">>]).
  558. three_nodes_cluster_changes(Config) ->
  559. %% get slaves
  560. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  561. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  562. %% disconnect 1 from 2
  563. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  564. %% start syn on 1 and 2, nodes don't know of each other
  565. ok = rpc:call(SlaveNode1, syn, start, []),
  566. ok = rpc:call(SlaveNode2, syn, start, []),
  567. %% add custom scopes
  568. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[custom_scope_bc]]),
  569. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[custom_scope_bc]]),
  570. timer:sleep(250),
  571. %% start processes
  572. PidRemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  573. PidRemoteOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  574. %% register
  575. ok = rpc:call(SlaveNode1, syn, register, ["proc-1", PidRemoteOn1, "meta-1"]),
  576. ok = rpc:call(SlaveNode1, syn, register, ["proc-2", PidRemoteOn2, "meta-2"]),
  577. ok = rpc:call(SlaveNode1, syn, register, [custom_scope_bc, "BC-proc-1", PidRemoteOn1, "meta-1"]),
  578. ok = rpc:call(SlaveNode1, syn, register, [custom_scope_bc, "BC-proc-1 alias", PidRemoteOn1, "meta-1 alias"]),
  579. timer:sleep(250),
  580. %% form full cluster
  581. ok = syn:start(),
  582. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  583. syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1, SlaveNode2]),
  584. timer:sleep(250),
  585. %% retrieve
  586. {PidRemoteOn1, "meta-1"} = syn:lookup("proc-1"),
  587. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, ["proc-1"]),
  588. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, ["proc-1"]),
  589. {PidRemoteOn2, "meta-2"} = syn:lookup("proc-2"),
  590. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode1, syn, lookup, ["proc-2"]),
  591. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, ["proc-2"]),
  592. 2 = syn:registry_count(default),
  593. 0 = syn:registry_count(default, node()),
  594. 1 = syn:registry_count(default, SlaveNode1),
  595. 1 = syn:registry_count(default, SlaveNode2),
  596. 2 = rpc:call(SlaveNode1, syn, registry_count, [default]),
  597. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, node()]),
  598. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode1]),
  599. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode2]),
  600. 2 = rpc:call(SlaveNode2, syn, registry_count, [default]),
  601. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, node()]),
  602. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode1]),
  603. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode2]),
  604. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1"),
  605. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  606. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  607. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1 alias"),
  608. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  609. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  610. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  611. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  612. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  613. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  614. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  615. 2 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  616. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  617. 2 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  618. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  619. %% partial netsplit (1 cannot see 2)
  620. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  621. timer:sleep(250),
  622. %% retrieve
  623. {PidRemoteOn1, "meta-1"} = syn:lookup("proc-1"),
  624. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, ["proc-1"]),
  625. undefined = rpc:call(SlaveNode2, syn, lookup, ["proc-1"]),
  626. {PidRemoteOn2, "meta-2"} = syn:lookup("proc-2"),
  627. undefined = rpc:call(SlaveNode1, syn, lookup, ["proc-2"]),
  628. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, ["proc-2"]),
  629. 2 = syn:registry_count(default),
  630. 0 = syn:registry_count(default, node()),
  631. 1 = syn:registry_count(default, SlaveNode1),
  632. 1 = syn:registry_count(default, SlaveNode2),
  633. 1 = rpc:call(SlaveNode1, syn, registry_count, [default]),
  634. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, node()]),
  635. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode1]),
  636. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode2]),
  637. 1 = rpc:call(SlaveNode2, syn, registry_count, [default]),
  638. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, node()]),
  639. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode1]),
  640. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode2]),
  641. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1"),
  642. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  643. undefined = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  644. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1 alias"),
  645. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  646. undefined = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  647. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  648. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  649. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  650. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  651. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  652. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  653. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  654. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  655. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  656. %% re-join
  657. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  658. syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1, SlaveNode2]),
  659. timer:sleep(250),
  660. %% retrieve
  661. {PidRemoteOn1, "meta-1"} = syn:lookup("proc-1"),
  662. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, ["proc-1"]),
  663. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, ["proc-1"]),
  664. {PidRemoteOn2, "meta-2"} = syn:lookup("proc-2"),
  665. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode1, syn, lookup, ["proc-2"]),
  666. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, ["proc-2"]),
  667. 2 = syn:registry_count(default),
  668. 0 = syn:registry_count(default, node()),
  669. 1 = syn:registry_count(default, SlaveNode1),
  670. 1 = syn:registry_count(default, SlaveNode2),
  671. 2 = rpc:call(SlaveNode1, syn, registry_count, [default]),
  672. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, node()]),
  673. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode1]),
  674. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode2]),
  675. 2 = rpc:call(SlaveNode2, syn, registry_count, [default]),
  676. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, node()]),
  677. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode1]),
  678. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode2]),
  679. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1"),
  680. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  681. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  682. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1 alias"),
  683. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  684. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  685. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  686. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  687. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  688. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  689. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  690. 2 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  691. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  692. 2 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  693. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]).
  694. three_nodes_cluster_conflicts(Config) ->
  695. %% get slaves
  696. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  697. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  698. %% start syn on nodes
  699. ok = syn:start(),
  700. ok = rpc:call(SlaveNode1, syn, start, []),
  701. ok = rpc:call(SlaveNode2, syn, start, []),
  702. %% add custom scopes
  703. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[custom_scope_bc]]),
  704. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[custom_scope_bc]]),
  705. timer:sleep(250),
  706. %% partial netsplit (1 cannot see 2)
  707. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  708. timer:sleep(250),
  709. %% start conflict processes
  710. Pid2RemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  711. Pid2RemoteOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  712. %% --> conflict by netsplit
  713. ok = rpc:call(SlaveNode1, syn, register, ["proc-confict", Pid2RemoteOn1, "meta-1"]),
  714. ok = rpc:call(SlaveNode2, syn, register, ["proc-confict", Pid2RemoteOn2, "meta-2"]),
  715. ok = rpc:call(SlaveNode1, syn, register, [custom_scope_bc, "proc-confict", Pid2RemoteOn1, "meta-1"]),
  716. ok = rpc:call(SlaveNode2, syn, register, [custom_scope_bc, "proc-confict", Pid2RemoteOn2, "meta-2"]),
  717. %% re-join
  718. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  719. syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1, SlaveNode2]),
  720. timer:sleep(250),
  721. %% retrieve
  722. {Pid2RemoteOn2, "meta-2"} = syn:lookup("proc-confict"),
  723. {Pid2RemoteOn2, "meta-2"} = rpc:call(SlaveNode1, syn, lookup, ["proc-confict"]),
  724. {Pid2RemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, ["proc-confict"]),
  725. 1 = syn:registry_count(default),
  726. 0 = syn:registry_count(default, node()),
  727. 0 = syn:registry_count(default, SlaveNode1),
  728. 1 = syn:registry_count(default, SlaveNode2),
  729. 1 = rpc:call(SlaveNode1, syn, registry_count, [default]),
  730. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, node()]),
  731. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode1]),
  732. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode2]),
  733. 1 = rpc:call(SlaveNode2, syn, registry_count, [default]),
  734. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, node()]),
  735. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode1]),
  736. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode2]),
  737. {Pid2RemoteOn2, "meta-2"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "proc-confict"]),
  738. {Pid2RemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "proc-confict"]),
  739. 1 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  740. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  741. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  742. 1 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  743. 1 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  744. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  745. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  746. 1 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  747. %% --> conflict by race condition
  748. Pid1 = syn_test_suite_helper:start_process(),
  749. Pid2 = syn_test_suite_helper:start_process(SlaveNode1),
  750. rpc:call(SlaveNode1, syn_registry, add_to_local_table, [default, <<"my proc">>, Pid2, "meta-2", erlang:system_time(), undefined]),
  751. ok = syn:register(<<"my proc">>, Pid1, "meta-1"),
  752. timer:sleep(250),
  753. {Pid1, "meta-1"} = syn:lookup(<<"my proc">>),
  754. {Pid1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [<<"my proc">>]),
  755. {Pid1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, [<<"my proc">>]),
  756. true = is_process_alive(Pid1),
  757. false = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid2]),
  758. PidCustom1 = syn_test_suite_helper:start_process(SlaveNode1),
  759. PidCustom2 = syn_test_suite_helper:start_process(SlaveNode2),
  760. rpc:call(SlaveNode2, syn_registry, add_to_local_table, [custom_scope_bc, <<"my proc">>, PidCustom2, "meta-2", erlang:system_time(), undefined]),
  761. ok = rpc:call(SlaveNode1, syn, register, [custom_scope_bc, <<"my proc">>, PidCustom1, "meta-1"]),
  762. timer:sleep(250),
  763. {PidCustom1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, <<"my proc">>]),
  764. {PidCustom1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, <<"my proc">>]),
  765. true = rpc:call(SlaveNode1, erlang, is_process_alive, [PidCustom1]),
  766. false = rpc:call(SlaveNode2, erlang, is_process_alive, [PidCustom2]).
  767. three_nodes_custom_event_handler(Config) ->
  768. %% get slaves
  769. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  770. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  771. %% add custom handler
  772. syn_test_suite_helper:use_custom_handler(),
  773. rpc:call(SlaveNode1, syn_test_suite_helper, use_custom_handler, []),
  774. rpc:call(SlaveNode2, syn_test_suite_helper, use_custom_handler, []),
  775. %% start syn on nodes
  776. ok = syn:start(),
  777. ok = rpc:call(SlaveNode1, syn, start, []),
  778. ok = rpc:call(SlaveNode2, syn, start, []),
  779. timer:sleep(250),
  780. %% register test process to receive messages back from test handler
  781. global:register_name(syn_test_main_process, self()),
  782. %% start process
  783. Pid = syn_test_suite_helper:start_process(),
  784. ok = syn:register("proc-handler", Pid, <<"my-meta">>),
  785. %% check callbacks on_process_registered called on all nodes
  786. CurrentNode = node(),
  787. receive
  788. {on_process_registered, CurrentNode, default, "proc-handler", Pid, <<"my-meta">>} -> ok
  789. after 1000 ->
  790. ok = on_process_registered_not_called_on_main_node
  791. end,
  792. receive
  793. {on_process_registered, SlaveNode1, default, "proc-handler", Pid, <<"my-meta">>} -> ok
  794. after 1000 ->
  795. ok = on_process_registered_not_called_on_slave_1_node
  796. end,
  797. receive
  798. {on_process_registered, SlaveNode2, default, "proc-handler", Pid, <<"my-meta">>} -> ok
  799. after 1000 ->
  800. ok = on_process_registered_not_called_on_slave_2_node
  801. end,
  802. ok = syn:register("proc-handler", Pid, <<"my-new-meta">>),
  803. %% check callbacks on_process_registered are called on nodes because of change of meta
  804. receive
  805. {on_process_registered, CurrentNode, default, "proc-handler", Pid, <<"my-new-meta">>} -> ok
  806. after 1000 ->
  807. ok = on_process_registered_not_called_on_main_node
  808. end,
  809. receive
  810. {on_process_registered, SlaveNode1, default, "proc-handler", Pid, <<"my-new-meta">>} -> ok
  811. after 1000 ->
  812. ok = on_process_registered_not_called_on_slave_1_node
  813. end,
  814. receive
  815. {on_process_registered, SlaveNode2, default, "proc-handler", Pid, <<"my-new-meta">>} -> ok
  816. after 1000 ->
  817. ok = on_process_registered_not_called_on_slave_2_node
  818. end,
  819. %% clean
  820. global:unregister_name(syn_test_main_process).
  821. %% ===================================================================
  822. %% Internal
  823. %% ===================================================================
  824. assert_scope_subcluster(Node, Scope, ExpectedNodes) ->
  825. NodesMap = rpc:call(Node, syn_registry, get_subcluster_nodes, [Scope]),
  826. Nodes = maps:keys(NodesMap),
  827. ExpectedCount = length(ExpectedNodes),
  828. ExpectedCount = length(Nodes),
  829. lists:foreach(fun(RemoteNode) -> true = lists:member(RemoteNode, Nodes) end, ExpectedNodes).