syn_registry_SUITE.erl 75 KB

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