syn_registry_SUITE.erl 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. %% check
  250. assert_scope_subcluster(node(), custom_scope_ab, [SlaveNode1]),
  251. assert_scope_subcluster(node(), custom_scope_all, [SlaveNode1, SlaveNode2]),
  252. assert_scope_subcluster(SlaveNode1, custom_scope_ab, [node()]),
  253. assert_scope_subcluster(SlaveNode1, custom_scope_bc, [SlaveNode2]),
  254. assert_scope_subcluster(SlaveNode1, custom_scope_all, [node(), SlaveNode2]),
  255. assert_scope_subcluster(SlaveNode2, custom_scope_bc, [SlaveNode1]),
  256. assert_scope_subcluster(SlaveNode2, custom_scope_c, []),
  257. assert_scope_subcluster(SlaveNode2, custom_scope_all, [node(), SlaveNode1]),
  258. %% crash a scope process on 2
  259. rpc:call(SlaveNode2, syn_test_suite_helper, kill_process, [syn_registry_custom_scope_bc]),
  260. timer:sleep(250),
  261. %% check
  262. assert_scope_subcluster(node(), custom_scope_ab, [SlaveNode1]),
  263. assert_scope_subcluster(node(), custom_scope_all, [SlaveNode1, SlaveNode2]),
  264. assert_scope_subcluster(SlaveNode1, custom_scope_ab, [node()]),
  265. assert_scope_subcluster(SlaveNode1, custom_scope_bc, [SlaveNode2]),
  266. assert_scope_subcluster(SlaveNode1, custom_scope_all, [node(), SlaveNode2]),
  267. assert_scope_subcluster(SlaveNode2, custom_scope_bc, [SlaveNode1]),
  268. assert_scope_subcluster(SlaveNode2, custom_scope_c, []),
  269. assert_scope_subcluster(SlaveNode2, custom_scope_all, [node(), SlaveNode1]),
  270. %% crash scopes supervisor on local
  271. syn_test_suite_helper:kill_process(syn_scopes_sup),
  272. timer:sleep(250),
  273. %% check
  274. assert_scope_subcluster(node(), custom_scope_ab, [SlaveNode1]),
  275. assert_scope_subcluster(node(), custom_scope_all, [SlaveNode1, SlaveNode2]),
  276. assert_scope_subcluster(SlaveNode1, custom_scope_ab, [node()]),
  277. assert_scope_subcluster(SlaveNode1, custom_scope_bc, [SlaveNode2]),
  278. assert_scope_subcluster(SlaveNode1, custom_scope_all, [node(), SlaveNode2]),
  279. assert_scope_subcluster(SlaveNode2, custom_scope_bc, [SlaveNode1]),
  280. assert_scope_subcluster(SlaveNode2, custom_scope_c, []),
  281. assert_scope_subcluster(SlaveNode2, custom_scope_all, [node(), SlaveNode1]).
  282. three_nodes_register_unregister_and_monitor_default_scope(Config) ->
  283. %% get slaves
  284. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  285. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  286. %% start syn on nodes
  287. ok = syn:start(),
  288. ok = rpc:call(SlaveNode1, syn, start, []),
  289. ok = rpc:call(SlaveNode2, syn, start, []),
  290. timer:sleep(250),
  291. %% start processes
  292. Pid = syn_test_suite_helper:start_process(),
  293. PidWithMeta = syn_test_suite_helper:start_process(),
  294. PidRemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  295. %% retrieve
  296. undefined = syn:lookup(<<"my proc">>),
  297. undefined = rpc:call(SlaveNode1, syn, lookup, [<<"my proc">>]),
  298. undefined = rpc:call(SlaveNode2, syn, lookup, [<<"my proc">>]),
  299. undefined = syn:lookup({"my proc alias"}),
  300. undefined = rpc:call(SlaveNode1, syn, lookup, [{"my proc alias"}]),
  301. undefined = rpc:call(SlaveNode2, syn, lookup, [{"my proc alias"}]),
  302. undefined = syn:lookup(<<"my proc with meta">>),
  303. undefined = rpc:call(SlaveNode1, syn, lookup, [<<"my proc with meta">>]),
  304. undefined = rpc:call(SlaveNode2, syn, lookup, [<<"my proc with meta">>]),
  305. undefined = syn:lookup({remote_pid_on, slave_1}),
  306. undefined = rpc:call(SlaveNode1, syn, lookup, [{remote_pid_on, slave_1}]),
  307. undefined = rpc:call(SlaveNode2, syn, lookup, [{remote_pid_on, slave_1}]),
  308. 0 = syn:registry_count(default),
  309. 0 = syn:registry_count(default, node()),
  310. 0 = syn:registry_count(default, SlaveNode1),
  311. 0 = syn:registry_count(default, SlaveNode2),
  312. %% register
  313. ok = syn:register(<<"my proc">>, Pid),
  314. ok = syn:register({"my proc alias"}, Pid), %% same pid, different name
  315. ok = syn:register(<<"my proc with meta">>, PidWithMeta, {meta, <<"meta">>}), %% pid with meta
  316. ok = syn:register({remote_pid_on, slave_1}, PidRemoteOn1), %% remote on slave 1
  317. timer:sleep(250),
  318. %% errors
  319. {error, taken} = syn:register(<<"my proc">>, PidRemoteOn1),
  320. {error, not_alive} = syn:register({"pid not alive"}, list_to_pid("<0.9999.0>")),
  321. %% retrieve
  322. {Pid, undefined} = syn:lookup(<<"my proc">>),
  323. {Pid, undefined} = rpc:call(SlaveNode1, syn, lookup, [<<"my proc">>]),
  324. {Pid, undefined} = rpc:call(SlaveNode2, syn, lookup, [<<"my proc">>]),
  325. {Pid, undefined} = syn:lookup({"my proc alias"}),
  326. {Pid, undefined} = rpc:call(SlaveNode1, syn, lookup, [{"my proc alias"}]),
  327. {Pid, undefined} = rpc:call(SlaveNode2, syn, lookup, [{"my proc alias"}]),
  328. {PidWithMeta, {meta, <<"meta">>}} = syn:lookup(<<"my proc with meta">>),
  329. {PidWithMeta, {meta, <<"meta">>}} = rpc:call(SlaveNode1, syn, lookup, [<<"my proc with meta">>]),
  330. {PidWithMeta, {meta, <<"meta">>}} = rpc:call(SlaveNode2, syn, lookup, [<<"my proc with meta">>]),
  331. {PidRemoteOn1, undefined} = syn:lookup({remote_pid_on, slave_1}),
  332. {PidRemoteOn1, undefined} = rpc:call(SlaveNode1, syn, lookup, [{remote_pid_on, slave_1}]),
  333. {PidRemoteOn1, undefined} = rpc:call(SlaveNode2, syn, lookup, [{remote_pid_on, slave_1}]),
  334. 4 = syn:registry_count(default),
  335. 3 = syn:registry_count(default, node()),
  336. 1 = syn:registry_count(default, SlaveNode1),
  337. 0 = syn:registry_count(default, SlaveNode2),
  338. %% re-register to edit meta
  339. ok = syn:register(<<"my proc with meta">>, PidWithMeta, {meta2, <<"meta2">>}),
  340. ok = rpc:call(SlaveNode2, syn, register, [{remote_pid_on, slave_1}, PidRemoteOn1, added_meta]), %% updated on slave 2
  341. timer:sleep(250),
  342. %% retrieve
  343. {PidWithMeta, {meta2, <<"meta2">>}} = syn:lookup(<<"my proc with meta">>),
  344. {PidWithMeta, {meta2, <<"meta2">>}} = rpc:call(SlaveNode1, syn, lookup, [<<"my proc with meta">>]),
  345. {PidWithMeta, {meta2, <<"meta2">>}} = rpc:call(SlaveNode2, syn, lookup, [<<"my proc with meta">>]),
  346. {PidRemoteOn1, added_meta} = syn:lookup({remote_pid_on, slave_1}),
  347. {PidRemoteOn1, added_meta} = rpc:call(SlaveNode1, syn, lookup, [{remote_pid_on, slave_1}]),
  348. {PidRemoteOn1, added_meta} = rpc:call(SlaveNode2, syn, lookup, [{remote_pid_on, slave_1}]),
  349. 4 = syn:registry_count(default),
  350. 3 = syn:registry_count(default, node()),
  351. 1 = syn:registry_count(default, SlaveNode1),
  352. 0 = syn:registry_count(default, SlaveNode2),
  353. %% crash scope process to ensure that monitors get recreated
  354. exit(whereis(syn_registry_default), kill),
  355. timer:sleep(250), %$ wait for sup to restart it
  356. %% kill process
  357. syn_test_suite_helper:kill_process(Pid),
  358. syn_test_suite_helper:kill_process(PidRemoteOn1),
  359. %% unregister process
  360. ok = syn:unregister(<<"my proc with meta">>),
  361. timer:sleep(250),
  362. %% retrieve
  363. undefined = syn:lookup(<<"my proc">>),
  364. undefined = rpc:call(SlaveNode1, syn, lookup, [<<"my proc">>]),
  365. undefined = rpc:call(SlaveNode2, syn, lookup, [<<"my proc">>]),
  366. undefined = syn:lookup({"my proc alias"}),
  367. undefined = rpc:call(SlaveNode1, syn, lookup, [{"my proc alias"}]),
  368. undefined = rpc:call(SlaveNode2, syn, lookup, [{"my proc alias"}]),
  369. undefined = syn:lookup(<<"my proc with meta">>),
  370. undefined = rpc:call(SlaveNode1, syn, lookup, [<<"my proc with meta">>]),
  371. undefined = rpc:call(SlaveNode2, syn, lookup, [<<"my proc with meta">>]),
  372. undefined = syn:lookup({remote_pid_on, slave_1}),
  373. undefined = rpc:call(SlaveNode1, syn, lookup, [{remote_pid_on, slave_1}]),
  374. undefined = rpc:call(SlaveNode2, syn, lookup, [{remote_pid_on, slave_1}]),
  375. 0 = syn:registry_count(default),
  376. 0 = syn:registry_count(default, node()),
  377. 0 = syn:registry_count(default, SlaveNode1),
  378. 0 = syn:registry_count(default, SlaveNode2),
  379. %% errors
  380. {error, undefined} = syn:unregister({invalid_name}),
  381. %% (simulate race condition)
  382. Pid1 = syn_test_suite_helper:start_process(),
  383. Pid2 = syn_test_suite_helper:start_process(),
  384. ok = syn:register(<<"my proc">>, Pid1),
  385. timer:sleep(250),
  386. syn_registry:remove_from_local_table(default, <<"my proc">>, Pid1),
  387. syn_registry:add_to_local_table(default, <<"my proc">>, Pid2, undefined, 0, undefined),
  388. {error, race_condition} = rpc:call(SlaveNode1, syn, unregister, [<<"my proc">>]).
  389. three_nodes_register_unregister_and_monitor_custom_scope(Config) ->
  390. %% get slaves
  391. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  392. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  393. %% start syn on nodes
  394. ok = syn:start(),
  395. ok = rpc:call(SlaveNode1, syn, start, []),
  396. ok = rpc:call(SlaveNode2, syn, start, []),
  397. timer:sleep(250),
  398. %% add custom scopes
  399. ok = syn:add_node_to_scope(custom_scope_ab),
  400. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[custom_scope_ab, custom_scope_bc]]),
  401. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[custom_scope_bc]]),
  402. timer:sleep(250),
  403. %% start processes
  404. Pid = syn_test_suite_helper:start_process(),
  405. PidWithMeta = syn_test_suite_helper:start_process(),
  406. PidRemoteWithMetaOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  407. %% retrieve
  408. undefined = syn:lookup("scope_a"),
  409. undefined = syn:lookup("scope_a"),
  410. undefined = rpc:call(SlaveNode1, syn, lookup, ["scope_a"]),
  411. undefined = syn:lookup(custom_scope_ab, "scope_a"),
  412. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a"]),
  413. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a"]),
  414. undefined = syn:lookup(custom_scope_ab, "scope_a_alias"),
  415. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  416. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  417. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, {remote_scoped_bc}),
  418. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  419. undefined = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  420. 0 = syn:registry_count(custom_scope_ab),
  421. 0 = syn:registry_count(custom_scope_ab, node()),
  422. 0 = syn:registry_count(custom_scope_ab, SlaveNode1),
  423. 0 = syn:registry_count(custom_scope_ab, SlaveNode2),
  424. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  425. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, node()),
  426. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode1),
  427. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode2),
  428. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab]),
  429. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, node()]),
  430. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  431. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  432. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  433. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  434. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  435. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  436. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab]),
  437. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, node()]),
  438. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  439. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  440. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  441. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  442. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  443. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  444. %% register
  445. ok = syn:register(custom_scope_ab, "scope_a", Pid),
  446. ok = syn:register(custom_scope_ab, "scope_a_alias", PidWithMeta, <<"with_meta">>),
  447. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:register(custom_scope_bc, "scope_a", Pid),
  448. {'EXIT', {{invalid_scope, non_existent_scope}, _}} = catch syn:register(non_existent_scope, "scope_a", Pid),
  449. ok = rpc:call(SlaveNode2, syn, register, [custom_scope_bc, {remote_scoped_bc}, PidRemoteWithMetaOn1, <<"with_meta 1">>]),
  450. timer:sleep(250),
  451. %% errors
  452. {error, taken} = syn:register(custom_scope_ab, "scope_a", PidWithMeta),
  453. {error, not_alive} = syn:register(custom_scope_ab, {"pid not alive"}, list_to_pid("<0.9999.0>")),
  454. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:register(custom_scope_bc, "scope_a_noscope", Pid),
  455. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:unregister(custom_scope_bc, "scope_a_noscope"),
  456. %% retrieve
  457. undefined = syn:lookup("scope_a"),
  458. undefined = syn:lookup("scope_a"),
  459. undefined = rpc:call(SlaveNode1, syn, lookup, ["scope_a"]),
  460. {Pid, undefined} = syn:lookup(custom_scope_ab, "scope_a"),
  461. {Pid, undefined} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a"]),
  462. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a"]),
  463. {PidWithMeta, <<"with_meta">>} = syn:lookup(custom_scope_ab, "scope_a_alias"),
  464. {PidWithMeta, <<"with_meta">>} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  465. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  466. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, {remote_scoped_bc}),
  467. {PidRemoteWithMetaOn1, <<"with_meta 1">>} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  468. {PidRemoteWithMetaOn1, <<"with_meta 1">>} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  469. 2 = syn:registry_count(custom_scope_ab),
  470. 2 = syn:registry_count(custom_scope_ab, node()),
  471. 0 = syn:registry_count(custom_scope_ab, SlaveNode1),
  472. 0 = syn:registry_count(custom_scope_ab, SlaveNode2),
  473. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  474. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, node()),
  475. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode1),
  476. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode2),
  477. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab]),
  478. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, node()]),
  479. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  480. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  481. 1 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  482. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  483. 1 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  484. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  485. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab]),
  486. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, node()]),
  487. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  488. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  489. 1 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  490. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  491. 1 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  492. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  493. %% re-register to edit meta
  494. ok = syn:register(custom_scope_ab, "scope_a_alias", PidWithMeta, <<"with_meta_updated">>),
  495. timer:sleep(250),
  496. {PidWithMeta, <<"with_meta_updated">>} = syn:lookup(custom_scope_ab, "scope_a_alias"),
  497. {PidWithMeta, <<"with_meta_updated">>} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  498. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  499. %% crash scope process to ensure that monitors get recreated
  500. exit(whereis(syn_registry_custom_scope_ab), kill),
  501. timer:sleep(250), %$ wait for sup to restart it
  502. %% kill process
  503. syn_test_suite_helper:kill_process(Pid),
  504. syn_test_suite_helper:kill_process(PidWithMeta),
  505. %% unregister processes
  506. {error, undefined} = catch syn:unregister(<<"my proc with meta">>),
  507. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:unregister(custom_scope_bc, <<"my proc with meta">>),
  508. ok = rpc:call(SlaveNode1, syn, unregister, [custom_scope_bc, {remote_scoped_bc}]),
  509. timer:sleep(250),
  510. %% retrieve
  511. undefined = syn:lookup("scope_a"),
  512. undefined = syn:lookup("scope_a"),
  513. undefined = rpc:call(SlaveNode1, syn, lookup, ["scope_a"]),
  514. undefined = syn:lookup(custom_scope_ab, "scope_a"),
  515. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a"]),
  516. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a"]),
  517. undefined = syn:lookup(custom_scope_ab, "scope_a_alias"),
  518. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  519. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  520. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, {remote_scoped_bc}),
  521. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  522. undefined = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  523. 0 = syn:registry_count(custom_scope_ab),
  524. 0 = syn:registry_count(custom_scope_ab, node()),
  525. 0 = syn:registry_count(custom_scope_ab, SlaveNode1),
  526. 0 = syn:registry_count(custom_scope_ab, SlaveNode2),
  527. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  528. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, node()),
  529. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode1),
  530. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode2),
  531. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab]),
  532. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, node()]),
  533. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  534. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  535. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  536. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  537. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  538. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  539. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab]),
  540. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, node()]),
  541. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  542. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  543. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  544. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  545. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  546. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  547. %% errors
  548. {error, undefined} = syn:unregister(custom_scope_ab, {invalid_name}),
  549. %% (simulate race condition)
  550. Pid1 = syn_test_suite_helper:start_process(),
  551. Pid2 = syn_test_suite_helper:start_process(),
  552. ok = syn:register(custom_scope_ab, <<"my proc">>, Pid1),
  553. timer:sleep(250),
  554. syn_registry:remove_from_local_table(custom_scope_ab, <<"my proc">>, Pid1),
  555. syn_registry:add_to_local_table(custom_scope_ab, <<"my proc">>, Pid2, undefined, 0, undefined),
  556. {error, race_condition} = rpc:call(SlaveNode1, syn, unregister, [custom_scope_ab, <<"my proc">>]).
  557. three_nodes_cluster_changes(Config) ->
  558. %% get slaves
  559. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  560. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  561. %% disconnect 1 from 2
  562. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  563. %% start syn on 1 and 2, nodes don't know of each other
  564. ok = rpc:call(SlaveNode1, syn, start, []),
  565. ok = rpc:call(SlaveNode2, syn, start, []),
  566. %% add custom scopes
  567. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[custom_scope_bc]]),
  568. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[custom_scope_bc]]),
  569. timer:sleep(250),
  570. %% start processes
  571. PidRemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  572. PidRemoteOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  573. %% register
  574. ok = rpc:call(SlaveNode1, syn, register, ["proc-1", PidRemoteOn1, "meta-1"]),
  575. ok = rpc:call(SlaveNode1, syn, register, ["proc-2", PidRemoteOn2, "meta-2"]),
  576. ok = rpc:call(SlaveNode1, syn, register, [custom_scope_bc, "BC-proc-1", PidRemoteOn1, "meta-1"]),
  577. ok = rpc:call(SlaveNode1, syn, register, [custom_scope_bc, "BC-proc-1 alias", PidRemoteOn1, "meta-1 alias"]),
  578. timer:sleep(250),
  579. %% form full cluster
  580. ok = syn:start(),
  581. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  582. syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1, SlaveNode2]),
  583. timer:sleep(250),
  584. %% retrieve
  585. {PidRemoteOn1, "meta-1"} = syn:lookup("proc-1"),
  586. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, ["proc-1"]),
  587. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, ["proc-1"]),
  588. {PidRemoteOn2, "meta-2"} = syn:lookup("proc-2"),
  589. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode1, syn, lookup, ["proc-2"]),
  590. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, ["proc-2"]),
  591. 2 = syn:registry_count(default),
  592. 0 = syn:registry_count(default, node()),
  593. 1 = syn:registry_count(default, SlaveNode1),
  594. 1 = syn:registry_count(default, SlaveNode2),
  595. 2 = rpc:call(SlaveNode1, syn, registry_count, [default]),
  596. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, node()]),
  597. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode1]),
  598. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode2]),
  599. 2 = rpc:call(SlaveNode2, syn, registry_count, [default]),
  600. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, node()]),
  601. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode1]),
  602. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode2]),
  603. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1"),
  604. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  605. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  606. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1 alias"),
  607. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  608. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  609. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  610. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  611. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  612. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  613. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  614. 2 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  615. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  616. 2 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  617. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  618. %% partial netsplit (1 cannot see 2)
  619. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  620. timer:sleep(250),
  621. %% retrieve
  622. {PidRemoteOn1, "meta-1"} = syn:lookup("proc-1"),
  623. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, ["proc-1"]),
  624. undefined = rpc:call(SlaveNode2, syn, lookup, ["proc-1"]),
  625. {PidRemoteOn2, "meta-2"} = syn:lookup("proc-2"),
  626. undefined = rpc:call(SlaveNode1, syn, lookup, ["proc-2"]),
  627. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, ["proc-2"]),
  628. 2 = syn:registry_count(default),
  629. 0 = syn:registry_count(default, node()),
  630. 1 = syn:registry_count(default, SlaveNode1),
  631. 1 = syn:registry_count(default, SlaveNode2),
  632. 1 = rpc:call(SlaveNode1, syn, registry_count, [default]),
  633. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, node()]),
  634. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode1]),
  635. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode2]),
  636. 1 = rpc:call(SlaveNode2, syn, registry_count, [default]),
  637. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, node()]),
  638. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode1]),
  639. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode2]),
  640. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1"),
  641. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  642. undefined = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  643. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1 alias"),
  644. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  645. undefined = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  646. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  647. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  648. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  649. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  650. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  651. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  652. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  653. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  654. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  655. %% re-join
  656. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  657. syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1, SlaveNode2]),
  658. timer:sleep(250),
  659. %% retrieve
  660. {PidRemoteOn1, "meta-1"} = syn:lookup("proc-1"),
  661. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, ["proc-1"]),
  662. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, ["proc-1"]),
  663. {PidRemoteOn2, "meta-2"} = syn:lookup("proc-2"),
  664. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode1, syn, lookup, ["proc-2"]),
  665. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, ["proc-2"]),
  666. 2 = syn:registry_count(default),
  667. 0 = syn:registry_count(default, node()),
  668. 1 = syn:registry_count(default, SlaveNode1),
  669. 1 = syn:registry_count(default, SlaveNode2),
  670. 2 = rpc:call(SlaveNode1, syn, registry_count, [default]),
  671. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, node()]),
  672. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode1]),
  673. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode2]),
  674. 2 = rpc:call(SlaveNode2, syn, registry_count, [default]),
  675. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, node()]),
  676. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode1]),
  677. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode2]),
  678. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1"),
  679. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  680. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  681. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1 alias"),
  682. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  683. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  684. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  685. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  686. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  687. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  688. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  689. 2 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  690. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  691. 2 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  692. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]).
  693. three_nodes_cluster_conflicts(Config) ->
  694. %% get slaves
  695. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  696. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  697. %% start syn on nodes
  698. ok = syn:start(),
  699. ok = rpc:call(SlaveNode1, syn, start, []),
  700. ok = rpc:call(SlaveNode2, syn, start, []),
  701. %% add custom scopes
  702. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[custom_scope_bc]]),
  703. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[custom_scope_bc]]),
  704. timer:sleep(250),
  705. %% partial netsplit (1 cannot see 2)
  706. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  707. timer:sleep(250),
  708. %% start conflict processes
  709. Pid2RemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  710. Pid2RemoteOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  711. %% --> conflict by netsplit
  712. ok = rpc:call(SlaveNode1, syn, register, ["proc-confict", Pid2RemoteOn1, "meta-1"]),
  713. ok = rpc:call(SlaveNode2, syn, register, ["proc-confict", Pid2RemoteOn2, "meta-2"]),
  714. ok = rpc:call(SlaveNode1, syn, register, [custom_scope_bc, "proc-confict", Pid2RemoteOn1, "meta-1"]),
  715. ok = rpc:call(SlaveNode2, syn, register, [custom_scope_bc, "proc-confict", Pid2RemoteOn2, "meta-2"]),
  716. %% re-join
  717. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  718. syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1, SlaveNode2]),
  719. timer:sleep(250),
  720. %% retrieve
  721. {Pid2RemoteOn2, "meta-2"} = syn:lookup("proc-confict"),
  722. {Pid2RemoteOn2, "meta-2"} = rpc:call(SlaveNode1, syn, lookup, ["proc-confict"]),
  723. {Pid2RemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, ["proc-confict"]),
  724. 1 = syn:registry_count(default),
  725. 0 = syn:registry_count(default, node()),
  726. 0 = syn:registry_count(default, SlaveNode1),
  727. 1 = syn:registry_count(default, SlaveNode2),
  728. 1 = rpc:call(SlaveNode1, syn, registry_count, [default]),
  729. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, node()]),
  730. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode1]),
  731. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode2]),
  732. 1 = rpc:call(SlaveNode2, syn, registry_count, [default]),
  733. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, node()]),
  734. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode1]),
  735. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode2]),
  736. {Pid2RemoteOn2, "meta-2"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "proc-confict"]),
  737. {Pid2RemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "proc-confict"]),
  738. 1 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  739. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  740. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  741. 1 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  742. 1 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  743. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  744. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  745. 1 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  746. %% --> conflict by race condition
  747. Pid1 = syn_test_suite_helper:start_process(),
  748. Pid2 = syn_test_suite_helper:start_process(SlaveNode1),
  749. rpc:call(SlaveNode1, syn_registry, add_to_local_table, [default, <<"my proc">>, Pid2, "meta-2", erlang:system_time(), undefined]),
  750. ok = syn:register(<<"my proc">>, Pid1, "meta-1"),
  751. timer:sleep(250),
  752. {Pid1, "meta-1"} = syn:lookup(<<"my proc">>),
  753. {Pid1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [<<"my proc">>]),
  754. {Pid1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, [<<"my proc">>]),
  755. true = is_process_alive(Pid1),
  756. false = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid2]),
  757. PidCustom1 = syn_test_suite_helper:start_process(SlaveNode1),
  758. PidCustom2 = syn_test_suite_helper:start_process(SlaveNode2),
  759. rpc:call(SlaveNode2, syn_registry, add_to_local_table, [custom_scope_bc, <<"my proc">>, PidCustom2, "meta-2", erlang:system_time(), undefined]),
  760. ok = rpc:call(SlaveNode1, syn, register, [custom_scope_bc, <<"my proc">>, PidCustom1, "meta-1"]),
  761. timer:sleep(250),
  762. {PidCustom1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, <<"my proc">>]),
  763. {PidCustom1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, <<"my proc">>]),
  764. true = rpc:call(SlaveNode1, erlang, is_process_alive, [PidCustom1]),
  765. false = rpc:call(SlaveNode2, erlang, is_process_alive, [PidCustom2]).
  766. three_nodes_custom_event_handler(Config) ->
  767. %% get slaves
  768. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  769. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  770. %% add custom handler
  771. syn_test_suite_helper:use_custom_handler(),
  772. rpc:call(SlaveNode1, syn_test_suite_helper, use_custom_handler, []),
  773. rpc:call(SlaveNode2, syn_test_suite_helper, use_custom_handler, []),
  774. %% start syn on nodes
  775. ok = syn:start(),
  776. ok = rpc:call(SlaveNode1, syn, start, []),
  777. ok = rpc:call(SlaveNode2, syn, start, []),
  778. timer:sleep(250),
  779. %% register test process to receive messages back from test handler
  780. global:register_name(syn_test_main_process, self()),
  781. %% start process
  782. Pid = syn_test_suite_helper:start_process(),
  783. ok = syn:register("proc-handler", Pid, <<"my-meta">>),
  784. %% check callbacks on_process_registered called on all nodes
  785. CurrentNode = node(),
  786. receive
  787. {on_process_registered, CurrentNode, default, "proc-handler", Pid, <<"my-meta">>} -> ok
  788. after 1000 ->
  789. ok = on_process_registered_not_called_on_main_node
  790. end,
  791. receive
  792. {on_process_registered, SlaveNode1, default, "proc-handler", Pid, <<"my-meta">>} -> ok
  793. after 1000 ->
  794. ok = on_process_registered_not_called_on_slave_1_node
  795. end,
  796. receive
  797. {on_process_registered, SlaveNode2, default, "proc-handler", Pid, <<"my-meta">>} -> ok
  798. after 1000 ->
  799. ok = on_process_registered_not_called_on_slave_2_node
  800. end,
  801. ok = syn:register("proc-handler", Pid, <<"my-new-meta">>),
  802. %% check callbacks on_process_registered are called on nodes because of change of meta
  803. receive
  804. {on_process_registered, CurrentNode, default, "proc-handler", Pid, <<"my-new-meta">>} -> ok
  805. after 1000 ->
  806. ok = on_process_registered_not_called_on_main_node
  807. end,
  808. receive
  809. {on_process_registered, SlaveNode1, default, "proc-handler", Pid, <<"my-new-meta">>} -> ok
  810. after 1000 ->
  811. ok = on_process_registered_not_called_on_slave_1_node
  812. end,
  813. receive
  814. {on_process_registered, SlaveNode2, default, "proc-handler", Pid, <<"my-new-meta">>} -> ok
  815. after 1000 ->
  816. ok = on_process_registered_not_called_on_slave_2_node
  817. end,
  818. %% clean
  819. global:unregister_name(syn_test_main_process).
  820. %% ===================================================================
  821. %% Internal
  822. %% ===================================================================
  823. assert_scope_subcluster(Node, Scope, ExpectedNodes) ->
  824. NodesMap = rpc:call(Node, syn_registry, get_subcluster_nodes, [Scope]),
  825. Nodes = maps:keys(NodesMap),
  826. ExpectedCount = length(ExpectedNodes),
  827. ExpectedCount = length(Nodes),
  828. lists:foreach(fun(RemoteNode) -> true = lists:member(RemoteNode, Nodes) end, ExpectedNodes).