syn_registry_SUITE.erl 80 KB

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