syn_registry_SUITE.erl 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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_process_registration},
  64. {group, three_nodes_process_registration},
  65. {group, four_nodes_process_registration}
  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_process_registration, [shuffle], [
  82. one_node_via_register_unregister
  83. ]},
  84. {three_nodes_process_registration, [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_process_registration, [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_process_registration, Config) ->
  121. case syn_test_suite_helper:init_cluster(3) of
  122. {error_initializing_cluster, Other} ->
  123. end_per_group(three_nodes_process_registration, Config),
  124. {skip, Other};
  125. NodesConfig ->
  126. NodesConfig ++ Config
  127. end;
  128. init_per_group(four_nodes_process_registration, Config) ->
  129. case syn_test_suite_helper:init_cluster(4) of
  130. {error_initializing_cluster, Other} ->
  131. end_per_group(four_nodes_process_registration, 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_process_registration, Config) ->
  145. syn_test_suite_helper:end_cluster(3, Config);
  146. end_per_group(four_nodes_process_registration, 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
  841. syn:set_event_handler(syn_test_event_handler_callbacks),
  842. rpc:call(SlaveNode1, syn, set_event_handler, [syn_test_event_handler_callbacks]),
  843. rpc:call(SlaveNode2, syn, set_event_handler, [syn_test_event_handler_callbacks]),
  844. %% start syn on nodes
  845. ok = syn:start(),
  846. ok = rpc:call(SlaveNode1, syn, start, []),
  847. ok = rpc:call(SlaveNode2, syn, start, []),
  848. %% add scopes
  849. ok = syn:add_node_to_scopes([scope_all]),
  850. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[scope_all]]),
  851. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[scope_all]]),
  852. %% init
  853. LocalNode = node(),
  854. %% start process
  855. Pid = syn_test_suite_helper:start_process(),
  856. Pid2 = syn_test_suite_helper:start_process(),
  857. %% ---> on registration
  858. ok = syn:register(scope_all, "proc-handler", Pid, {recipient, self(), <<"meta">>}),
  859. %% check callbacks called
  860. syn_test_suite_helper:assert_received_messages([
  861. {on_process_registered, LocalNode, scope_all, "proc-handler", Pid, <<"meta">>, normal},
  862. {on_process_registered, SlaveNode1, scope_all, "proc-handler", Pid, <<"meta">>, normal},
  863. {on_process_registered, SlaveNode2, scope_all, "proc-handler", Pid, <<"meta">>, normal}
  864. ]),
  865. syn_test_suite_helper:assert_empty_queue(self()),
  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. syn_test_suite_helper:assert_empty_queue(self()),
  875. %% ---> on meta update
  876. ok = syn:register(scope_all, "proc-handler", Pid, {recipient, self(), <<"new-meta">>}),
  877. %% check callbacks called
  878. syn_test_suite_helper:assert_received_messages([
  879. {on_registry_process_updated, LocalNode, scope_all, "proc-handler", Pid, <<"new-meta">>, normal},
  880. {on_registry_process_updated, SlaveNode1, scope_all, "proc-handler", Pid, <<"new-meta">>, normal},
  881. {on_registry_process_updated, SlaveNode2, scope_all, "proc-handler", Pid, <<"new-meta">>, normal}
  882. ]),
  883. syn_test_suite_helper:assert_empty_queue(self()),
  884. %% meta update from another node
  885. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-handler-2", Pid2, {recipient, self(), <<"meta-for-2-update">>}]),
  886. %% check callbacks called
  887. syn_test_suite_helper:assert_received_messages([
  888. {on_registry_process_updated, LocalNode, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal},
  889. {on_registry_process_updated, SlaveNode1, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal},
  890. {on_registry_process_updated, SlaveNode2, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal}
  891. ]),
  892. syn_test_suite_helper:assert_empty_queue(self()),
  893. %% ---> on unregister
  894. ok = syn:unregister(scope_all, "proc-handler"),
  895. %% check callbacks called
  896. syn_test_suite_helper:assert_received_messages([
  897. {on_process_unregistered, LocalNode, scope_all, "proc-handler", Pid, <<"new-meta">>, normal},
  898. {on_process_unregistered, SlaveNode1, scope_all, "proc-handler", Pid, <<"new-meta">>, normal},
  899. {on_process_unregistered, SlaveNode2, scope_all, "proc-handler", Pid, <<"new-meta">>, normal}
  900. ]),
  901. syn_test_suite_helper:assert_empty_queue(self()),
  902. %% unregister from another node
  903. ok = rpc:call(SlaveNode1, syn, unregister, [scope_all, "proc-handler-2"]),
  904. %% check callbacks called
  905. syn_test_suite_helper:assert_received_messages([
  906. {on_process_unregistered, LocalNode, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal},
  907. {on_process_unregistered, SlaveNode1, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal},
  908. {on_process_unregistered, SlaveNode2, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal}
  909. ]),
  910. syn_test_suite_helper:assert_empty_queue(self()),
  911. %% clean & check
  912. syn_test_suite_helper:kill_process(Pid),
  913. syn_test_suite_helper:assert_empty_queue(self()),
  914. %% ---> after a netsplit
  915. PidRemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  916. syn:register(scope_all, remote_on_1, PidRemoteOn1, {recipient, self(), <<"netsplit">>}),
  917. %% check callbacks called
  918. syn_test_suite_helper:assert_received_messages([
  919. {on_process_registered, LocalNode, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, normal},
  920. {on_process_registered, SlaveNode1, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, normal},
  921. {on_process_registered, SlaveNode2, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, normal}
  922. ]),
  923. syn_test_suite_helper:assert_empty_queue(self()),
  924. %% partial netsplit (1 cannot see 2)
  925. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  926. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  927. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  928. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  929. %% check callbacks called
  930. syn_test_suite_helper:assert_received_messages([
  931. {on_process_unregistered, SlaveNode2, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, {syn_remote_scope_node_down, scope_all, SlaveNode1}}
  932. ]),
  933. syn_test_suite_helper:assert_empty_queue(self()),
  934. %% ---> after a re-join
  935. %% re-join
  936. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  937. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  938. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  939. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  940. %% check callbacks called
  941. syn_test_suite_helper:assert_received_messages([
  942. {on_process_registered, SlaveNode2, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, {syn_remote_scope_node_up, scope_all, SlaveNode1}}
  943. ]),
  944. syn_test_suite_helper:assert_empty_queue(self()),
  945. %% clean
  946. syn_test_suite_helper:kill_process(PidRemoteOn1),
  947. %% check callbacks called
  948. syn_test_suite_helper:assert_received_messages([
  949. {on_process_unregistered, LocalNode, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, killed},
  950. {on_process_unregistered, SlaveNode1, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, killed},
  951. {on_process_unregistered, SlaveNode2, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, killed}
  952. ]),
  953. syn_test_suite_helper:assert_empty_queue(self()),
  954. %% ---> after a conflict resolution
  955. %% partial netsplit (1 cannot see 2)
  956. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  957. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  958. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  959. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  960. %% start conflict processes
  961. Pid2RemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  962. Pid2RemoteOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  963. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict", Pid2RemoteOn1, {recipient, self(), <<"meta-1">>}]),
  964. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict", Pid2RemoteOn2, {recipient, self(), <<"meta-2">>}]),
  965. %% check callbacks called
  966. syn_test_suite_helper:assert_received_messages([
  967. {on_process_registered, LocalNode, scope_all, "proc-confict", Pid2RemoteOn1, <<"meta-1">>, normal},
  968. {on_process_unregistered, LocalNode, scope_all, "proc-confict", Pid2RemoteOn1, <<"meta-1">>, normal},
  969. {on_process_registered, LocalNode, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, normal},
  970. {on_process_registered, SlaveNode1, scope_all, "proc-confict", Pid2RemoteOn1, <<"meta-1">>, normal},
  971. {on_process_registered, SlaveNode2, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, normal}
  972. ]),
  973. syn_test_suite_helper:assert_empty_queue(self()),
  974. %% re-join
  975. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  976. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  977. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  978. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  979. %% check callbacks called
  980. syn_test_suite_helper:assert_received_messages([
  981. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict", Pid2RemoteOn1, <<"meta-1">>, syn_conflict_resolution},
  982. {on_process_registered, SlaveNode1, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, syn_conflict_resolution}
  983. ]),
  984. syn_test_suite_helper:assert_empty_queue(self()),
  985. %% kill
  986. syn_test_suite_helper:kill_process(Pid2RemoteOn1),
  987. syn_test_suite_helper:kill_process(Pid2RemoteOn2),
  988. %% check callbacks called
  989. syn_test_suite_helper:assert_received_messages([
  990. {on_process_unregistered, LocalNode, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, killed},
  991. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, killed},
  992. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, killed}
  993. ]),
  994. syn_test_suite_helper:assert_empty_queue(self()),
  995. %% ---> don't call on monitor rebuild
  996. %% crash the scope process on local
  997. syn_test_suite_helper:kill_process(syn_registry_scope_all),
  998. syn_test_suite_helper:wait_process_name_ready(syn_registry_scope_all),
  999. %% no messages
  1000. syn_test_suite_helper:assert_wait(
  1001. ok,
  1002. fun() -> syn_test_suite_helper:assert_empty_queue(self()) end
  1003. ),
  1004. %% ---> call if process died during the scope process crash
  1005. TransientPid = syn_test_suite_helper:start_process(),
  1006. syn:register(scope_all, "transient-pid", TransientPid, {recipient, self(), "transient-meta"}),
  1007. %% check callbacks called
  1008. syn_test_suite_helper:assert_received_messages([
  1009. {on_process_registered, LocalNode, scope_all, "transient-pid", TransientPid, "transient-meta", normal},
  1010. {on_process_registered, SlaveNode1, scope_all, "transient-pid", TransientPid, "transient-meta", normal},
  1011. {on_process_registered, SlaveNode2, scope_all, "transient-pid", TransientPid, "transient-meta", normal}
  1012. ]),
  1013. syn_test_suite_helper:assert_empty_queue(self()),
  1014. %% crash the scope process & transient process on local
  1015. syn_test_suite_helper:kill_process(syn_registry_scope_all),
  1016. syn_test_suite_helper:kill_process(TransientPid),
  1017. %% check callbacks called
  1018. syn_test_suite_helper:assert_received_messages([
  1019. {on_process_unregistered, LocalNode, scope_all, "transient-pid", TransientPid, "transient-meta", undefined},
  1020. {on_process_unregistered, SlaveNode1, scope_all, "transient-pid", TransientPid, "transient-meta", {syn_remote_scope_node_down, scope_all, LocalNode}},
  1021. {on_process_unregistered, SlaveNode2, scope_all, "transient-pid", TransientPid, "transient-meta", {syn_remote_scope_node_down, scope_all, LocalNode}}
  1022. ]),
  1023. syn_test_suite_helper:assert_empty_queue(self()).
  1024. three_nodes_custom_event_handler_conflict_resolution(Config) ->
  1025. %% get slaves
  1026. SlaveNode1 = proplists:get_value(syn_slave_1, Config),
  1027. SlaveNode2 = proplists:get_value(syn_slave_2, Config),
  1028. %% add custom handler for resolution
  1029. syn:set_event_handler(syn_test_event_handler_resolution),
  1030. rpc:call(SlaveNode1, syn, set_event_handler, [syn_test_event_handler_resolution]),
  1031. rpc:call(SlaveNode2, syn, set_event_handler, [syn_test_event_handler_resolution]),
  1032. %% start syn on nodes
  1033. ok = syn:start(),
  1034. ok = rpc:call(SlaveNode1, syn, start, []),
  1035. ok = rpc:call(SlaveNode2, syn, start, []),
  1036. %% add scopes
  1037. ok = syn:add_node_to_scopes([scope_all]),
  1038. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[scope_all, scope_bc]]),
  1039. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[scope_all, scope_bc]]),
  1040. %% current node
  1041. TestPid = self(),
  1042. LocalNode = node(),
  1043. %% partial netsplit (1 cannot see 2)
  1044. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  1045. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1046. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  1047. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  1048. %% start conflict processes
  1049. PidOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  1050. PidOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  1051. %% --> conflict by netsplit
  1052. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict-by-netsplit-custom", PidOn1, {recipient, TestPid, keepthis}]),
  1053. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict-by-netsplit-custom", PidOn2, {recipient, TestPid, "meta-2"}]),
  1054. ok = rpc:call(SlaveNode1, syn, register, [scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, {recipient, TestPid, keepthis}]),
  1055. ok = rpc:call(SlaveNode2, syn, register, [scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn2, {recipient, TestPid, "meta-2"}]),
  1056. %% check callbacks
  1057. syn_test_suite_helper:assert_received_messages([
  1058. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1059. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1060. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom", PidOn2, "meta-2", normal},
  1061. {on_process_registered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1062. {on_process_registered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom", PidOn2, "meta-2", normal},
  1063. {on_process_registered, SlaveNode1, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, keepthis, normal},
  1064. {on_process_registered, SlaveNode2, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn2, "meta-2", normal}
  1065. ]),
  1066. syn_test_suite_helper:assert_empty_queue(self()),
  1067. %% re-join
  1068. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  1069. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1070. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  1071. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  1072. %% retrieve
  1073. syn_test_suite_helper:assert_wait(
  1074. {PidOn1, {recipient, TestPid, keepthis}},
  1075. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit-custom") end
  1076. ),
  1077. syn_test_suite_helper:assert_wait(
  1078. {PidOn1, {recipient, TestPid, keepthis}},
  1079. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom"]) end
  1080. ),
  1081. syn_test_suite_helper:assert_wait(
  1082. {PidOn1, {recipient, TestPid, keepthis}},
  1083. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom"]) end
  1084. ),
  1085. 1 = syn:registry_count(scope_all),
  1086. 0 = syn:registry_count(scope_all, node()),
  1087. 1 = syn:registry_count(scope_all, SlaveNode1),
  1088. 0 = syn:registry_count(scope_all, SlaveNode2),
  1089. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  1090. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  1091. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  1092. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  1093. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  1094. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  1095. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  1096. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  1097. syn_test_suite_helper:assert_wait(
  1098. {PidOn1, {recipient, TestPid, keepthis}},
  1099. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]) end
  1100. ),
  1101. syn_test_suite_helper:assert_wait(
  1102. {PidOn1, {recipient, TestPid, keepthis}},
  1103. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]) end
  1104. ),
  1105. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc]),
  1106. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, node()]),
  1107. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode1]),
  1108. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode2]),
  1109. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc]),
  1110. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, node()]),
  1111. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode1]),
  1112. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode2]),
  1113. syn_test_suite_helper:assert_received_messages([
  1114. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom", PidOn2, "meta-2", syn_conflict_resolution},
  1115. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, syn_conflict_resolution},
  1116. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom", PidOn2, "meta-2", syn_conflict_resolution},
  1117. {on_process_registered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, syn_conflict_resolution},
  1118. {on_process_unregistered, SlaveNode2, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn2, "meta-2", syn_conflict_resolution},
  1119. {on_process_registered, SlaveNode2, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, keepthis, syn_conflict_resolution}
  1120. ]),
  1121. syn_test_suite_helper:assert_empty_queue(self()),
  1122. %% process alive (discarded process does not get killed with a custom handler)
  1123. syn_test_suite_helper:assert_wait(
  1124. true,
  1125. fun() -> rpc:call(SlaveNode1, erlang, is_process_alive, [PidOn1]) end
  1126. ),
  1127. syn_test_suite_helper:assert_wait(
  1128. true,
  1129. fun() -> rpc:call(SlaveNode2, erlang, is_process_alive, [PidOn2]) end
  1130. ),
  1131. %% clean up default scope
  1132. syn:unregister(scope_all, "proc-confict-by-netsplit-custom"),
  1133. ok = rpc:call(SlaveNode1, syn, unregister, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]),
  1134. %% retrieve
  1135. syn_test_suite_helper:assert_wait(
  1136. undefined,
  1137. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit-custom") end
  1138. ),
  1139. syn_test_suite_helper:assert_wait(
  1140. undefined,
  1141. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom"]) end
  1142. ),
  1143. syn_test_suite_helper:assert_wait(
  1144. undefined,
  1145. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom"]) end
  1146. ),
  1147. syn_test_suite_helper:assert_wait(
  1148. undefined,
  1149. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]) end
  1150. ),
  1151. syn_test_suite_helper:assert_wait(
  1152. undefined,
  1153. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]) end
  1154. ),
  1155. %% check callbacks
  1156. syn_test_suite_helper:assert_received_messages([
  1157. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1158. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1159. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1160. {on_process_unregistered, SlaveNode1, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, keepthis, normal},
  1161. {on_process_unregistered, SlaveNode2, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, keepthis, normal}
  1162. ]),
  1163. syn_test_suite_helper:assert_empty_queue(self()),
  1164. %% --> conflict by netsplit, which returns invalid pid
  1165. %% partial netsplit (1 cannot see 2)
  1166. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  1167. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1168. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  1169. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  1170. %% register with meta with no 'keepthis' element
  1171. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, {recipient, TestPid, "meta-1"}]),
  1172. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, {recipient, TestPid, "meta-2"}]),
  1173. %% check callbacks
  1174. syn_test_suite_helper:assert_received_messages([
  1175. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, "meta-1", normal},
  1176. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, "meta-1", normal},
  1177. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, "meta-2", normal},
  1178. {on_process_registered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, "meta-1", normal},
  1179. {on_process_registered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, "meta-2", normal}
  1180. ]),
  1181. syn_test_suite_helper:assert_empty_queue(self()),
  1182. %% re-join
  1183. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  1184. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1185. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  1186. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  1187. %% retrieve (names get freed)
  1188. syn_test_suite_helper:assert_wait(
  1189. undefined,
  1190. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit-custom-other-pid") end
  1191. ),
  1192. syn_test_suite_helper:assert_wait(
  1193. undefined,
  1194. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom-other-pid"]) end
  1195. ),
  1196. syn_test_suite_helper:assert_wait(
  1197. undefined,
  1198. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom-other-pid"]) end
  1199. ),
  1200. 0 = syn:registry_count(scope_all),
  1201. 0 = syn:registry_count(scope_all, node()),
  1202. 0 = syn:registry_count(scope_all, SlaveNode1),
  1203. 0 = syn:registry_count(scope_all, SlaveNode2),
  1204. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  1205. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  1206. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  1207. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  1208. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  1209. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  1210. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  1211. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  1212. %% check callbacks
  1213. syn_test_suite_helper:assert_received_messages([
  1214. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, "meta-2", syn_conflict_resolution},
  1215. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, "meta-1", syn_conflict_resolution},
  1216. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, "meta-2", syn_conflict_resolution}
  1217. ]),
  1218. syn_test_suite_helper:assert_empty_queue(self()),
  1219. %% process alive (discarded process does not get killed with a custom handler)
  1220. syn_test_suite_helper:assert_wait(
  1221. true,
  1222. fun() -> rpc:call(SlaveNode1, erlang, is_process_alive, [PidOn1]) end
  1223. ),
  1224. syn_test_suite_helper:assert_wait(
  1225. true,
  1226. fun() -> rpc:call(SlaveNode2, erlang, is_process_alive, [PidOn2]) end
  1227. ),
  1228. %% --> conflict by netsplit, which crashes
  1229. %% partial netsplit (1 cannot see 2)
  1230. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  1231. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1232. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  1233. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  1234. %% register with meta with no 'crash' element
  1235. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, {recipient, TestPid, crash}]),
  1236. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, {recipient, TestPid, crash}]),
  1237. %% check callbacks
  1238. syn_test_suite_helper:assert_received_messages([
  1239. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, crash, normal},
  1240. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, crash, normal},
  1241. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, crash, normal},
  1242. {on_process_registered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, crash, normal},
  1243. {on_process_registered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, crash, normal}
  1244. ]),
  1245. syn_test_suite_helper:assert_empty_queue(self()),
  1246. %% re-join
  1247. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  1248. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1249. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  1250. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  1251. %% retrieve (names get freed)
  1252. syn_test_suite_helper:assert_wait(
  1253. undefined,
  1254. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit-custom-crash") end
  1255. ),
  1256. syn_test_suite_helper:assert_wait(
  1257. undefined,
  1258. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom-crash"]) end
  1259. ),
  1260. syn_test_suite_helper:assert_wait(
  1261. undefined,
  1262. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom-crash"]) end
  1263. ),
  1264. 0 = syn:registry_count(scope_all),
  1265. 0 = syn:registry_count(scope_all, node()),
  1266. 0 = syn:registry_count(scope_all, SlaveNode1),
  1267. 0 = syn:registry_count(scope_all, SlaveNode2),
  1268. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  1269. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  1270. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  1271. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  1272. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  1273. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  1274. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  1275. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  1276. %% check callbacks
  1277. syn_test_suite_helper:assert_received_messages([
  1278. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, crash, syn_conflict_resolution},
  1279. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, crash, syn_conflict_resolution},
  1280. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, crash, syn_conflict_resolution}
  1281. ]),
  1282. syn_test_suite_helper:assert_empty_queue(self()),
  1283. %% process alive (discarded process does not get killed with a custom handler)
  1284. syn_test_suite_helper:assert_wait(
  1285. true,
  1286. fun() -> rpc:call(SlaveNode1, erlang, is_process_alive, [PidOn1]) end
  1287. ),
  1288. syn_test_suite_helper:assert_wait(
  1289. true,
  1290. fun() -> rpc:call(SlaveNode2, erlang, is_process_alive, [PidOn2]) end
  1291. ).
  1292. four_nodes_concurrency(Config) ->
  1293. %% get slaves
  1294. SlaveNode1 = proplists:get_value(syn_slave_1, Config),
  1295. SlaveNode2 = proplists:get_value(syn_slave_2, Config),
  1296. SlaveNode3 = proplists:get_value(syn_slave_3, Config),
  1297. %% start syn on nodes
  1298. ok = syn:start(),
  1299. ok = rpc:call(SlaveNode1, syn, start, []),
  1300. ok = rpc:call(SlaveNode2, syn, start, []),
  1301. ok = rpc:call(SlaveNode3, syn, start, []),
  1302. %% add scopes
  1303. ok = syn:add_node_to_scopes([scope_all]),
  1304. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[scope_all]]),
  1305. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[scope_all]]),
  1306. ok = rpc:call(SlaveNode3, syn, add_node_to_scopes, [[scope_all]]),
  1307. %% ref
  1308. TestPid = self(),
  1309. Iterations = 250,
  1310. %% concurrent test
  1311. WorkerFun = fun() ->
  1312. lists:foreach(fun(_) ->
  1313. %% start pid
  1314. Pid = syn_test_suite_helper:start_process(),
  1315. %% loop
  1316. case syn:register(scope_all, <<"concurrent">>, Pid) of
  1317. ok ->
  1318. ok;
  1319. {error, taken} ->
  1320. case syn:unregister(scope_all, <<"concurrent">>) of
  1321. {error, undefined} ->
  1322. ok;
  1323. {error, race_condition} ->
  1324. ok;
  1325. ok ->
  1326. syn:register(scope_all, <<"concurrent">>, Pid)
  1327. end
  1328. end,
  1329. RndTime = rand:uniform(30),
  1330. timer:sleep(RndTime)
  1331. end, lists:seq(1, Iterations)),
  1332. TestPid ! {done, node()}
  1333. end,
  1334. %% spawn concurrent
  1335. LocalNode = node(),
  1336. spawn(LocalNode, WorkerFun),
  1337. spawn(SlaveNode1, WorkerFun),
  1338. spawn(SlaveNode2, WorkerFun),
  1339. spawn(SlaveNode3, WorkerFun),
  1340. %% check results are same across network
  1341. syn_test_suite_helper:assert_received_messages([
  1342. {done, LocalNode},
  1343. {done, SlaveNode1},
  1344. {done, SlaveNode2},
  1345. {done, SlaveNode3}
  1346. ]),
  1347. syn_test_suite_helper:assert_wait(
  1348. 1,
  1349. fun() ->
  1350. ResultPidLocal = syn:lookup(scope_all, <<"concurrent">>),
  1351. ResultPidOn1 = rpc:call(SlaveNode1, syn, lookup, [scope_all, <<"concurrent">>]),
  1352. ResultPidOn2 = rpc:call(SlaveNode2, syn, lookup, [scope_all, <<"concurrent">>]),
  1353. ResultPidOn3 = rpc:call(SlaveNode3, syn, lookup, [scope_all, <<"concurrent">>]),
  1354. O = ordsets:from_list([ResultPidLocal, ResultPidOn1, ResultPidOn2, ResultPidOn3]),
  1355. ordsets:size(O)
  1356. end
  1357. ).
  1358. %% ===================================================================
  1359. %% Internal
  1360. %% ===================================================================
  1361. add_to_local_table(Scope, Name, Pid, Meta, Time, MRef) ->
  1362. TableByName = syn_backbone:get_table_name(syn_registry_by_name, Scope),
  1363. TableByPid = syn_backbone:get_table_name(syn_registry_by_pid, Scope),
  1364. syn_registry:add_to_local_table(Name, Pid, Meta, Time, MRef, TableByName, TableByPid).
  1365. remove_from_local_table(Scope, Name, Pid) ->
  1366. TableByName = syn_backbone:get_table_name(syn_registry_by_name, Scope),
  1367. TableByPid = syn_backbone:get_table_name(syn_registry_by_pid, Scope),
  1368. syn_registry:remove_from_local_table(Name, Pid, TableByName, TableByPid).