syn_registry_SUITE.erl 75 KB

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