syn_registry_SUITE.erl 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  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. %% registration from another node
  866. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-handler-2", Pid2, {recipient, self(), <<"meta-for-2">>}]),
  867. %% check callbacks called
  868. syn_test_suite_helper:assert_received_messages([
  869. {on_process_registered, LocalNode, scope_all, "proc-handler-2", Pid2, <<"meta-for-2">>, normal},
  870. {on_process_registered, SlaveNode1, scope_all, "proc-handler-2", Pid2, <<"meta-for-2">>, normal},
  871. {on_process_registered, SlaveNode2, scope_all, "proc-handler-2", Pid2, <<"meta-for-2">>, normal}
  872. ]),
  873. %% ---> on meta update
  874. ok = syn:register(scope_all, "proc-handler", Pid, {recipient, self(), <<"new-meta">>}),
  875. %% check callbacks called
  876. syn_test_suite_helper:assert_received_messages([
  877. {on_registry_process_updated, LocalNode, scope_all, "proc-handler", Pid, <<"new-meta">>, normal},
  878. {on_registry_process_updated, SlaveNode1, scope_all, "proc-handler", Pid, <<"new-meta">>, normal},
  879. {on_registry_process_updated, SlaveNode2, scope_all, "proc-handler", Pid, <<"new-meta">>, normal}
  880. ]),
  881. %% meta update from another node
  882. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-handler-2", Pid2, {recipient, self(), <<"meta-for-2-update">>}]),
  883. %% check callbacks called
  884. syn_test_suite_helper:assert_received_messages([
  885. {on_registry_process_updated, LocalNode, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal},
  886. {on_registry_process_updated, SlaveNode1, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal},
  887. {on_registry_process_updated, SlaveNode2, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal}
  888. ]),
  889. %% ---> on unregister
  890. ok = syn:unregister(scope_all, "proc-handler"),
  891. %% check callbacks called
  892. syn_test_suite_helper:assert_received_messages([
  893. {on_process_unregistered, LocalNode, scope_all, "proc-handler", Pid, <<"new-meta">>, normal},
  894. {on_process_unregistered, SlaveNode1, scope_all, "proc-handler", Pid, <<"new-meta">>, normal},
  895. {on_process_unregistered, SlaveNode2, scope_all, "proc-handler", Pid, <<"new-meta">>, normal}
  896. ]),
  897. %% unregister from another node
  898. ok = rpc:call(SlaveNode1, syn, unregister, [scope_all, "proc-handler-2"]),
  899. %% check callbacks called
  900. syn_test_suite_helper:assert_received_messages([
  901. {on_process_unregistered, LocalNode, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal},
  902. {on_process_unregistered, SlaveNode1, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal},
  903. {on_process_unregistered, SlaveNode2, scope_all, "proc-handler-2", Pid2, <<"meta-for-2-update">>, normal}
  904. ]),
  905. %% clean & check
  906. syn_test_suite_helper:kill_process(Pid),
  907. %% no messages
  908. syn_test_suite_helper:assert_empty_queue(),
  909. %% ---> after a netsplit
  910. PidRemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  911. syn:register(scope_all, remote_on_1, PidRemoteOn1, {recipient, self(), <<"netsplit">>}),
  912. %% check callbacks called
  913. syn_test_suite_helper:assert_received_messages([
  914. {on_process_registered, LocalNode, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, normal},
  915. {on_process_registered, SlaveNode1, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, normal},
  916. {on_process_registered, SlaveNode2, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, normal}
  917. ]),
  918. %% partial netsplit (1 cannot see 2)
  919. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  920. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  921. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  922. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  923. %% check callbacks called
  924. syn_test_suite_helper:assert_received_messages([
  925. {on_process_unregistered, SlaveNode2, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, {syn_remote_scope_node_down, scope_all, SlaveNode1}}
  926. ]),
  927. %% ---> after a re-join
  928. %% re-join
  929. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  930. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  931. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  932. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  933. %% check callbacks called
  934. syn_test_suite_helper:assert_received_messages([
  935. {on_process_registered, SlaveNode2, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, {syn_remote_scope_node_up, scope_all, SlaveNode1}}
  936. ]),
  937. %% clean
  938. syn_test_suite_helper:kill_process(PidRemoteOn1),
  939. %% check callbacks called
  940. syn_test_suite_helper:assert_received_messages([
  941. {on_process_unregistered, LocalNode, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, killed},
  942. {on_process_unregistered, SlaveNode1, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, killed},
  943. {on_process_unregistered, SlaveNode2, scope_all, remote_on_1, PidRemoteOn1, <<"netsplit">>, killed}
  944. ]),
  945. %% ---> after a conflict resolution
  946. %% partial netsplit (1 cannot see 2)
  947. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  948. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  949. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  950. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  951. %% start conflict processes
  952. Pid2RemoteOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  953. Pid2RemoteOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  954. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict", Pid2RemoteOn1, {recipient, self(), <<"meta-1">>}]),
  955. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict", Pid2RemoteOn2, {recipient, self(), <<"meta-2">>}]),
  956. %% check callbacks called
  957. syn_test_suite_helper:assert_received_messages([
  958. {on_process_registered, LocalNode, scope_all, "proc-confict", Pid2RemoteOn1, <<"meta-1">>, normal},
  959. {on_process_unregistered, LocalNode, scope_all, "proc-confict", Pid2RemoteOn1, <<"meta-1">>, normal},
  960. {on_process_registered, LocalNode, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, normal},
  961. {on_process_registered, SlaveNode1, scope_all, "proc-confict", Pid2RemoteOn1, <<"meta-1">>, normal},
  962. {on_process_registered, SlaveNode2, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, normal}
  963. ]),
  964. %% re-join
  965. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  966. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  967. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  968. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  969. %% check callbacks called
  970. syn_test_suite_helper:assert_received_messages([
  971. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict", Pid2RemoteOn1, <<"meta-1">>, syn_conflict_resolution},
  972. {on_process_registered, SlaveNode1, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, syn_conflict_resolution}
  973. ]),
  974. %% kill
  975. syn_test_suite_helper:kill_process(Pid2RemoteOn1),
  976. syn_test_suite_helper:kill_process(Pid2RemoteOn2),
  977. %% check callbacks called
  978. syn_test_suite_helper:assert_received_messages([
  979. {on_process_unregistered, LocalNode, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, killed},
  980. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, killed},
  981. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict", Pid2RemoteOn2, <<"meta-2">>, killed}
  982. ]),
  983. %% ---> don't call on monitor rebuild
  984. %% crash the scope process on local
  985. syn_test_suite_helper:kill_process(syn_registry_scope_all),
  986. syn_test_suite_helper:wait_process_name_ready(syn_registry_scope_all),
  987. %% no messages
  988. syn_test_suite_helper:assert_empty_queue(),
  989. %% ---> call if process died during the scope process crash
  990. TransientPid = syn_test_suite_helper:start_process(),
  991. syn:register(scope_all, "transient-pid", TransientPid, {recipient, self(), "transient-meta"}),
  992. %% check callbacks called
  993. syn_test_suite_helper:assert_received_messages([
  994. {on_process_registered, LocalNode, scope_all, "transient-pid", TransientPid, "transient-meta", normal},
  995. {on_process_registered, SlaveNode1, scope_all, "transient-pid", TransientPid, "transient-meta", normal},
  996. {on_process_registered, SlaveNode2, scope_all, "transient-pid", TransientPid, "transient-meta", normal}
  997. ]),
  998. %% crash the scope process & fake a died process on local
  999. InvalidPid = list_to_pid("<0.9999.0>"),
  1000. add_to_local_table(scope_all, "invalid-pid", InvalidPid, {recipient, self(), "invalid-meta"}, 0, undefined),
  1001. syn_test_suite_helper:kill_process(syn_registry_scope_all),
  1002. %% check callbacks called
  1003. syn_test_suite_helper:assert_received_messages([
  1004. {on_process_unregistered, LocalNode, scope_all, "invalid-pid", InvalidPid, "invalid-meta", undefined},
  1005. {on_process_unregistered, SlaveNode1, scope_all, "transient-pid", TransientPid, "transient-meta", {syn_remote_scope_node_down, scope_all, LocalNode}},
  1006. {on_process_unregistered, SlaveNode2, scope_all, "transient-pid", TransientPid, "transient-meta", {syn_remote_scope_node_down, scope_all, LocalNode}},
  1007. {on_process_registered, SlaveNode1, scope_all, "transient-pid", TransientPid, "transient-meta", {syn_remote_scope_node_up, scope_all, LocalNode}},
  1008. {on_process_registered, SlaveNode2, scope_all, "transient-pid", TransientPid, "transient-meta", {syn_remote_scope_node_up, scope_all, LocalNode}}
  1009. ]).
  1010. three_nodes_custom_event_handler_conflict_resolution(Config) ->
  1011. %% get slaves
  1012. SlaveNode1 = proplists:get_value(syn_slave_1, Config),
  1013. SlaveNode2 = proplists:get_value(syn_slave_2, Config),
  1014. %% add custom handler for resolution
  1015. syn:set_event_handler(syn_test_event_handler_resolution),
  1016. rpc:call(SlaveNode1, syn, set_event_handler, [syn_test_event_handler_resolution]),
  1017. rpc:call(SlaveNode2, syn, set_event_handler, [syn_test_event_handler_resolution]),
  1018. %% start syn on nodes
  1019. ok = syn:start(),
  1020. ok = rpc:call(SlaveNode1, syn, start, []),
  1021. ok = rpc:call(SlaveNode2, syn, start, []),
  1022. %% add scopes
  1023. ok = syn:add_node_to_scopes([scope_all]),
  1024. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[scope_all, scope_bc]]),
  1025. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[scope_all, scope_bc]]),
  1026. %% current node
  1027. TestPid = self(),
  1028. LocalNode = node(),
  1029. %% partial netsplit (1 cannot see 2)
  1030. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  1031. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1032. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  1033. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  1034. %% start conflict processes
  1035. PidOn1 = syn_test_suite_helper:start_process(SlaveNode1),
  1036. PidOn2 = syn_test_suite_helper:start_process(SlaveNode2),
  1037. %% --> conflict by netsplit
  1038. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict-by-netsplit-custom", PidOn1, {recipient, TestPid, keepthis}]),
  1039. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict-by-netsplit-custom", PidOn2, {recipient, TestPid, "meta-2"}]),
  1040. ok = rpc:call(SlaveNode1, syn, register, [scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, {recipient, TestPid, keepthis}]),
  1041. ok = rpc:call(SlaveNode2, syn, register, [scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn2, {recipient, TestPid, "meta-2"}]),
  1042. %% check callbacks
  1043. syn_test_suite_helper:assert_received_messages([
  1044. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1045. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1046. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom", PidOn2, "meta-2", normal},
  1047. {on_process_registered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1048. {on_process_registered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom", PidOn2, "meta-2", normal},
  1049. {on_process_registered, SlaveNode1, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, keepthis, normal},
  1050. {on_process_registered, SlaveNode2, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn2, "meta-2", normal}
  1051. ]),
  1052. %% re-join
  1053. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  1054. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1055. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  1056. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  1057. %% retrieve
  1058. syn_test_suite_helper:assert_wait(
  1059. {PidOn1, {recipient, TestPid, keepthis}},
  1060. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit-custom") end
  1061. ),
  1062. syn_test_suite_helper:assert_wait(
  1063. {PidOn1, {recipient, TestPid, keepthis}},
  1064. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom"]) end
  1065. ),
  1066. syn_test_suite_helper:assert_wait(
  1067. {PidOn1, {recipient, TestPid, keepthis}},
  1068. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom"]) end
  1069. ),
  1070. 1 = syn:registry_count(scope_all),
  1071. 0 = syn:registry_count(scope_all, node()),
  1072. 1 = syn:registry_count(scope_all, SlaveNode1),
  1073. 0 = syn:registry_count(scope_all, SlaveNode2),
  1074. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  1075. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  1076. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  1077. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  1078. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  1079. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  1080. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  1081. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  1082. syn_test_suite_helper:assert_wait(
  1083. {PidOn1, {recipient, TestPid, keepthis}},
  1084. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]) end
  1085. ),
  1086. syn_test_suite_helper:assert_wait(
  1087. {PidOn1, {recipient, TestPid, keepthis}},
  1088. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]) end
  1089. ),
  1090. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc]),
  1091. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, node()]),
  1092. 1 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode1]),
  1093. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_bc, SlaveNode2]),
  1094. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc]),
  1095. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, node()]),
  1096. 1 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode1]),
  1097. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_bc, SlaveNode2]),
  1098. syn_test_suite_helper:assert_received_messages([
  1099. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom", PidOn2, "meta-2", syn_conflict_resolution},
  1100. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, syn_conflict_resolution},
  1101. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom", PidOn2, "meta-2", syn_conflict_resolution},
  1102. {on_process_registered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, syn_conflict_resolution},
  1103. {on_process_unregistered, SlaveNode2, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn2, "meta-2", syn_conflict_resolution},
  1104. {on_process_registered, SlaveNode2, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, keepthis, syn_conflict_resolution}
  1105. ]),
  1106. %% process alive (discarded process does not get killed with a custom handler)
  1107. syn_test_suite_helper:assert_wait(
  1108. true,
  1109. fun() -> rpc:call(SlaveNode1, erlang, is_process_alive, [PidOn1]) end
  1110. ),
  1111. syn_test_suite_helper:assert_wait(
  1112. true,
  1113. fun() -> rpc:call(SlaveNode2, erlang, is_process_alive, [PidOn2]) end
  1114. ),
  1115. %% clean up default scope
  1116. syn:unregister(scope_all, "proc-confict-by-netsplit-custom"),
  1117. ok = rpc:call(SlaveNode1, syn, unregister, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]),
  1118. %% retrieve
  1119. syn_test_suite_helper:assert_wait(
  1120. undefined,
  1121. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit-custom") end
  1122. ),
  1123. syn_test_suite_helper:assert_wait(
  1124. undefined,
  1125. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom"]) end
  1126. ),
  1127. syn_test_suite_helper:assert_wait(
  1128. undefined,
  1129. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom"]) end
  1130. ),
  1131. syn_test_suite_helper:assert_wait(
  1132. undefined,
  1133. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]) end
  1134. ),
  1135. syn_test_suite_helper:assert_wait(
  1136. undefined,
  1137. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_bc, "proc-confict-by-netsplit-scoped-custom"]) end
  1138. ),
  1139. %% check callbacks
  1140. syn_test_suite_helper:assert_received_messages([
  1141. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1142. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1143. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom", PidOn1, keepthis, normal},
  1144. {on_process_unregistered, SlaveNode1, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, keepthis, normal},
  1145. {on_process_unregistered, SlaveNode2, scope_bc, "proc-confict-by-netsplit-scoped-custom", PidOn1, keepthis, normal}
  1146. ]),
  1147. %% --> conflict by netsplit, which returns invalid pid
  1148. %% partial netsplit (1 cannot see 2)
  1149. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  1150. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1151. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  1152. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  1153. %% register with meta with no 'keepthis' element
  1154. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, {recipient, TestPid, "meta-1"}]),
  1155. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, {recipient, TestPid, "meta-2"}]),
  1156. %% check callbacks
  1157. syn_test_suite_helper:assert_received_messages([
  1158. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, "meta-1", normal},
  1159. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, "meta-1", normal},
  1160. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, "meta-2", normal},
  1161. {on_process_registered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, "meta-1", normal},
  1162. {on_process_registered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, "meta-2", normal}
  1163. ]),
  1164. %% re-join
  1165. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  1166. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1167. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  1168. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  1169. %% retrieve (names get freed)
  1170. syn_test_suite_helper:assert_wait(
  1171. undefined,
  1172. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit-custom-other-pid") end
  1173. ),
  1174. syn_test_suite_helper:assert_wait(
  1175. undefined,
  1176. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom-other-pid"]) end
  1177. ),
  1178. syn_test_suite_helper:assert_wait(
  1179. undefined,
  1180. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom-other-pid"]) end
  1181. ),
  1182. 0 = syn:registry_count(scope_all),
  1183. 0 = syn:registry_count(scope_all, node()),
  1184. 0 = syn:registry_count(scope_all, SlaveNode1),
  1185. 0 = syn:registry_count(scope_all, SlaveNode2),
  1186. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  1187. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  1188. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  1189. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  1190. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  1191. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  1192. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  1193. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  1194. %% check callbacks
  1195. syn_test_suite_helper:assert_received_messages([
  1196. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, "meta-2", syn_conflict_resolution},
  1197. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn1, "meta-1", syn_conflict_resolution},
  1198. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom-other-pid", PidOn2, "meta-2", syn_conflict_resolution}
  1199. ]),
  1200. %% process alive (discarded process does not get killed with a custom handler)
  1201. syn_test_suite_helper:assert_wait(
  1202. true,
  1203. fun() -> rpc:call(SlaveNode1, erlang, is_process_alive, [PidOn1]) end
  1204. ),
  1205. syn_test_suite_helper:assert_wait(
  1206. true,
  1207. fun() -> rpc:call(SlaveNode2, erlang, is_process_alive, [PidOn2]) end
  1208. ),
  1209. %% --> conflict by netsplit, which crashes
  1210. %% partial netsplit (1 cannot see 2)
  1211. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  1212. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1213. syn_test_suite_helper:assert_cluster(SlaveNode1, [node()]),
  1214. syn_test_suite_helper:assert_cluster(SlaveNode2, [node()]),
  1215. %% register with meta with no 'crash' element
  1216. ok = rpc:call(SlaveNode1, syn, register, [scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, {recipient, TestPid, crash}]),
  1217. ok = rpc:call(SlaveNode2, syn, register, [scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, {recipient, TestPid, crash}]),
  1218. %% check callbacks
  1219. syn_test_suite_helper:assert_received_messages([
  1220. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, crash, normal},
  1221. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, crash, normal},
  1222. {on_process_registered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, crash, normal},
  1223. {on_process_registered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, crash, normal},
  1224. {on_process_registered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, crash, normal}
  1225. ]),
  1226. %% re-join
  1227. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  1228. syn_test_suite_helper:assert_cluster(node(), [SlaveNode1, SlaveNode2]),
  1229. syn_test_suite_helper:assert_cluster(SlaveNode1, [node(), SlaveNode2]),
  1230. syn_test_suite_helper:assert_cluster(SlaveNode2, [node(), SlaveNode1]),
  1231. %% retrieve (names get freed)
  1232. syn_test_suite_helper:assert_wait(
  1233. undefined,
  1234. fun() -> syn:lookup(scope_all, "proc-confict-by-netsplit-custom-crash") end
  1235. ),
  1236. syn_test_suite_helper:assert_wait(
  1237. undefined,
  1238. fun() -> rpc:call(SlaveNode1, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom-crash"]) end
  1239. ),
  1240. syn_test_suite_helper:assert_wait(
  1241. undefined,
  1242. fun() -> rpc:call(SlaveNode2, syn, lookup, [scope_all, "proc-confict-by-netsplit-custom-crash"]) end
  1243. ),
  1244. 0 = syn:registry_count(scope_all),
  1245. 0 = syn:registry_count(scope_all, node()),
  1246. 0 = syn:registry_count(scope_all, SlaveNode1),
  1247. 0 = syn:registry_count(scope_all, SlaveNode2),
  1248. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all]),
  1249. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, node()]),
  1250. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode1]),
  1251. 0 = rpc:call(SlaveNode1, syn, registry_count, [scope_all, SlaveNode2]),
  1252. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all]),
  1253. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, node()]),
  1254. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode1]),
  1255. 0 = rpc:call(SlaveNode2, syn, registry_count, [scope_all, SlaveNode2]),
  1256. %% check callbacks
  1257. syn_test_suite_helper:assert_received_messages([
  1258. {on_process_unregistered, LocalNode, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, crash, syn_conflict_resolution},
  1259. {on_process_unregistered, SlaveNode1, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn1, crash, syn_conflict_resolution},
  1260. {on_process_unregistered, SlaveNode2, scope_all, "proc-confict-by-netsplit-custom-crash", PidOn2, crash, syn_conflict_resolution}
  1261. ]),
  1262. %% process alive (discarded process does not get killed with a custom handler)
  1263. syn_test_suite_helper:assert_wait(
  1264. true,
  1265. fun() -> rpc:call(SlaveNode1, erlang, is_process_alive, [PidOn1]) end
  1266. ),
  1267. syn_test_suite_helper:assert_wait(
  1268. true,
  1269. fun() -> rpc:call(SlaveNode2, erlang, is_process_alive, [PidOn2]) end
  1270. ).
  1271. four_nodes_concurrency(Config) ->
  1272. %% get slaves
  1273. SlaveNode1 = proplists:get_value(syn_slave_1, Config),
  1274. SlaveNode2 = proplists:get_value(syn_slave_2, Config),
  1275. SlaveNode3 = proplists:get_value(syn_slave_3, Config),
  1276. %% start syn on nodes
  1277. ok = syn:start(),
  1278. ok = rpc:call(SlaveNode1, syn, start, []),
  1279. ok = rpc:call(SlaveNode2, syn, start, []),
  1280. ok = rpc:call(SlaveNode3, syn, start, []),
  1281. %% add scopes
  1282. ok = syn:add_node_to_scopes([scope_all]),
  1283. ok = rpc:call(SlaveNode1, syn, add_node_to_scopes, [[scope_all]]),
  1284. ok = rpc:call(SlaveNode2, syn, add_node_to_scopes, [[scope_all]]),
  1285. ok = rpc:call(SlaveNode3, syn, add_node_to_scopes, [[scope_all]]),
  1286. %% ref
  1287. TestPid = self(),
  1288. Iterations = 250,
  1289. %% concurrent test
  1290. WorkerFun = fun() ->
  1291. lists:foreach(fun(_) ->
  1292. %% start pid
  1293. Pid = syn_test_suite_helper:start_process(),
  1294. RandomMeta = rand:uniform(99999),
  1295. %% loop
  1296. case syn:register(scope_all, <<"concurrent">>, Pid, RandomMeta) of
  1297. ok ->
  1298. ok;
  1299. {error, taken} ->
  1300. case syn:unregister(scope_all, <<"concurrent">>) of
  1301. {error, undefined} ->
  1302. ok;
  1303. {error, race_condition} ->
  1304. ok;
  1305. ok ->
  1306. syn:register(scope_all, <<"concurrent">>, Pid, RandomMeta)
  1307. end
  1308. end,
  1309. %% random kill
  1310. case rand:uniform(10) of
  1311. 1 -> exit(Pid, kill);
  1312. _ -> ok
  1313. end,
  1314. %% random sleep
  1315. RndTime = rand:uniform(30),
  1316. timer:sleep(RndTime)
  1317. end, lists:seq(1, Iterations)),
  1318. TestPid ! {done, node()}
  1319. end,
  1320. %% spawn concurrent
  1321. LocalNode = node(),
  1322. spawn(LocalNode, WorkerFun),
  1323. spawn(SlaveNode1, WorkerFun),
  1324. spawn(SlaveNode2, WorkerFun),
  1325. spawn(SlaveNode3, WorkerFun),
  1326. %% wait for workers done
  1327. syn_test_suite_helper:assert_received_messages([
  1328. {done, LocalNode},
  1329. {done, SlaveNode1},
  1330. {done, SlaveNode2},
  1331. {done, SlaveNode3}
  1332. ]),
  1333. %% check results are same across network
  1334. syn_test_suite_helper:assert_wait(
  1335. 1,
  1336. fun() ->
  1337. ResultPidLocal = syn:lookup(scope_all, <<"concurrent">>),
  1338. ResultPidOn1 = rpc:call(SlaveNode1, syn, lookup, [scope_all, <<"concurrent">>]),
  1339. ResultPidOn2 = rpc:call(SlaveNode2, syn, lookup, [scope_all, <<"concurrent">>]),
  1340. ResultPidOn3 = rpc:call(SlaveNode3, syn, lookup, [scope_all, <<"concurrent">>]),
  1341. %% if unique set is of 1 element then they all contain the same result
  1342. Ordset = ordsets:from_list([ResultPidLocal, ResultPidOn1, ResultPidOn2, ResultPidOn3]),
  1343. ordsets:size(Ordset)
  1344. end
  1345. ).
  1346. %% ===================================================================
  1347. %% Internal
  1348. %% ===================================================================
  1349. add_to_local_table(Scope, Name, Pid, Meta, Time, MRef) ->
  1350. TableByName = syn_backbone:get_table_name(syn_registry_by_name, Scope),
  1351. TableByPid = syn_backbone:get_table_name(syn_registry_by_pid, Scope),
  1352. syn_registry:add_to_local_table(Name, Pid, Meta, Time, MRef, TableByName, TableByPid).
  1353. remove_from_local_table(Scope, Name, Pid) ->
  1354. TableByName = syn_backbone:get_table_name(syn_registry_by_name, Scope),
  1355. TableByPid = syn_backbone:get_table_name(syn_registry_by_pid, Scope),
  1356. syn_registry:remove_from_local_table(Name, Pid, TableByName, TableByPid).