syn_registry_SUITE.erl 76 KB

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