syn_registry_SUITE.erl 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2021 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. one_node_via_register_unregister/1
  35. ]).
  36. -export([
  37. three_nodes_discover/1,
  38. three_nodes_register_unregister_and_monitor/1,
  39. three_nodes_cluster_changes/1,
  40. three_nodes_cluster_conflicts/1,
  41. three_nodes_custom_event_handler_reg_unreg/1,
  42. three_nodes_custom_event_handler_conflict_resolution/1
  43. ]).
  44. %% include
  45. -include_lib("common_test/include/ct.hrl").
  46. -include_lib("syn/src/syn.hrl").
  47. %% ===================================================================
  48. %% Callbacks
  49. %% ===================================================================
  50. %% -------------------------------------------------------------------
  51. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  52. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  53. %% GroupName = atom()
  54. %% TestCase = atom()
  55. %% Reason = any()
  56. %% -------------------------------------------------------------------
  57. all() ->
  58. [
  59. {group, one_node_process_registration},
  60. {group, three_nodes_process_registration}
  61. ].
  62. %% -------------------------------------------------------------------
  63. %% Function: groups() -> [Group]
  64. %% Group = {GroupName,Properties,GroupsAndTestCases}
  65. %% GroupName = atom()
  66. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  67. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  68. %% TestCase = atom()
  69. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  70. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  71. %% repeat_until_any_ok | repeat_until_any_fail
  72. %% N = integer() | forever
  73. %% -------------------------------------------------------------------
  74. groups() ->
  75. [
  76. {one_node_process_registration, [shuffle], [
  77. one_node_via_register_unregister
  78. ]},
  79. {three_nodes_process_registration, [shuffle], [
  80. three_nodes_discover,
  81. three_nodes_register_unregister_and_monitor,
  82. three_nodes_cluster_changes,
  83. three_nodes_cluster_conflicts,
  84. three_nodes_custom_event_handler_reg_unreg,
  85. three_nodes_custom_event_handler_conflict_resolution
  86. ]}
  87. ].
  88. %% -------------------------------------------------------------------
  89. %% Function: init_per_suite(Config0) ->
  90. %% Config1 | {skip,Reason} |
  91. %% {skip_and_save,Reason,Config1}
  92. %% Config0 = Config1 = [tuple()]
  93. %% Reason = any()
  94. %% -------------------------------------------------------------------
  95. init_per_suite(Config) ->
  96. Config.
  97. %% -------------------------------------------------------------------
  98. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  99. %% Config0 = Config1 = [tuple()]
  100. %% -------------------------------------------------------------------
  101. end_per_suite(_Config) ->
  102. ok.
  103. %% -------------------------------------------------------------------
  104. %% Function: init_per_group(GroupName, Config0) ->
  105. %% Config1 | {skip,Reason} |
  106. %% {skip_and_save,Reason,Config1}
  107. %% GroupName = atom()
  108. %% Config0 = Config1 = [tuple()]
  109. %% Reason = any()
  110. %% -------------------------------------------------------------------
  111. init_per_group(three_nodes_process_registration, Config) ->
  112. %% start slave
  113. {ok, SlaveNode1} = syn_test_suite_helper:start_slave(syn_slave_1),
  114. {ok, SlaveNode2} = syn_test_suite_helper:start_slave(syn_slave_2),
  115. syn_test_suite_helper:connect_node(SlaveNode1),
  116. syn_test_suite_helper:connect_node(SlaveNode2),
  117. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  118. %% wait full cluster
  119. case syn_test_suite_helper:wait_cluster_mesh_connected([node(), SlaveNode1, SlaveNode2]) of
  120. ok ->
  121. %% config
  122. [{slave_node_1, SlaveNode1}, {slave_node_2, SlaveNode2} | Config];
  123. Other ->
  124. ct:pal("*********** Could not get full cluster, skipping"),
  125. end_per_group(three_nodes_process_registration, Config),
  126. {skip, Other}
  127. end;
  128. init_per_group(_GroupName, Config) ->
  129. Config.
  130. %% -------------------------------------------------------------------
  131. %% Function: end_per_group(GroupName, Config0) ->
  132. %% void() | {save_config,Config1}
  133. %% GroupName = atom()
  134. %% Config0 = Config1 = [tuple()]
  135. %% -------------------------------------------------------------------
  136. end_per_group(three_nodes_process_registration, Config) ->
  137. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  138. syn_test_suite_helper:connect_node(SlaveNode1),
  139. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  140. syn_test_suite_helper:connect_node(SlaveNode2),
  141. syn_test_suite_helper:clean_after_test(),
  142. syn_test_suite_helper:stop_slave(syn_slave_1),
  143. syn_test_suite_helper:stop_slave(syn_slave_2),
  144. timer:sleep(1000);
  145. end_per_group(_GroupName, _Config) ->
  146. syn_test_suite_helper:clean_after_test().
  147. %% -------------------------------------------------------------------
  148. %% Function: init_per_testcase(TestCase, Config0) ->
  149. %% Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  150. %% TestCase = atom()
  151. %% Config0 = Config1 = [tuple()]
  152. %% Reason = any()
  153. %% -------------------------------------------------------------------
  154. init_per_testcase(TestCase, Config) ->
  155. ct:pal("Starting test: ~p", [TestCase]),
  156. Config.
  157. %% -------------------------------------------------------------------
  158. %% Function: end_per_testcase(TestCase, Config0) ->
  159. %% void() | {save_config,Config1} | {fail,Reason}
  160. %% TestCase = atom()
  161. %% Config0 = Config1 = [tuple()]
  162. %% Reason = any()
  163. %% -------------------------------------------------------------------
  164. end_per_testcase(_, _Config) ->
  165. syn_test_suite_helper:clean_after_test().
  166. %% ===================================================================
  167. %% Tests
  168. %% ===================================================================
  169. one_node_via_register_unregister(_Config) ->
  170. %% start syn
  171. ok = syn:start(),
  172. %% ---> scope
  173. syn:add_node_to_scopes([scope]),
  174. %% start gen server via syn
  175. GenServerNameCustom = {scope, <<"my proc">>},
  176. TupleCustom = {via, syn, GenServerNameCustom},
  177. {ok, PidCustom} = syn_test_gen_server:start_link(TupleCustom),
  178. %% retrieve
  179. {PidCustom, undefined} = syn:lookup(scope, <<"my proc">>),
  180. %% call
  181. pong = syn_test_gen_server:ping(TupleCustom),
  182. %% send via syn
  183. syn:send(GenServerNameCustom, {self(), send_ping}),
  184. syn_test_suite_helper:assert_received_messages([
  185. reply_pong
  186. ]),
  187. %% stop server
  188. syn_test_gen_server:stop(TupleCustom),
  189. %% retrieve
  190. syn_test_suite_helper:assert_wait(
  191. undefined,
  192. fun() -> syn:lookup(scope, <<"my proc">>) end
  193. ),
  194. %% send via syn
  195. {badarg, {GenServerNameCustom, anything}} = catch syn:send(GenServerNameCustom, anything).
  196. three_nodes_discover(Config) ->
  197. %% get slaves
  198. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  199. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  200. %% start syn on nodes
  201. ok = syn:start(),
  202. ok = rpc:call(SlaveNode1, syn, start, []),
  203. ok = rpc:call(SlaveNode2, syn, start, []),
  204. %% add scopes
  205. ok = syn:add_node_to_scopes([scope_ab]),
  206. ok = syn:add_node_to_scopes([scope_all]),
  207. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[scope_ab, scope_bc, scope_all]]),
  208. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[scope_bc, scope_c, scope_all]]),
  209. %% get_subcluster_nodes should return invalid errors
  210. {'EXIT', {{invalid_scope, custom_abcdef}, _}} = catch syn_registry:get_subcluster_nodes(custom_abcdef),
  211. %% check
  212. syn_test_suite_helper:assert_registry_scope_subcluster(node(), scope_ab, [SlaveNode1]),
  213. syn_test_suite_helper:assert_registry_scope_subcluster(node(), scope_all, [SlaveNode1, SlaveNode2]),
  214. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_ab, [node()]),
  215. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_bc, [SlaveNode2]),
  216. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_all, [node(), SlaveNode2]),
  217. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode2, scope_bc, [SlaveNode1]),
  218. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode2, scope_c, []),
  219. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode2, scope_all, [node(), SlaveNode1]),
  220. %% disconnect node 2 (node 1 can still see node 2)
  221. syn_test_suite_helper:disconnect_node(SlaveNode2),
  222. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1]),
  223. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  224. %% check
  225. syn_test_suite_helper:assert_registry_scope_subcluster(node(), scope_ab, [SlaveNode1]),
  226. syn_test_suite_helper:assert_registry_scope_subcluster(node(), scope_all, [SlaveNode1]),
  227. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_ab, [node()]),
  228. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_bc, [SlaveNode2]),
  229. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_all, [node(), SlaveNode2]),
  230. %% reconnect node 2
  231. syn_test_suite_helper:connect_node(SlaveNode2),
  232. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  233. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  234. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  235. %% check
  236. syn_test_suite_helper:assert_registry_scope_subcluster(node(), scope_ab, [SlaveNode1]),
  237. syn_test_suite_helper:assert_registry_scope_subcluster(node(), scope_all, [SlaveNode1, SlaveNode2]),
  238. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_ab, [node()]),
  239. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_bc, [SlaveNode2]),
  240. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_all, [node(), SlaveNode2]),
  241. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode2, scope_bc, [SlaveNode1]),
  242. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode2, scope_c, []),
  243. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode2, scope_all, [node(), SlaveNode1]),
  244. %% crash a scope process on 2
  245. rpc:call(SlaveNode2, syn_test_suite_helper, kill_process, [syn_registry_scope_bc]),
  246. rpc:call(SlaveNode2, syn_test_suite_helper, wait_process_name_ready, [syn_registry_default]),
  247. %% check
  248. syn_test_suite_helper:assert_registry_scope_subcluster(node(), scope_ab, [SlaveNode1]),
  249. syn_test_suite_helper:assert_registry_scope_subcluster(node(), scope_all, [SlaveNode1, SlaveNode2]),
  250. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_ab, [node()]),
  251. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_bc, [SlaveNode2]),
  252. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_all, [node(), SlaveNode2]),
  253. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode2, scope_bc, [SlaveNode1]),
  254. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode2, scope_c, []),
  255. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode2, scope_all, [node(), SlaveNode1]),
  256. %% crash scopes supervisor on local
  257. syn_test_suite_helper:kill_process(syn_scopes_sup),
  258. syn_test_suite_helper:wait_process_name_ready(syn_registry_scope_ab),
  259. syn_test_suite_helper:wait_process_name_ready(syn_registry_scope_all),
  260. %% check
  261. syn_test_suite_helper:assert_registry_scope_subcluster(node(), scope_ab, [SlaveNode1]),
  262. syn_test_suite_helper:assert_registry_scope_subcluster(node(), scope_all, [SlaveNode1, SlaveNode2]),
  263. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_ab, [node()]),
  264. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_bc, [SlaveNode2]),
  265. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode1, scope_all, [node(), SlaveNode2]),
  266. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode2, scope_bc, [SlaveNode1]),
  267. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode2, scope_c, []),
  268. syn_test_suite_helper:assert_registry_scope_subcluster(SlaveNode2, scope_all, [node(), SlaveNode1]).
  269. three_nodes_register_unregister_and_monitor(Config) ->
  270. %% get slaves
  271. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  272. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  273. %% start syn on nodes
  274. ok = syn:start(),
  275. ok = rpc:call(SlaveNode1, syn, start, []),
  276. ok = rpc:call(SlaveNode2, syn, start, []),
  277. %% add scopes
  278. ok = syn:add_node_to_scopes([scope_ab]),
  279. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[scope_ab, scope_bc]]),
  280. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[scope_bc]]),
  281. %% start processes
  282. Pid = syn_test_suite_helper:start_process(),
  283. PidWithMeta = syn_test_suite_helper:start_process(),
  284. PidRemoteWithMetaOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  285. %% retrieve
  286. undefined = syn:lookup(scope_ab, "scope_a"),
  287. undefined = rpc:call(SlaveNode1, syn, lookup, [scope_ab, "scope_a"]),
  288. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [scope_ab, "scope_a"]),
  289. undefined = syn:lookup(scope_ab, "scope_a_alias"),
  290. undefined = rpc:call(SlaveNode1, syn, lookup, [scope_ab, "scope_a_alias"]),
  291. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [scope_ab, "scope_a_alias"]),
  292. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:lookup(scope_bc, {remote_scoped_bc}),
  293. undefined = rpc:call(SlaveNode1, syn, lookup, [scope_bc, {remote_scoped_bc}]),
  294. undefined = rpc:call(SlaveNode2, syn, lookup, [scope_bc, {remote_scoped_bc}]),
  295. 0 = syn:registry_count(scope_ab),
  296. 0 = syn:registry_count(scope_ab, node()),
  297. 0 = syn:registry_count(scope_ab, SlaveNode1),
  298. 0 = syn:registry_count(scope_ab, SlaveNode2),
  299. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc),
  300. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc, node()),
  301. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc, SlaveNode1),
  302. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc, SlaveNode2),
  303. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_ab]),
  304. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_ab, node()]),
  305. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_ab, SlaveNode1]),
  306. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_ab, SlaveNode2]),
  307. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc]),
  308. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, node()]),
  309. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode1]),
  310. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode2]),
  311. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [scope_ab]),
  312. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [scope_ab, node()]),
  313. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [scope_ab, SlaveNode1]),
  314. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [scope_ab, SlaveNode2]),
  315. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc]),
  316. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, node()]),
  317. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode1]),
  318. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode2]),
  319. %% register
  320. ok = syn:register(scope_ab, "scope_a", Pid),
  321. ok = syn:register(scope_ab, "scope_a_alias", PidWithMeta, <<"with_meta">>),
  322. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:register(scope_bc, "scope_a", Pid),
  323. {'EXIT', {{invalid_scope, non_existent_scope}, _}} = catch syn:register(non_existent_scope, "scope_a", Pid),
  324. ok = rpc:call(SlaveNode2, syn, register, [scope_bc, {remote_scoped_bc}, PidRemoteWithMetaOn1, <<"with_meta 1">>]),
  325. %% errors
  326. {error, taken} = syn:register(scope_ab, "scope_a", PidWithMeta),
  327. {error, not_alive} = syn:register(scope_ab, {"pid not alive"}, list_to_pid("<0.9999.0>")),
  328. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:register(scope_bc, "scope_a_noscope", Pid),
  329. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:unregister(scope_bc, "scope_a_noscope"),
  330. {badrpc, {'EXIT', {{invalid_remote_scope, scope_bc}, _}}} = catch rpc:call(SlaveNode1, syn, register, [scope_bc, "pid-outside", Pid]),
  331. %% retrieve
  332. syn_test_suite_helper:assert_wait(
  333. {Pid, undefined},
  334. fun() -> syn:lookup(scope_ab, "scope_a") end
  335. ),
  336. syn_test_suite_helper:assert_wait(
  337. {Pid, undefined},
  338. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_ab, "scope_a"]) end
  339. ),
  340. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [scope_ab, "scope_a"]),
  341. syn_test_suite_helper:assert_wait(
  342. {PidWithMeta, <<"with_meta">>},
  343. fun() -> syn:lookup(scope_ab, "scope_a_alias") end
  344. ),
  345. syn_test_suite_helper:assert_wait(
  346. {PidWithMeta, <<"with_meta">>},
  347. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_ab, "scope_a_alias"]) end
  348. ),
  349. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [scope_ab, "scope_a_alias"]),
  350. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:lookup(scope_bc, {remote_scoped_bc}),
  351. syn_test_suite_helper:assert_wait(
  352. {PidRemoteWithMetaOn1, <<"with_meta 1">>},
  353. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, {remote_scoped_bc}]) end
  354. ),
  355. syn_test_suite_helper:assert_wait(
  356. {PidRemoteWithMetaOn1, <<"with_meta 1">>},
  357. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, {remote_scoped_bc}]) end
  358. ),
  359. 2 = syn:registry_count(scope_ab),
  360. 2 = syn:registry_count(scope_ab, node()),
  361. 0 = syn:registry_count(scope_ab, SlaveNode1),
  362. 0 = syn:registry_count(scope_ab, SlaveNode2),
  363. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc),
  364. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc, node()),
  365. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc, SlaveNode1),
  366. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc, SlaveNode2),
  367. 2 = rpc:call(SlaveNode1, syn, registry_count, [scope_ab]),
  368. 2 = rpc:call(SlaveNode1, syn, registry_count, [scope_ab, node()]),
  369. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_ab, SlaveNode1]),
  370. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_ab, SlaveNode2]),
  371. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc]),
  372. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, node()]),
  373. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode1]),
  374. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode2]),
  375. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [scope_ab]),
  376. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [scope_ab, node()]),
  377. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [scope_ab, SlaveNode1]),
  378. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [scope_ab, SlaveNode2]),
  379. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc]),
  380. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, node()]),
  381. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode1]),
  382. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode2]),
  383. %% re-register to edit meta
  384. ok = syn:register(scope_ab, "scope_a_alias", PidWithMeta, <<"with_meta_updated">>),
  385. syn_test_suite_helper:assert_wait(
  386. {PidWithMeta, <<"with_meta_updated">>},
  387. fun() -> syn:lookup(scope_ab, "scope_a_alias") end
  388. ),
  389. syn_test_suite_helper:assert_wait(
  390. {PidWithMeta, <<"with_meta_updated">>},
  391. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_ab, "scope_a_alias"]) end
  392. ),
  393. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [scope_ab, "scope_a_alias"]),
  394. %% crash scope process to ensure that monitors get recreated
  395. syn_test_suite_helper:kill_process(syn_registry_scope_ab),
  396. syn_test_suite_helper:wait_process_name_ready(syn_registry_scope_ab),
  397. %% kill process
  398. syn_test_suite_helper:kill_process(Pid),
  399. syn_test_suite_helper:kill_process(PidWithMeta),
  400. %% unregister processes
  401. {error, undefined} = catch syn:unregister(scope_ab, <<"my proc with meta">>),
  402. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:unregister(scope_bc, <<"my proc with meta">>),
  403. ok = rpc:call(SlaveNode1, syn, unregister, [scope_bc, {remote_scoped_bc}]),
  404. %% retrieve
  405. syn_test_suite_helper:assert_wait(
  406. undefined,
  407. fun() -> syn:lookup(scope_ab, "scope_a") end
  408. ),
  409. syn_test_suite_helper:assert_wait(
  410. undefined,
  411. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_ab, "scope_a"]) end
  412. ),
  413. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [scope_ab, "scope_a"]),
  414. syn_test_suite_helper:assert_wait(
  415. undefined,
  416. fun() -> syn:lookup(scope_ab, "scope_a_alias") end
  417. ),
  418. syn_test_suite_helper:assert_wait(
  419. undefined,
  420. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_ab, "scope_a_alias"]) end
  421. ),
  422. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, lookup, [scope_ab, "scope_a_alias"]),
  423. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:lookup(scope_bc, {remote_scoped_bc}),
  424. syn_test_suite_helper:assert_wait(
  425. undefined,
  426. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, {remote_scoped_bc}]) end
  427. ),
  428. syn_test_suite_helper:assert_wait(
  429. undefined,
  430. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, {remote_scoped_bc}]) end
  431. ),
  432. 0 = syn:registry_count(scope_ab),
  433. 0 = syn:registry_count(scope_ab, node()),
  434. 0 = syn:registry_count(scope_ab, SlaveNode1),
  435. 0 = syn:registry_count(scope_ab, SlaveNode2),
  436. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc),
  437. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc, node()),
  438. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc, SlaveNode1),
  439. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc, SlaveNode2),
  440. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_ab]),
  441. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_ab, node()]),
  442. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_ab, SlaveNode1]),
  443. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_ab, SlaveNode2]),
  444. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc]),
  445. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, node()]),
  446. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode1]),
  447. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode2]),
  448. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [scope_ab]),
  449. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [scope_ab, node()]),
  450. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [scope_ab, SlaveNode1]),
  451. {badrpc, {'EXIT', {{invalid_scope, scope_ab}, _}}} = catch rpc:call(SlaveNode2, syn, registry_count, [scope_ab, SlaveNode2]),
  452. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc]),
  453. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, node()]),
  454. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode1]),
  455. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode2]),
  456. %% errors
  457. {error, undefined} = syn:unregister(scope_ab, {invalid_name}),
  458. %% (simulate race condition)
  459. Pid1 = syn_test_suite_helper:start_process(),
  460. Pid2 = syn_test_suite_helper:start_process(),
  461. ok = syn:register(scope_ab, <<"my proc">>, Pid1),
  462. syn_test_suite_helper:assert_wait(
  463. {Pid1, undefined},
  464. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_ab, <<"my proc">>]) end
  465. ),
  466. remove_from_local_table(scope_ab, <<"my proc">>, Pid1),
  467. add_to_local_table(scope_ab, <<"my proc">>, Pid2, undefined, 0, undefined),
  468. {error, race_condition} = rpc:call(SlaveNode1, syn, unregister, [scope_ab, <<"my proc">>]).
  469. three_nodes_cluster_changes(Config) ->
  470. %% get slaves
  471. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  472. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  473. %% disconnect 1 from 2
  474. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  475. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  476. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  477. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  478. %% start syn on 1 and 2, nodes don't know of each other
  479. ok = rpc:call(SlaveNode1, syn, start, []),
  480. ok = rpc:call(SlaveNode2, syn, start, []),
  481. %% add scopes
  482. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[scope_all, scope_bc]]),
  483. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[scope_all, scope_bc]]),
  484. %% start processes
  485. PidRemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  486. PidRemoteOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  487. %% register
  488. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-1", PidRemoteOn1, "meta-1"]),
  489. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-2", PidRemoteOn2, "meta-2"]),
  490. ok = rpc:call(SlaveNode1, syn, register, [scope_bc, "BC-proc-1", PidRemoteOn1, "meta-1"]),
  491. ok = rpc:call(SlaveNode1, syn, register, [scope_bc, "BC-proc-1 alias", PidRemoteOn1, "meta-1 alias"]),
  492. %% form full cluster
  493. ok = syn:start(),
  494. ok = syn:add_node_to_scopes([scope_all]),
  495. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  496. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  497. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  498. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  499. %% retrieve
  500. syn_test_suite_helper:assert_wait(
  501. {PidRemoteOn1, "meta-1"},
  502. fun() -> syn:lookup(scope_all, "proc-1") end
  503. ),
  504. syn_test_suite_helper:assert_wait(
  505. {PidRemoteOn1, "meta-1"},
  506. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-1"]) end
  507. ),
  508. syn_test_suite_helper:assert_wait(
  509. {PidRemoteOn1, "meta-1"},
  510. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-1"]) end
  511. ),
  512. syn_test_suite_helper:assert_wait(
  513. {PidRemoteOn2, "meta-2"},
  514. fun() -> syn:lookup(scope_all, "proc-2") end
  515. ),
  516. syn_test_suite_helper:assert_wait(
  517. {PidRemoteOn2, "meta-2"},
  518. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-2"]) end
  519. ),
  520. syn_test_suite_helper:assert_wait(
  521. {PidRemoteOn2, "meta-2"},
  522. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-2"]) end
  523. ),
  524. 2 = syn:registry_count(scope_all),
  525. 0 = syn:registry_count(scope_all, node()),
  526. 1 = syn:registry_count(scope_all, SlaveNode1),
  527. 1 = syn:registry_count(scope_all, SlaveNode2),
  528. 2 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  529. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  530. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  531. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  532. 2 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  533. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  534. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  535. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  536. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:lookup(scope_bc, "BC-proc-1"),
  537. syn_test_suite_helper:assert_wait(
  538. {PidRemoteOn1, "meta-1"},
  539. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "BC-proc-1"]) end
  540. ),
  541. syn_test_suite_helper:assert_wait(
  542. {PidRemoteOn1, "meta-1"},
  543. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "BC-proc-1"]) end
  544. ),
  545. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:lookup(scope_bc, "BC-proc-1 alias"),
  546. syn_test_suite_helper:assert_wait(
  547. {PidRemoteOn1, "meta-1 alias"},
  548. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "BC-proc-1 alias"]) end
  549. ),
  550. syn_test_suite_helper:assert_wait(
  551. {PidRemoteOn1, "meta-1 alias"},
  552. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "BC-proc-1 alias"]) end
  553. ),
  554. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc),
  555. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc, node()),
  556. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc, SlaveNode1),
  557. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc, SlaveNode2),
  558. 2 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc]),
  559. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, node()]),
  560. 2 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode1]),
  561. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode2]),
  562. 2 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc]),
  563. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, node()]),
  564. 2 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode1]),
  565. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode2]),
  566. %% partial netsplit (1 cannot see 2)
  567. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  568. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  569. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  570. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  571. %% retrieve
  572. syn_test_suite_helper:assert_wait(
  573. {PidRemoteOn1, "meta-1"},
  574. fun() -> syn:lookup(scope_all, "proc-1") end
  575. ),
  576. syn_test_suite_helper:assert_wait(
  577. {PidRemoteOn1, "meta-1"},
  578. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-1"]) end
  579. ),
  580. syn_test_suite_helper:assert_wait(
  581. undefined,
  582. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-1"]) end
  583. ),
  584. syn_test_suite_helper:assert_wait(
  585. {PidRemoteOn2, "meta-2"},
  586. fun() -> syn:lookup(scope_all, "proc-2") end
  587. ),
  588. syn_test_suite_helper:assert_wait(
  589. undefined,
  590. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-2"]) end
  591. ),
  592. syn_test_suite_helper:assert_wait(
  593. {PidRemoteOn2, "meta-2"},
  594. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-2"]) end
  595. ),
  596. 2 = syn:registry_count(scope_all),
  597. 0 = syn:registry_count(scope_all, node()),
  598. 1 = syn:registry_count(scope_all, SlaveNode1),
  599. 1 = syn:registry_count(scope_all, SlaveNode2),
  600. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  601. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  602. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  603. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  604. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  605. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  606. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  607. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  608. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:lookup(scope_bc, "BC-proc-1"),
  609. syn_test_suite_helper:assert_wait(
  610. {PidRemoteOn1, "meta-1"},
  611. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "BC-proc-1"]) end
  612. ),
  613. syn_test_suite_helper:assert_wait(
  614. undefined,
  615. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "BC-proc-1"]) end
  616. ),
  617. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:lookup(scope_bc, "BC-proc-1 alias"),
  618. syn_test_suite_helper:assert_wait(
  619. {PidRemoteOn1, "meta-1 alias"},
  620. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "BC-proc-1 alias"]) end
  621. ),
  622. syn_test_suite_helper:assert_wait(
  623. undefined,
  624. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "BC-proc-1 alias"]) end
  625. ),
  626. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc),
  627. 2 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc]),
  628. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, node()]),
  629. 2 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode1]),
  630. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode2]),
  631. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc]),
  632. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, node()]),
  633. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode1]),
  634. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode2]),
  635. %% re-join
  636. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  637. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  638. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  639. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  640. %% retrieve
  641. syn_test_suite_helper:assert_wait(
  642. {PidRemoteOn1, "meta-1"},
  643. fun() -> syn:lookup(scope_all, "proc-1") end
  644. ),
  645. syn_test_suite_helper:assert_wait(
  646. {PidRemoteOn1, "meta-1"},
  647. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-1"]) end
  648. ),
  649. syn_test_suite_helper:assert_wait(
  650. {PidRemoteOn1, "meta-1"},
  651. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-1"]) end
  652. ),
  653. syn_test_suite_helper:assert_wait(
  654. {PidRemoteOn2, "meta-2"},
  655. fun() -> syn:lookup(scope_all, "proc-2") end
  656. ),
  657. syn_test_suite_helper:assert_wait(
  658. {PidRemoteOn2, "meta-2"},
  659. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-2"]) end
  660. ),
  661. syn_test_suite_helper:assert_wait(
  662. {PidRemoteOn2, "meta-2"},
  663. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-2"]) end
  664. ),
  665. 2 = syn:registry_count(scope_all),
  666. 0 = syn:registry_count(scope_all, node()),
  667. 1 = syn:registry_count(scope_all, SlaveNode1),
  668. 1 = syn:registry_count(scope_all, SlaveNode2),
  669. 2 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  670. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  671. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  672. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  673. 2 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  674. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  675. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  676. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  677. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:lookup(scope_bc, "BC-proc-1"),
  678. syn_test_suite_helper:assert_wait(
  679. {PidRemoteOn1, "meta-1"},
  680. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "BC-proc-1"]) end
  681. ),
  682. syn_test_suite_helper:assert_wait(
  683. {PidRemoteOn1, "meta-1"},
  684. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "BC-proc-1"]) end
  685. ),
  686. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:lookup(scope_bc, "BC-proc-1 alias"),
  687. syn_test_suite_helper:assert_wait(
  688. {PidRemoteOn1, "meta-1 alias"},
  689. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "BC-proc-1 alias"]) end
  690. ),
  691. syn_test_suite_helper:assert_wait(
  692. {PidRemoteOn1, "meta-1 alias"},
  693. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "BC-proc-1 alias"]) end
  694. ),
  695. {'EXIT', {{invalid_scope, scope_bc}, _}} = catch syn:registry_count(scope_bc),
  696. 2 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc]),
  697. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, node()]),
  698. 2 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode1]),
  699. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode2]),
  700. 2 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc]),
  701. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, node()]),
  702. 2 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode1]),
  703. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode2]).
  704. three_nodes_cluster_conflicts(Config) ->
  705. %% get slaves
  706. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  707. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  708. %% start syn on nodes
  709. ok = syn:start(),
  710. ok = rpc:call(SlaveNode1, syn, start, []),
  711. ok = rpc:call(SlaveNode2, syn, start, []),
  712. %% add scopes
  713. ok = syn:add_node_to_scopes([scope_all]),
  714. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[scope_all, scope_bc]]),
  715. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[scope_all, scope_bc]]),
  716. %% partial netsplit (1 cannot see 2)
  717. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  718. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  719. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  720. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  721. %% start conflict processes
  722. Pid2RemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  723. Pid2RemoteOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  724. %% --> conflict by netsplit
  725. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict-by-netsplit", Pid2RemoteOn1, "meta-1"]),
  726. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict-by-netsplit", Pid2RemoteOn2, "meta-2"]),
  727. ok = rpc:call(SlaveNode1, syn, register, [scope_bc, "proc-confict-by-netsplit-scoped", Pid2RemoteOn1, "meta-1"]),
  728. ok = rpc:call(SlaveNode2, syn, register, [scope_bc, "proc-confict-by-netsplit-scoped", Pid2RemoteOn2, "meta-2"]),
  729. %% re-join
  730. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  731. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  732. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  733. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  734. %% retrieve
  735. syn_test_suite_helper:assert_wait(
  736. {Pid2RemoteOn2, "meta-2"},
  737. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit") end
  738. ),
  739. syn_test_suite_helper:assert_wait(
  740. {Pid2RemoteOn2, "meta-2"},
  741. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit"]) end
  742. ),
  743. syn_test_suite_helper:assert_wait(
  744. {Pid2RemoteOn2, "meta-2"},
  745. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit"]) end
  746. ),
  747. 1 = syn:registry_count(scope_all),
  748. 0 = syn:registry_count(scope_all, node()),
  749. 0 = syn:registry_count(scope_all, SlaveNode1),
  750. 1 = syn:registry_count(scope_all, SlaveNode2),
  751. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  752. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  753. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  754. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  755. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  756. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  757. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  758. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  759. syn_test_suite_helper:assert_wait(
  760. {Pid2RemoteOn2, "meta-2"},
  761. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped"]) end
  762. ),
  763. syn_test_suite_helper:assert_wait(
  764. {Pid2RemoteOn2, "meta-2"},
  765. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped"]) end
  766. ),
  767. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc]),
  768. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, node()]),
  769. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode1]),
  770. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode2]),
  771. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc]),
  772. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, node()]),
  773. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode1]),
  774. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode2]),
  775. %% process alive
  776. syn_test_suite_helper:assert_wait(
  777. false,
  778. fun() -> rpc:call(SlaveNode1, erlang, is_process_alive, [Pid2RemoteOn1]) end
  779. ),
  780. syn_test_suite_helper:assert_wait(
  781. true,
  782. fun() -> rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2RemoteOn2]) end
  783. ),
  784. %% --> conflict by race condition
  785. PidOnMaster = syn_test_suite_helper:start_process(),
  786. PidOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  787. rpc:call(SlaveNode1, syn_registry, add_to_local_table,
  788. [default, <<"my proc">>, PidOn1, "meta-2", erlang:system_time(), undefined]
  789. ),
  790. ok = syn:register(scope_all, <<"my proc">>, PidOnMaster, "meta-1"),
  791. %% retrieve
  792. syn_test_suite_helper:assert_wait(
  793. {PidOnMaster, "meta-1"},
  794. fun() -> syn:lookup(scope_all, <<"my proc">>) end
  795. ),
  796. syn_test_suite_helper:assert_wait(
  797. {PidOnMaster, "meta-1"},
  798. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, <<"my proc">>]) end
  799. ),
  800. syn_test_suite_helper:assert_wait(
  801. {PidOnMaster, "meta-1"},
  802. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, <<"my proc">>]) end
  803. ).
  804. %% NB: we can't check for process alive here because we injected the conflicting process in the DB
  805. %% -> it's not actually monitored
  806. three_nodes_custom_event_handler_reg_unreg(Config) ->
  807. %% get slaves
  808. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  809. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  810. %% add custom handler for callbacks
  811. syn:set_event_handler(syn_test_event_handler_callbacks),
  812. rpc:call(SlaveNode1, syn, set_event_handler, [syn_test_event_handler_callbacks]),
  813. rpc:call(SlaveNode2, syn, set_event_handler, [syn_test_event_handler_callbacks]),
  814. %% start syn on nodes
  815. ok = syn:start(),
  816. ok = rpc:call(SlaveNode1, syn, start, []),
  817. ok = rpc:call(SlaveNode2, syn, start, []),
  818. %% add scopes
  819. ok = syn:add_node_to_scopes([scope_all]),
  820. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[scope_all]]),
  821. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[scope_all]]),
  822. %% init
  823. CurrentNode = node(),
  824. %% start process
  825. Pid = syn_test_suite_helper:start_process(),
  826. Pid2 = syn_test_suite_helper:start_process(),
  827. %% ---> on registration
  828. ok = syn:register(scope_all, "proc-handler", Pid, {recipient, self(), <<"meta">>}),
  829. %% check callbacks called
  830. syn_test_suite_helper:assert_received_messages([
  831. {on_process_registered, CurrentNode, scope_all, "proc-handler", Pid, <<"meta">>, normal},
  832. {on_process_registered, SlaveNode1, scope_all, "proc-handler", Pid, <<"meta">>, normal},
  833. {on_process_registered, SlaveNode2, scope_all, "proc-handler", Pid, <<"meta">>, normal}
  834. ]),
  835. syn_test_suite_helper:assert_empty_queue(self()),
  836. %% registration from another node
  837. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-handler-2", Pid2, {recipient, self(), <<"meta-for-2">>}]),
  838. %% check callbacks called
  839. syn_test_suite_helper:assert_received_messages([
  840. {on_process_registered, CurrentNode, scope_all, "proc-handler-2", Pid2, <<"meta-for-2">>, normal},
  841. {on_process_registered, SlaveNode1, scope_all, "proc-handler-2", Pid2, <<"meta-for-2">>, normal},
  842. {on_process_registered, SlaveNode2, scope_all, "proc-handler-2", Pid2, <<"meta-for-2">>, normal}
  843. ]),
  844. syn_test_suite_helper:assert_empty_queue(self()),
  845. %% ---> on meta update
  846. ok = syn:register(scope_all, "proc-handler", Pid, {recipient, self(), <<"new-meta">>}),
  847. %% check callbacks called
  848. syn_test_suite_helper:assert_received_messages([
  849. {on_registry_process_updated, CurrentNode, scope_all, "proc-handler", Pid, <<"new-meta">>, normal},
  850. {on_registry_process_updated, SlaveNode1, scope_all, "proc-handler", Pid, <<"new-meta">>, normal},
  851. {on_registry_process_updated, SlaveNode2, scope_all, "proc-handler", Pid, <<"new-meta">>, normal}
  852. ]),
  853. syn_test_suite_helper:assert_empty_queue(self()),
  854. %% meta update from another node
  855. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-handler-2", Pid2, {recipient, self(), <<"meta-for-2-update">>}]),
  856. %% check callbacks called
  857. syn_test_suite_helper:assert_received_messages([
  858. {on_registry_process_updated, CurrentNode, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal},
  859. {on_registry_process_updated, SlaveNode1, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal},
  860. {on_registry_process_updated, SlaveNode2, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal}
  861. ]),
  862. syn_test_suite_helper:assert_empty_queue(self()),
  863. %% ---> on unregister
  864. ok = syn:unregister(scope_all, "proc-handler"),
  865. %% check callbacks called
  866. syn_test_suite_helper:assert_received_messages([
  867. {on_process_unregistered, CurrentNode, scope_all, "proc-handler", Pid, <<"new-meta">>, normal},
  868. {on_process_unregistered, SlaveNode1, scope_all, "proc-handler", Pid, <<"new-meta">>, normal},
  869. {on_process_unregistered, SlaveNode2, scope_all, "proc-handler", Pid, <<"new-meta">>, normal}
  870. ]),
  871. syn_test_suite_helper:assert_empty_queue(self()),
  872. %% unregister from another node
  873. ok = rpc:call(SlaveNode1, syn, unregister, [scope_all, "proc-handler-2"]),
  874. %% check callbacks called
  875. syn_test_suite_helper:assert_received_messages([
  876. {on_process_unregistered, CurrentNode, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal},
  877. {on_process_unregistered, SlaveNode1, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal},
  878. {on_process_unregistered, SlaveNode2, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal}
  879. ]),
  880. syn_test_suite_helper:assert_empty_queue(self()),
  881. %% clean & check
  882. syn_test_suite_helper:kill_process(Pid),
  883. syn_test_suite_helper:assert_empty_queue(self()),
  884. %% ---> after a netsplit
  885. PidRemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  886. syn:register(scope_all, remote_on_1, PidRemoteOn1, {recipient, self(), <<"netsplit">>}),
  887. %% check callbacks called
  888. syn_test_suite_helper:assert_received_messages([
  889. {on_process_registered, CurrentNode, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, normal},
  890. {on_process_registered, SlaveNode1, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, normal},
  891. {on_process_registered, SlaveNode2, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, normal}
  892. ]),
  893. syn_test_suite_helper:assert_empty_queue(self()),
  894. %% partial netsplit (1 cannot see 2)
  895. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  896. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  897. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  898. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  899. %% check callbacks called
  900. syn_test_suite_helper:assert_received_messages([
  901. {on_process_unregistered, SlaveNode2, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, {syn_remote_scope_node_down, scope_all, SlaveNode1}}
  902. ]),
  903. syn_test_suite_helper:assert_empty_queue(self()),
  904. %% ---> after a re-join
  905. %% re-join
  906. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  907. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  908. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  909. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  910. %% check callbacks called
  911. syn_test_suite_helper:assert_received_messages([
  912. {on_process_registered, SlaveNode2, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, {syn_remote_scope_node_up, scope_all, SlaveNode1}}
  913. ]),
  914. syn_test_suite_helper:assert_empty_queue(self()),
  915. %% clean
  916. syn_test_suite_helper:kill_process(PidRemoteOn1),
  917. %% check callbacks called
  918. syn_test_suite_helper:assert_received_messages([
  919. {on_process_unregistered, CurrentNode, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, killed},
  920. {on_process_unregistered, SlaveNode1, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, killed},
  921. {on_process_unregistered, SlaveNode2, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, killed}
  922. ]),
  923. syn_test_suite_helper:assert_empty_queue(self()),
  924. %% ---> after a conflict resolution
  925. %% partial netsplit (1 cannot see 2)
  926. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  927. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  928. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  929. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  930. %% start conflict processes
  931. Pid2RemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  932. Pid2RemoteOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  933. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict", Pid2RemoteOn1, {recipient, self(), <<"meta-1">>}]),
  934. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict", Pid2RemoteOn2, {recipient, self(), <<"meta-2">>}]),
  935. %% check callbacks called
  936. syn_test_suite_helper:assert_received_messages([
  937. {on_process_registered, CurrentNode, scope_all, "proc-confict", Pid2RemoteOn1, <<"meta-1">>, normal},
  938. {on_process_unregistered, CurrentNode, scope_all, "proc-confict", Pid2RemoteOn1, <<"meta-1">>, normal},
  939. {on_process_registered, CurrentNode, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, normal},
  940. {on_process_registered, SlaveNode1, scope_all, "proc-confict", Pid2RemoteOn1, <<"meta-1">>, normal},
  941. {on_process_registered, SlaveNode2, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, normal}
  942. ]),
  943. syn_test_suite_helper:assert_empty_queue(self()),
  944. %% re-join
  945. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  946. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  947. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  948. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  949. %% check callbacks called
  950. syn_test_suite_helper:assert_received_messages([
  951. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict", Pid2RemoteOn1, <<"meta-1">>, syn_conflict_resolution},
  952. {on_process_registered, SlaveNode1, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, syn_conflict_resolution}
  953. ]),
  954. syn_test_suite_helper:assert_empty_queue(self()),
  955. %% kill
  956. syn_test_suite_helper:kill_process(Pid2RemoteOn1),
  957. syn_test_suite_helper:kill_process(Pid2RemoteOn2),
  958. %% check callbacks called
  959. syn_test_suite_helper:assert_received_messages([
  960. {on_process_unregistered, CurrentNode, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, killed},
  961. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, killed},
  962. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, killed}
  963. ]),
  964. syn_test_suite_helper:assert_empty_queue(self()),
  965. %% ---> don't call on monitor rebuild
  966. %% crash the scope process on local
  967. syn_test_suite_helper:kill_process(syn_registry_scope_all),
  968. %% no messages
  969. syn_test_suite_helper:assert_wait(
  970. ok,
  971. fun() -> syn_test_suite_helper:assert_empty_queue(self()) end
  972. ).
  973. three_nodes_custom_event_handler_conflict_resolution(Config) ->
  974. %% get slaves
  975. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  976. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  977. %% add custom handler for resolution
  978. syn:set_event_handler(syn_test_event_handler_resolution),
  979. rpc:call(SlaveNode1, syn, set_event_handler, [syn_test_event_handler_resolution]),
  980. rpc:call(SlaveNode2, syn, set_event_handler, [syn_test_event_handler_resolution]),
  981. %% start syn on nodes
  982. ok = syn:start(),
  983. ok = rpc:call(SlaveNode1, syn, start, []),
  984. ok = rpc:call(SlaveNode2, syn, start, []),
  985. %% add scopes
  986. ok = syn:add_node_to_scopes([scope_all]),
  987. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[scope_all, scope_bc]]),
  988. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[scope_all, scope_bc]]),
  989. %% current node
  990. TestPid = self(),
  991. CurrentNode = node(),
  992. %% partial netsplit (1 cannot see 2)
  993. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  994. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  995. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  996. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  997. %% start conflict processes
  998. PidOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  999. PidOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  1000. %% --> conflict by netsplit
  1001. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict-by-netsplit-custom", PidOn1, {recipient, TestPid, keepthis}]),
  1002. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict-by-netsplit-custom", PidOn2, {recipient, TestPid, "meta-2"}]),
  1003. ok = rpc:call(SlaveNode1, syn, register, [scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, {recipient, TestPid, keepthis}]),
  1004. ok = rpc:call(SlaveNode2, syn, register, [scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn2, {recipient, TestPid, "meta-2"}]),
  1005. %% check callbacks
  1006. syn_test_suite_helper:assert_received_messages([
  1007. {on_process_registered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1008. {on_process_unregistered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1009. {on_process_registered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom", PidOn2, "meta-2", normal},
  1010. {on_process_registered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1011. {on_process_registered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom", PidOn2, "meta-2", normal},
  1012. {on_process_registered, SlaveNode1, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, keepthis, normal},
  1013. {on_process_registered, SlaveNode2, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn2, "meta-2", normal}
  1014. ]),
  1015. syn_test_suite_helper:assert_empty_queue(self()),
  1016. %% re-join
  1017. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  1018. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1019. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  1020. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  1021. %% retrieve
  1022. syn_test_suite_helper:assert_wait(
  1023. {PidOn1, {recipient, TestPid, keepthis}},
  1024. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit-custom") end
  1025. ),
  1026. syn_test_suite_helper:assert_wait(
  1027. {PidOn1, {recipient, TestPid, keepthis}},
  1028. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom"]) end
  1029. ),
  1030. syn_test_suite_helper:assert_wait(
  1031. {PidOn1, {recipient, TestPid, keepthis}},
  1032. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom"]) end
  1033. ),
  1034. 1 = syn:registry_count(scope_all),
  1035. 0 = syn:registry_count(scope_all, node()),
  1036. 1 = syn:registry_count(scope_all, SlaveNode1),
  1037. 0 = syn:registry_count(scope_all, SlaveNode2),
  1038. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  1039. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  1040. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  1041. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  1042. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  1043. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  1044. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  1045. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  1046. syn_test_suite_helper:assert_wait(
  1047. {PidOn1, {recipient, TestPid, keepthis}},
  1048. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]) end
  1049. ),
  1050. syn_test_suite_helper:assert_wait(
  1051. {PidOn1, {recipient, TestPid, keepthis}},
  1052. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]) end
  1053. ),
  1054. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc]),
  1055. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, node()]),
  1056. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode1]),
  1057. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode2]),
  1058. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc]),
  1059. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, node()]),
  1060. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode1]),
  1061. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode2]),
  1062. syn_test_suite_helper:assert_received_messages([
  1063. {on_process_unregistered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom", PidOn2, "meta-2", syn_conflict_resolution},
  1064. {on_process_registered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, syn_conflict_resolution},
  1065. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom", PidOn2, "meta-2", syn_conflict_resolution},
  1066. {on_process_registered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, syn_conflict_resolution},
  1067. {on_process_unregistered, SlaveNode2, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn2, "meta-2", syn_conflict_resolution},
  1068. {on_process_registered, SlaveNode2, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, keepthis, syn_conflict_resolution}
  1069. ]),
  1070. syn_test_suite_helper:assert_empty_queue(self()),
  1071. %% process alive (discarded process does not get killed with a custom handler)
  1072. syn_test_suite_helper:assert_wait(
  1073. true,
  1074. fun() -> rpc:call(SlaveNode1, erlang, is_process_alive, [PidOn1]) end
  1075. ),
  1076. syn_test_suite_helper:assert_wait(
  1077. true,
  1078. fun() -> rpc:call(SlaveNode2, erlang, is_process_alive, [PidOn2]) end
  1079. ),
  1080. %% clean up default scope
  1081. syn:unregister(scope_all, "proc-confict-by-netsplit-custom"),
  1082. ok = rpc:call(SlaveNode1, syn, unregister, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]),
  1083. %% retrieve
  1084. syn_test_suite_helper:assert_wait(
  1085. undefined,
  1086. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit-custom") end
  1087. ),
  1088. syn_test_suite_helper:assert_wait(
  1089. undefined,
  1090. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom"]) end
  1091. ),
  1092. syn_test_suite_helper:assert_wait(
  1093. undefined,
  1094. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom"]) end
  1095. ),
  1096. syn_test_suite_helper:assert_wait(
  1097. undefined,
  1098. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]) end
  1099. ),
  1100. syn_test_suite_helper:assert_wait(
  1101. undefined,
  1102. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]) end
  1103. ),
  1104. %% check callbacks
  1105. syn_test_suite_helper:assert_received_messages([
  1106. {on_process_unregistered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1107. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1108. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1109. {on_process_unregistered, SlaveNode1, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, keepthis, normal},
  1110. {on_process_unregistered, SlaveNode2, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, keepthis, normal}
  1111. ]),
  1112. syn_test_suite_helper:assert_empty_queue(self()),
  1113. %% --> conflict by netsplit, which returns invalid pid
  1114. %% partial netsplit (1 cannot see 2)
  1115. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  1116. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1117. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  1118. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  1119. %% register with meta with no 'keepthis' element
  1120. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, {recipient, TestPid, "meta-1"}]),
  1121. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, {recipient, TestPid, "meta-2"}]),
  1122. %% check callbacks
  1123. syn_test_suite_helper:assert_received_messages([
  1124. {on_process_registered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, "meta-1", normal},
  1125. {on_process_unregistered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, "meta-1", normal},
  1126. {on_process_registered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, "meta-2", normal},
  1127. {on_process_registered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, "meta-1", normal},
  1128. {on_process_registered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, "meta-2", normal}
  1129. ]),
  1130. syn_test_suite_helper:assert_empty_queue(self()),
  1131. %% re-join
  1132. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  1133. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1134. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  1135. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  1136. %% retrieve (names get freed)
  1137. syn_test_suite_helper:assert_wait(
  1138. undefined,
  1139. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit-custom-other-pid") end
  1140. ),
  1141. syn_test_suite_helper:assert_wait(
  1142. undefined,
  1143. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom-other-pid"]) end
  1144. ),
  1145. syn_test_suite_helper:assert_wait(
  1146. undefined,
  1147. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom-other-pid"]) end
  1148. ),
  1149. 0 = syn:registry_count(scope_all),
  1150. 0 = syn:registry_count(scope_all, node()),
  1151. 0 = syn:registry_count(scope_all, SlaveNode1),
  1152. 0 = syn:registry_count(scope_all, SlaveNode2),
  1153. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  1154. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  1155. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  1156. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  1157. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  1158. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  1159. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  1160. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  1161. %% check callbacks
  1162. syn_test_suite_helper:assert_received_messages([
  1163. {on_process_unregistered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, "meta-2", syn_conflict_resolution},
  1164. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, "meta-1", syn_conflict_resolution},
  1165. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, "meta-2", syn_conflict_resolution}
  1166. ]),
  1167. syn_test_suite_helper:assert_empty_queue(self()),
  1168. %% process alive (discarded process does not get killed with a custom handler)
  1169. syn_test_suite_helper:assert_wait(
  1170. true,
  1171. fun() -> rpc:call(SlaveNode1, erlang, is_process_alive, [PidOn1]) end
  1172. ),
  1173. syn_test_suite_helper:assert_wait(
  1174. true,
  1175. fun() -> rpc:call(SlaveNode2, erlang, is_process_alive, [PidOn2]) end
  1176. ),
  1177. %% --> conflict by netsplit, which crashes
  1178. %% partial netsplit (1 cannot see 2)
  1179. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  1180. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1181. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  1182. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  1183. %% register with meta with no 'crash' element
  1184. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, {recipient, TestPid, crash}]),
  1185. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, {recipient, TestPid, crash}]),
  1186. %% check callbacks
  1187. syn_test_suite_helper:assert_received_messages([
  1188. {on_process_registered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, crash, normal},
  1189. {on_process_unregistered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, crash, normal},
  1190. {on_process_registered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, crash, normal},
  1191. {on_process_registered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, crash, normal},
  1192. {on_process_registered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, crash, normal}
  1193. ]),
  1194. syn_test_suite_helper:assert_empty_queue(self()),
  1195. %% re-join
  1196. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  1197. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1198. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  1199. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  1200. %% retrieve (names get freed)
  1201. syn_test_suite_helper:assert_wait(
  1202. undefined,
  1203. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit-custom-crash") end
  1204. ),
  1205. syn_test_suite_helper:assert_wait(
  1206. undefined,
  1207. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom-crash"]) end
  1208. ),
  1209. syn_test_suite_helper:assert_wait(
  1210. undefined,
  1211. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom-crash"]) end
  1212. ),
  1213. 0 = syn:registry_count(scope_all),
  1214. 0 = syn:registry_count(scope_all, node()),
  1215. 0 = syn:registry_count(scope_all, SlaveNode1),
  1216. 0 = syn:registry_count(scope_all, SlaveNode2),
  1217. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  1218. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  1219. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  1220. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  1221. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  1222. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  1223. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  1224. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  1225. %% check callbacks
  1226. syn_test_suite_helper:assert_received_messages([
  1227. {on_process_unregistered, CurrentNode, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, crash, syn_conflict_resolution},
  1228. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, crash, syn_conflict_resolution},
  1229. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, crash, syn_conflict_resolution}
  1230. ]),
  1231. syn_test_suite_helper:assert_empty_queue(self()),
  1232. %% process alive (discarded process does not get killed with a custom handler)
  1233. syn_test_suite_helper:assert_wait(
  1234. true,
  1235. fun() -> rpc:call(SlaveNode1, erlang, is_process_alive, [PidOn1]) end
  1236. ),
  1237. syn_test_suite_helper:assert_wait(
  1238. true,
  1239. fun() -> rpc:call(SlaveNode2, erlang, is_process_alive, [PidOn2]) end
  1240. ).
  1241. %% ===================================================================
  1242. %% Internal
  1243. %% ===================================================================
  1244. add_to_local_table(Scope, Name, Pid, Meta, Time, MRef) ->
  1245. TableByName = syn_backbone:get_table_name(syn_registry_by_name, Scope),
  1246. TableByPid = syn_backbone:get_table_name(syn_registry_by_pid, Scope),
  1247. syn_registry:add_to_local_table(Name, Pid, Meta, Time, MRef, TableByName, TableByPid).
  1248. remove_from_local_table(Scope, Name, Pid) ->
  1249. TableByName = syn_backbone:get_table_name(syn_registry_by_name, Scope),
  1250. TableByPid = syn_backbone:get_table_name(syn_registry_by_pid, Scope),
  1251. syn_registry:remove_from_local_table(Name, Pid, TableByName, TableByPid).