syn_registry_SUITE.erl 75 KB

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