syn_registry_SUITE.erl 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:unregister(custom_scope_bc, "scope_a_noscope"),
  454. %% retrieve
  455. undefined = syn:lookup("scope_a"),
  456. undefined = syn:lookup("scope_a"),
  457. undefined = rpc:call(SlaveNode1, syn, lookup, ["scope_a"]),
  458. {Pid, undefined} = syn:lookup(custom_scope_ab, "scope_a"),
  459. {Pid, undefined} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a"]),
  460. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a"]),
  461. {PidWithMeta, <<"with_meta">>} = syn:lookup(custom_scope_ab, "scope_a_alias"),
  462. {PidWithMeta, <<"with_meta">>} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  463. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  464. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, {remote_scoped_bc}),
  465. {PidRemoteWithMetaOn1, <<"with_meta 1">>} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  466. {PidRemoteWithMetaOn1, <<"with_meta 1">>} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  467. 2 = syn:registry_count(custom_scope_ab),
  468. 2 = syn:registry_count(custom_scope_ab, node()),
  469. 0 = syn:registry_count(custom_scope_ab, SlaveNode1),
  470. 0 = syn:registry_count(custom_scope_ab, SlaveNode2),
  471. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  472. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, node()),
  473. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode1),
  474. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode2),
  475. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab]),
  476. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, node()]),
  477. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  478. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  479. 1 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  480. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  481. 1 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  482. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  483. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab]),
  484. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, node()]),
  485. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  486. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  487. 1 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  488. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  489. 1 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  490. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  491. %% re-register to edit meta
  492. ok = syn:register(custom_scope_ab, "scope_a_alias", PidWithMeta, <<"with_meta_updated">>),
  493. timer:sleep(250),
  494. {PidWithMeta, <<"with_meta_updated">>} = syn:lookup(custom_scope_ab, "scope_a_alias"),
  495. {PidWithMeta, <<"with_meta_updated">>} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  496. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  497. %% crash scope process to ensure that monitors get recreated
  498. exit(whereis(syn_registry_custom_scope_ab), kill),
  499. timer:sleep(250), %$ wait for sup to restart it
  500. %% kill process
  501. syn_test_suite_helper:kill_process(Pid),
  502. syn_test_suite_helper:kill_process(PidWithMeta),
  503. %% unregister processes
  504. {error, undefined} = catch syn:unregister(<<"my proc with meta">>),
  505. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:unregister(custom_scope_bc, <<"my proc with meta">>),
  506. ok = rpc:call(SlaveNode1, syn, unregister, [custom_scope_bc, {remote_scoped_bc}]),
  507. timer:sleep(250),
  508. %% retrieve
  509. undefined = syn:lookup("scope_a"),
  510. undefined = syn:lookup("scope_a"),
  511. undefined = rpc:call(SlaveNode1, syn, lookup, ["scope_a"]),
  512. undefined = syn:lookup(custom_scope_ab, "scope_a"),
  513. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a"]),
  514. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a"]),
  515. undefined = syn:lookup(custom_scope_ab, "scope_a_alias"),
  516. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  517. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [custom_scope_ab, "scope_a_alias"]),
  518. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, {remote_scoped_bc}),
  519. undefined = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  520. undefined = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, {remote_scoped_bc}]),
  521. 0 = syn:registry_count(custom_scope_ab),
  522. 0 = syn:registry_count(custom_scope_ab, node()),
  523. 0 = syn:registry_count(custom_scope_ab, SlaveNode1),
  524. 0 = syn:registry_count(custom_scope_ab, SlaveNode2),
  525. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  526. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, node()),
  527. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode1),
  528. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc, SlaveNode2),
  529. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab]),
  530. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, node()]),
  531. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  532. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  533. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  534. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  535. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  536. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  537. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab]),
  538. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, node()]),
  539. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode1]),
  540. {badrpc, {'EXIT', {{invalid_scope, custom_scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [custom_scope_ab, SlaveNode2]),
  541. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  542. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  543. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  544. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  545. %% errors
  546. {error, undefined} = syn:unregister(custom_scope_ab, {invalid_name}),
  547. %% (simulate race condition)
  548. Pid1 = syn_test_suite_helper:start_process(),
  549. Pid2 = syn_test_suite_helper:start_process(),
  550. ok = syn:register(custom_scope_ab, <<"my proc">>, Pid1),
  551. timer:sleep(250),
  552. syn_registry:remove_from_local_table(custom_scope_ab, <<"my proc">>, Pid1),
  553. syn_registry:add_to_local_table(custom_scope_ab, <<"my proc">>, Pid2, undefined, 0, undefined),
  554. {error, race_condition} = rpc:call(SlaveNode1, syn, unregister, [custom_scope_ab, <<"my proc">>]).
  555. three_nodes_cluster_changes(Config) ->
  556. %% get slaves
  557. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  558. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  559. %% disconnect 1 from 2
  560. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  561. %% start syn on 1 and 2, nodes don't know of each other
  562. ok = rpc:call(SlaveNode1, syn, start, []),
  563. ok = rpc:call(SlaveNode2, syn, start, []),
  564. %% add custom scopes
  565. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[custom_scope_bc]]),
  566. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[custom_scope_bc]]),
  567. timer:sleep(250),
  568. %% start processes
  569. PidRemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  570. PidRemoteOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  571. %% register
  572. ok = rpc:call(SlaveNode1, syn, register, ["proc-1", PidRemoteOn1, "meta-1"]),
  573. ok = rpc:call(SlaveNode1, syn, register, ["proc-2", PidRemoteOn2, "meta-2"]),
  574. ok = rpc:call(SlaveNode1, syn, register, [custom_scope_bc, "BC-proc-1", PidRemoteOn1, "meta-1"]),
  575. ok = rpc:call(SlaveNode1, syn, register, [custom_scope_bc, "BC-proc-1 alias", PidRemoteOn1, "meta-1 alias"]),
  576. timer:sleep(250),
  577. %% form full cluster
  578. ok = syn:start(),
  579. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  580. syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1, SlaveNode2]),
  581. timer:sleep(250),
  582. %% retrieve
  583. {PidRemoteOn1, "meta-1"} = syn:lookup("proc-1"),
  584. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, ["proc-1"]),
  585. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, ["proc-1"]),
  586. {PidRemoteOn2, "meta-2"} = syn:lookup("proc-2"),
  587. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode1, syn, lookup, ["proc-2"]),
  588. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, ["proc-2"]),
  589. 2 = syn:registry_count(default),
  590. 0 = syn:registry_count(default, node()),
  591. 1 = syn:registry_count(default, SlaveNode1),
  592. 1 = syn:registry_count(default, SlaveNode2),
  593. 2 = rpc:call(SlaveNode1, syn, registry_count, [default]),
  594. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, node()]),
  595. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode1]),
  596. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode2]),
  597. 2 = rpc:call(SlaveNode2, syn, registry_count, [default]),
  598. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, node()]),
  599. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode1]),
  600. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode2]),
  601. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1"),
  602. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  603. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  604. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1 alias"),
  605. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  606. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  607. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  608. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  609. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  610. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  611. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  612. 2 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  613. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  614. 2 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  615. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  616. %% partial netsplit (1 cannot see 2)
  617. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  618. timer:sleep(250),
  619. %% retrieve
  620. {PidRemoteOn1, "meta-1"} = syn:lookup("proc-1"),
  621. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, ["proc-1"]),
  622. undefined = rpc:call(SlaveNode2, syn, lookup, ["proc-1"]),
  623. {PidRemoteOn2, "meta-2"} = syn:lookup("proc-2"),
  624. undefined = rpc:call(SlaveNode1, syn, lookup, ["proc-2"]),
  625. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, ["proc-2"]),
  626. 2 = syn:registry_count(default),
  627. 0 = syn:registry_count(default, node()),
  628. 1 = syn:registry_count(default, SlaveNode1),
  629. 1 = syn:registry_count(default, SlaveNode2),
  630. 1 = rpc:call(SlaveNode1, syn, registry_count, [default]),
  631. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, node()]),
  632. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode1]),
  633. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode2]),
  634. 1 = rpc:call(SlaveNode2, syn, registry_count, [default]),
  635. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, node()]),
  636. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode1]),
  637. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode2]),
  638. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1"),
  639. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  640. undefined = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  641. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1 alias"),
  642. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  643. undefined = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  644. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  645. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  646. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  647. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  648. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  649. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  650. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  651. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  652. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  653. %% re-join
  654. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  655. syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1, SlaveNode2]),
  656. timer:sleep(250),
  657. %% retrieve
  658. {PidRemoteOn1, "meta-1"} = syn:lookup("proc-1"),
  659. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, ["proc-1"]),
  660. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, ["proc-1"]),
  661. {PidRemoteOn2, "meta-2"} = syn:lookup("proc-2"),
  662. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode1, syn, lookup, ["proc-2"]),
  663. {PidRemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, ["proc-2"]),
  664. 2 = syn:registry_count(default),
  665. 0 = syn:registry_count(default, node()),
  666. 1 = syn:registry_count(default, SlaveNode1),
  667. 1 = syn:registry_count(default, SlaveNode2),
  668. 2 = rpc:call(SlaveNode1, syn, registry_count, [default]),
  669. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, node()]),
  670. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode1]),
  671. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode2]),
  672. 2 = rpc:call(SlaveNode2, syn, registry_count, [default]),
  673. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, node()]),
  674. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode1]),
  675. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode2]),
  676. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1"),
  677. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  678. {PidRemoteOn1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1"]),
  679. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:lookup(custom_scope_bc, "BC-proc-1 alias"),
  680. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  681. {PidRemoteOn1, "meta-1 alias"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "BC-proc-1 alias"]),
  682. {'EXIT', {{invalid_scope, custom_scope_bc}, _}} = catch syn:registry_count(custom_scope_bc),
  683. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  684. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  685. 2 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  686. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  687. 2 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  688. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  689. 2 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  690. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]).
  691. three_nodes_cluster_conflicts(Config) ->
  692. %% get slaves
  693. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  694. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  695. %% start syn on nodes
  696. ok = syn:start(),
  697. ok = rpc:call(SlaveNode1, syn, start, []),
  698. ok = rpc:call(SlaveNode2, syn, start, []),
  699. %% add custom scopes
  700. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[custom_scope_bc]]),
  701. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[custom_scope_bc]]),
  702. timer:sleep(250),
  703. %% partial netsplit (1 cannot see 2)
  704. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  705. timer:sleep(250),
  706. %% start conflict processes
  707. Pid2RemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  708. Pid2RemoteOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  709. %% --> conflict by netsplit
  710. ok = rpc:call(SlaveNode1, syn, register, ["proc-confict", Pid2RemoteOn1, "meta-1"]),
  711. ok = rpc:call(SlaveNode2, syn, register, ["proc-confict", Pid2RemoteOn2, "meta-2"]),
  712. ok = rpc:call(SlaveNode1, syn, register, [custom_scope_bc, "proc-confict", Pid2RemoteOn1, "meta-1"]),
  713. ok = rpc:call(SlaveNode2, syn, register, [custom_scope_bc, "proc-confict", Pid2RemoteOn2, "meta-2"]),
  714. %% re-join
  715. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  716. syn_test_suite_helper:wait_cluster_connected([node(), SlaveNode1, SlaveNode2]),
  717. timer:sleep(250),
  718. %% retrieve
  719. {Pid2RemoteOn2, "meta-2"} = syn:lookup("proc-confict"),
  720. {Pid2RemoteOn2, "meta-2"} = rpc:call(SlaveNode1, syn, lookup, ["proc-confict"]),
  721. {Pid2RemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, ["proc-confict"]),
  722. 1 = syn:registry_count(default),
  723. 0 = syn:registry_count(default, node()),
  724. 0 = syn:registry_count(default, SlaveNode1),
  725. 1 = syn:registry_count(default, SlaveNode2),
  726. 1 = rpc:call(SlaveNode1, syn, registry_count, [default]),
  727. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, node()]),
  728. 0 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode1]),
  729. 1 = rpc:call(SlaveNode1, syn, registry_count, [default, SlaveNode2]),
  730. 1 = rpc:call(SlaveNode2, syn, registry_count, [default]),
  731. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, node()]),
  732. 0 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode1]),
  733. 1 = rpc:call(SlaveNode2, syn, registry_count, [default, SlaveNode2]),
  734. {Pid2RemoteOn2, "meta-2"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, "proc-confict"]),
  735. {Pid2RemoteOn2, "meta-2"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, "proc-confict"]),
  736. 1 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc]),
  737. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, node()]),
  738. 0 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  739. 1 = rpc:call(SlaveNode1, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  740. 1 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc]),
  741. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, node()]),
  742. 0 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode1]),
  743. 1 = rpc:call(SlaveNode2, syn, registry_count, [custom_scope_bc, SlaveNode2]),
  744. %% --> conflict by race condition
  745. Pid1 = syn_test_suite_helper:start_process(),
  746. Pid2 = syn_test_suite_helper:start_process(SlaveNode1),
  747. rpc:call(SlaveNode1, syn_registry, add_to_local_table, [default, <<"my proc">>, Pid2, "meta-2", erlang:system_time(), undefined]),
  748. ok = syn:register(<<"my proc">>, Pid1, "meta-1"),
  749. timer:sleep(250),
  750. {Pid1, "meta-1"} = syn:lookup(<<"my proc">>),
  751. {Pid1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [<<"my proc">>]),
  752. {Pid1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, [<<"my proc">>]),
  753. true = is_process_alive(Pid1),
  754. false = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid2]),
  755. PidCustom1 = syn_test_suite_helper:start_process(SlaveNode1),
  756. PidCustom2 = syn_test_suite_helper:start_process(SlaveNode2),
  757. rpc:call(SlaveNode2, syn_registry, add_to_local_table, [custom_scope_bc, <<"my proc">>, PidCustom2, "meta-2", erlang:system_time(), undefined]),
  758. ok = rpc:call(SlaveNode1, syn, register, [custom_scope_bc, <<"my proc">>, PidCustom1, "meta-1"]),
  759. timer:sleep(250),
  760. {PidCustom1, "meta-1"} = rpc:call(SlaveNode1, syn, lookup, [custom_scope_bc, <<"my proc">>]),
  761. {PidCustom1, "meta-1"} = rpc:call(SlaveNode2, syn, lookup, [custom_scope_bc, <<"my proc">>]),
  762. true = rpc:call(SlaveNode1, erlang, is_process_alive, [PidCustom1]),
  763. false = rpc:call(SlaveNode2, erlang, is_process_alive, [PidCustom2]).
  764. %% ===================================================================
  765. %% Internal
  766. %% ===================================================================
  767. assert_scope_subcluster(Node, Scope, ExpectedNodes) ->
  768. NodesMap = rpc:call(Node, syn_registry, get_subcluster_nodes, [Scope]),
  769. Nodes = maps:keys(NodesMap),
  770. ExpectedCount = length(ExpectedNodes),
  771. ExpectedCount = length(Nodes),
  772. lists:foreach(fun(RemoteNode) -> true = lists:member(RemoteNode, Nodes) end, ExpectedNodes).