syn_registry_SUITE.erl 75 KB

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