syn_registry_SUITE.erl 46 KB

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