syn_registry_SUITE.erl 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2019 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. single_node_register_and_monitor/1,
  35. single_node_register_and_unregister/1,
  36. single_node_registration_errors/1,
  37. single_node_registry_count/1,
  38. single_node_register_gen_server/1,
  39. single_node_callback_on_process_exit/1,
  40. single_node_ensure_callback_process_exit_is_called_if_process_killed/1,
  41. single_node_monitor_after_registry_crash/1
  42. ]).
  43. -export([
  44. two_nodes_register_monitor_and_unregister/1,
  45. two_nodes_registry_count/1,
  46. two_nodes_registration_race_condition_conflict_resolution/1,
  47. two_nodes_registration_race_condition_conflict_resolution_when_process_died/1,
  48. two_nodes_registry_wait_for_syn_up/1
  49. ]).
  50. -export([
  51. three_nodes_partial_netsplit_consistency/1,
  52. three_nodes_full_netsplit_consistency/1,
  53. three_nodes_start_syn_before_connecting_cluster_with_conflict/1,
  54. three_nodes_start_syn_before_connecting_cluster_with_custom_conflict_resolution/1,
  55. three_nodes_registration_race_condition_custom_conflict_resolution/1
  56. ]).
  57. %% support
  58. -export([
  59. start_syn_delayed_and_register_local_process/3,
  60. start_syn_delayed_with_custom_handler_register_local_process/4
  61. ]).
  62. %% include
  63. -include_lib("common_test/include/ct.hrl").
  64. -include_lib("../src/syn.hrl").
  65. %% ===================================================================
  66. %% Callbacks
  67. %% ===================================================================
  68. %% -------------------------------------------------------------------
  69. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  70. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  71. %% GroupName = atom()
  72. %% TestCase = atom()
  73. %% Reason = any()
  74. %% -------------------------------------------------------------------
  75. all() ->
  76. [
  77. {group, single_node_process_registration},
  78. {group, two_nodes_process_registration},
  79. {group, three_nodes_process_registration}
  80. ].
  81. %% -------------------------------------------------------------------
  82. %% Function: groups() -> [Group]
  83. %% Group = {GroupName,Properties,GroupsAndTestCases}
  84. %% GroupName = atom()
  85. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  86. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  87. %% TestCase = atom()
  88. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  89. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  90. %% repeat_until_any_ok | repeat_until_any_fail
  91. %% N = integer() | forever
  92. %% -------------------------------------------------------------------
  93. groups() ->
  94. [
  95. {single_node_process_registration, [shuffle], [
  96. single_node_register_and_monitor,
  97. single_node_register_and_unregister,
  98. single_node_registration_errors,
  99. single_node_registry_count,
  100. single_node_register_gen_server,
  101. single_node_callback_on_process_exit,
  102. single_node_ensure_callback_process_exit_is_called_if_process_killed,
  103. single_node_monitor_after_registry_crash
  104. ]},
  105. {two_nodes_process_registration, [shuffle], [
  106. two_nodes_register_monitor_and_unregister,
  107. two_nodes_registry_count,
  108. two_nodes_registration_race_condition_conflict_resolution,
  109. two_nodes_registration_race_condition_conflict_resolution_when_process_died,
  110. two_nodes_registry_wait_for_syn_up
  111. ]},
  112. {three_nodes_process_registration, [shuffle], [
  113. three_nodes_partial_netsplit_consistency,
  114. three_nodes_full_netsplit_consistency,
  115. three_nodes_start_syn_before_connecting_cluster_with_conflict,
  116. three_nodes_start_syn_before_connecting_cluster_with_custom_conflict_resolution,
  117. three_nodes_registration_race_condition_custom_conflict_resolution
  118. ]}
  119. ].
  120. %% -------------------------------------------------------------------
  121. %% Function: init_per_suite(Config0) ->
  122. %% Config1 | {skip,Reason} |
  123. %% {skip_and_save,Reason,Config1}
  124. %% Config0 = Config1 = [tuple()]
  125. %% Reason = any()
  126. %% -------------------------------------------------------------------
  127. init_per_suite(Config) ->
  128. Config.
  129. %% -------------------------------------------------------------------
  130. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  131. %% Config0 = Config1 = [tuple()]
  132. %% -------------------------------------------------------------------
  133. end_per_suite(_Config) ->
  134. ok.
  135. %% -------------------------------------------------------------------
  136. %% Function: init_per_group(GroupName, Config0) ->
  137. %% Config1 | {skip,Reason} |
  138. %% {skip_and_save,Reason,Config1}
  139. %% GroupName = atom()
  140. %% Config0 = Config1 = [tuple()]
  141. %% Reason = any()
  142. %% -------------------------------------------------------------------
  143. init_per_group(two_nodes_process_registration, Config) ->
  144. %% start slave
  145. {ok, SlaveNode} = syn_test_suite_helper:start_slave(syn_slave),
  146. %% config
  147. [{slave_node, SlaveNode} | Config];
  148. init_per_group(three_nodes_process_registration, Config) ->
  149. %% start slave
  150. {ok, SlaveNode1} = syn_test_suite_helper:start_slave(syn_slave_1),
  151. {ok, SlaveNode2} = syn_test_suite_helper:start_slave(syn_slave_2),
  152. %% config
  153. [{slave_node_1, SlaveNode1}, {slave_node_2, SlaveNode2} | Config];
  154. init_per_group(_GroupName, Config) ->
  155. Config.
  156. %% -------------------------------------------------------------------
  157. %% Function: end_per_group(GroupName, Config0) ->
  158. %% void() | {save_config,Config1}
  159. %% GroupName = atom()
  160. %% Config0 = Config1 = [tuple()]
  161. %% -------------------------------------------------------------------
  162. end_per_group(two_nodes_process_registration, Config) ->
  163. SlaveNode = proplists:get_value(slave_node, Config),
  164. syn_test_suite_helper:connect_node(SlaveNode),
  165. syn_test_suite_helper:clean_after_test(),
  166. syn_test_suite_helper:stop_slave(syn_slave),
  167. timer:sleep(1000);
  168. end_per_group(three_nodes_process_registration, Config) ->
  169. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  170. syn_test_suite_helper:connect_node(SlaveNode1),
  171. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  172. syn_test_suite_helper:connect_node(SlaveNode2),
  173. syn_test_suite_helper:clean_after_test(),
  174. syn_test_suite_helper:stop_slave(syn_slave_1),
  175. syn_test_suite_helper:stop_slave(syn_slave_2),
  176. timer:sleep(1000);
  177. end_per_group(_GroupName, _Config) ->
  178. syn_test_suite_helper:clean_after_test().
  179. %% -------------------------------------------------------------------
  180. %% Function: init_per_testcase(TestCase, Config0) ->
  181. %% Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  182. %% TestCase = atom()
  183. %% Config0 = Config1 = [tuple()]
  184. %% Reason = any()
  185. %% -------------------------------------------------------------------
  186. init_per_testcase(_TestCase, Config) ->
  187. Config.
  188. %% -------------------------------------------------------------------
  189. %% Function: end_per_testcase(TestCase, Config0) ->
  190. %% void() | {save_config,Config1} | {fail,Reason}
  191. %% TestCase = atom()
  192. %% Config0 = Config1 = [tuple()]
  193. %% Reason = any()
  194. %% -------------------------------------------------------------------
  195. end_per_testcase(_, _Config) ->
  196. syn_test_suite_helper:clean_after_test().
  197. %% ===================================================================
  198. %% Tests
  199. %% ===================================================================
  200. single_node_register_and_monitor(_Config) ->
  201. %% start
  202. ok = syn:start(),
  203. %% start processes
  204. Pid = syn_test_suite_helper:start_process(),
  205. PidWithMeta = syn_test_suite_helper:start_process(),
  206. %% retrieve
  207. undefined = syn:whereis(<<"my proc">>),
  208. %% register
  209. ok = syn:register(<<"my proc">>, Pid),
  210. ok = syn:register(<<"my proc 2">>, Pid),
  211. ok = syn:register(<<"my proc with meta">>, PidWithMeta, {meta, <<"meta">>}),
  212. %% retrieve
  213. Pid = syn:whereis(<<"my proc">>),
  214. Pid = syn:whereis(<<"my proc 2">>),
  215. {PidWithMeta, {meta, <<"meta">>}} = syn:whereis(<<"my proc with meta">>, with_meta),
  216. %% re-register
  217. ok = syn:register(<<"my proc with meta">>, PidWithMeta, {meta2, <<"meta2">>}),
  218. {PidWithMeta, {meta2, <<"meta2">>}} = syn:whereis(<<"my proc with meta">>, with_meta),
  219. %% kill process
  220. syn_test_suite_helper:kill_process(Pid),
  221. syn_test_suite_helper:kill_process(PidWithMeta),
  222. timer:sleep(100),
  223. %% retrieve
  224. undefined = syn:whereis(<<"my proc">>),
  225. undefined = syn:whereis(<<"my proc 2">>),
  226. undefined = syn:whereis(<<"my proc with meta">>).
  227. single_node_register_and_unregister(_Config) ->
  228. %% start
  229. ok = syn:start(),
  230. %% start process
  231. Pid = syn_test_suite_helper:start_process(),
  232. %% retrieve
  233. undefined = syn:whereis(<<"my proc">>),
  234. %% register
  235. ok = syn:register(<<"my proc">>, Pid),
  236. ok = syn:register(<<"my proc 2">>, Pid),
  237. %% retrieve
  238. Pid = syn:whereis(<<"my proc">>),
  239. Pid = syn:whereis(<<"my proc 2">>),
  240. %% unregister 1
  241. ok = syn:unregister(<<"my proc">>),
  242. %% retrieve
  243. undefined = syn:whereis(<<"my proc">>),
  244. Pid = syn:whereis(<<"my proc 2">>),
  245. %% unregister 2
  246. ok = syn:unregister(<<"my proc 2">>),
  247. {error, undefined} = syn:unregister(<<"my proc 2">>),
  248. %% retrieve
  249. undefined = syn:whereis(<<"my proc">>),
  250. undefined = syn:whereis(<<"my proc 2">>).
  251. single_node_registration_errors(_Config) ->
  252. %% start
  253. ok = syn:start(),
  254. %% start process
  255. Pid = syn_test_suite_helper:start_process(),
  256. Pid2 = syn_test_suite_helper:start_process(),
  257. %% register
  258. ok = syn:register(<<"my proc">>, Pid),
  259. {error, taken} = syn:register(<<"my proc">>, Pid2),
  260. %% kill processes
  261. syn_test_suite_helper:kill_process(Pid),
  262. syn_test_suite_helper:kill_process(Pid2),
  263. timer:sleep(100),
  264. %% retrieve
  265. undefined = syn:whereis(<<"my proc">>),
  266. %% try registering a dead pid
  267. {error, not_alive} = syn:register(<<"my proc">>, Pid).
  268. single_node_registry_count(_Config) ->
  269. %% start
  270. ok = syn:start(),
  271. %% start process
  272. Pid = syn_test_suite_helper:start_process(),
  273. Pid2 = syn_test_suite_helper:start_process(),
  274. PidUnregistered = syn_test_suite_helper:start_process(),
  275. %% register
  276. ok = syn:register(<<"my proc">>, Pid),
  277. ok = syn:register(<<"my proc 2">>, Pid2),
  278. %% count
  279. 2 = syn:registry_count(),
  280. 2 = syn:registry_count(node()),
  281. %% kill & unregister
  282. syn_test_suite_helper:kill_process(Pid),
  283. ok = syn:unregister(<<"my proc 2">>),
  284. syn_test_suite_helper:kill_process(PidUnregistered),
  285. timer:sleep(100),
  286. %% count
  287. 0 = syn:registry_count(),
  288. 0 = syn:registry_count(node()).
  289. single_node_register_gen_server(_Config) ->
  290. %% start
  291. ok = syn:start(),
  292. %% start gen server via syn
  293. {ok, Pid} = syn_test_gen_server:start_link(),
  294. %% retrieve
  295. Pid = syn:whereis(syn_test_gen_server),
  296. %% call
  297. pong = syn_test_gen_server:ping(),
  298. %% send via syn
  299. syn:send(syn_test_gen_server, {self(), send_ping}),
  300. receive
  301. send_pong -> ok
  302. after 1000 ->
  303. ok = did_not_receive_gen_server_pong
  304. end,
  305. %% stop server
  306. syn_test_gen_server:stop(),
  307. timer:sleep(200),
  308. %% retrieve
  309. undefined = syn:whereis(syn_test_gen_server),
  310. %% send via syn
  311. {badarg, {syn_test_gen_server, anything}} = (catch syn:send(syn_test_gen_server, anything)).
  312. single_node_callback_on_process_exit(_Config) ->
  313. %% use custom handler
  314. syn_test_suite_helper:use_custom_handler(),
  315. %% start
  316. ok = syn:start(),
  317. %% start process
  318. Pid = syn_test_suite_helper:start_process(),
  319. Pid2 = syn_test_suite_helper:start_process(),
  320. %% register
  321. TestPid = self(),
  322. ok = syn:register(<<"my proc">>, Pid, {pid, TestPid}),
  323. ok = syn:register(<<"my proc - alternate">>, Pid, {pid_alternate, TestPid}),
  324. ok = syn:register(<<"my proc 2">>, Pid2, {pid2, TestPid}),
  325. %% kill 1
  326. syn_test_suite_helper:kill_process(Pid),
  327. receive
  328. {received_event_on, pid} ->
  329. ok;
  330. {received_event_on, pid2} ->
  331. ok = callback_on_process_exit_was_received_by_pid2
  332. after 1000 ->
  333. ok = callback_on_process_exit_was_not_received_by_pid
  334. end,
  335. receive
  336. {received_event_on, pid_alternate} ->
  337. ok;
  338. {received_event_on, pid2} ->
  339. ok = callback_on_process_exit_was_received_by_pid2
  340. after 1000 ->
  341. ok = callback_on_process_exit_was_not_received_by_pid
  342. end,
  343. %% unregister & kill 2
  344. ok = syn:unregister(<<"my proc 2">>),
  345. syn_test_suite_helper:kill_process(Pid2),
  346. receive
  347. {received_event_on, pid2} ->
  348. ok = callback_on_process_exit_was_received_by_pid2
  349. after 1000 ->
  350. ok
  351. end.
  352. single_node_ensure_callback_process_exit_is_called_if_process_killed(_Config) ->
  353. Name = <<"my proc">>,
  354. %% use custom handler
  355. syn_test_suite_helper:use_custom_handler(),
  356. %% start
  357. ok = syn:start(),
  358. %% start process
  359. Pid = syn_test_suite_helper:start_process(),
  360. %% register
  361. TestPid = self(),
  362. ok = syn:register(Name, Pid, {some_meta, TestPid}),
  363. %% remove from table to simulate conflict resolution
  364. syn_registry:remove_from_local_table(Name),
  365. %% kill
  366. exit(Pid, {syn_resolve_kill, Name, {some_meta, TestPid}}),
  367. receive
  368. {received_event_on, some_meta} ->
  369. ok
  370. after 1000 ->
  371. ok = callback_on_process_exit_was_not_received_by_pid
  372. end.
  373. single_node_monitor_after_registry_crash(_Config) ->
  374. %% start
  375. ok = syn:start(),
  376. %% start processes
  377. Pid = syn_test_suite_helper:start_process(),
  378. %% register
  379. ok = syn:register(<<"my proc">>, Pid),
  380. %% kill registry
  381. exit(whereis(syn_registry), kill),
  382. timer:sleep(200),
  383. %% retrieve
  384. Pid = syn:whereis(<<"my proc">>),
  385. %% kill process
  386. syn_test_suite_helper:kill_process(Pid),
  387. timer:sleep(200),
  388. %% retrieve
  389. undefined = syn:whereis(<<"my proc 2">>).
  390. two_nodes_register_monitor_and_unregister(Config) ->
  391. %% get slave
  392. SlaveNode = proplists:get_value(slave_node, Config),
  393. %% start
  394. ok = syn:start(),
  395. ok = rpc:call(SlaveNode, syn, start, []),
  396. timer:sleep(100),
  397. %% start processes
  398. LocalPid = syn_test_suite_helper:start_process(),
  399. RemotePid = syn_test_suite_helper:start_process(SlaveNode),
  400. RemotePidRegRemote = syn_test_suite_helper:start_process(SlaveNode),
  401. %% retrieve
  402. undefined = syn:whereis(<<"local proc">>),
  403. undefined = syn:whereis(<<"remote proc">>),
  404. undefined = syn:whereis(<<"remote proc reg_remote">>),
  405. undefined = rpc:call(SlaveNode, syn, whereis, [<<"local proc">>]),
  406. undefined = rpc:call(SlaveNode, syn, whereis, [<<"remote proc">>]),
  407. undefined = rpc:call(SlaveNode, syn, whereis, [<<"remote proc reg_remote">>]),
  408. %% register
  409. ok = syn:register(<<"local proc">>, LocalPid),
  410. ok = syn:register(<<"remote proc">>, RemotePid),
  411. ok = rpc:call(SlaveNode, syn, register, [<<"remote proc reg_remote">>, RemotePidRegRemote]),
  412. timer:sleep(500),
  413. %% retrieve
  414. LocalPid = syn:whereis(<<"local proc">>),
  415. RemotePid = syn:whereis(<<"remote proc">>),
  416. RemotePidRegRemote = syn:whereis(<<"remote proc reg_remote">>),
  417. LocalPid = rpc:call(SlaveNode, syn, whereis, [<<"local proc">>]),
  418. RemotePid = rpc:call(SlaveNode, syn, whereis, [<<"remote proc">>]),
  419. RemotePidRegRemote = rpc:call(SlaveNode, syn, whereis, [<<"remote proc reg_remote">>]),
  420. %% kill & unregister processes
  421. syn_test_suite_helper:kill_process(LocalPid),
  422. ok = syn:unregister(<<"remote proc">>),
  423. syn_test_suite_helper:kill_process(RemotePidRegRemote),
  424. timer:sleep(100),
  425. %% retrieve
  426. undefined = syn:whereis(<<"local proc">>),
  427. undefined = syn:whereis(<<"remote proc">>),
  428. undefined = syn:whereis(<<"remote proc reg_remote">>),
  429. undefined = rpc:call(SlaveNode, syn, whereis, [<<"local proc">>]),
  430. undefined = rpc:call(SlaveNode, syn, whereis, [<<"remote proc">>]),
  431. undefined = rpc:call(SlaveNode, syn, whereis, [<<"remote proc reg_remote">>]).
  432. two_nodes_registry_count(Config) ->
  433. %% get slave
  434. SlaveNode = proplists:get_value(slave_node, Config),
  435. %% start
  436. ok = syn:start(),
  437. ok = rpc:call(SlaveNode, syn, start, []),
  438. timer:sleep(100),
  439. %% start processes
  440. LocalPid = syn_test_suite_helper:start_process(),
  441. RemotePid = syn_test_suite_helper:start_process(SlaveNode),
  442. RemotePidRegRemote = syn_test_suite_helper:start_process(SlaveNode),
  443. _PidUnregistered = syn_test_suite_helper:start_process(),
  444. %% register
  445. ok = syn:register(<<"local proc">>, LocalPid),
  446. ok = syn:register(<<"remote proc">>, RemotePid),
  447. ok = rpc:call(SlaveNode, syn, register, [<<"remote proc reg_remote">>, RemotePidRegRemote]),
  448. timer:sleep(500),
  449. %% count
  450. 3 = syn:registry_count(),
  451. 1 = syn:registry_count(node()),
  452. 2 = syn:registry_count(SlaveNode),
  453. %% kill & unregister processes
  454. syn_test_suite_helper:kill_process(LocalPid),
  455. ok = syn:unregister(<<"remote proc">>),
  456. syn_test_suite_helper:kill_process(RemotePidRegRemote),
  457. timer:sleep(100),
  458. %% count
  459. 0 = syn:registry_count(),
  460. 0 = syn:registry_count(node()),
  461. 0 = syn:registry_count(SlaveNode).
  462. two_nodes_registration_race_condition_conflict_resolution(Config) ->
  463. ConflictingName = "COMMON",
  464. %% get slaves
  465. SlaveNode = proplists:get_value(slave_node, Config),
  466. %% start syn on nodes
  467. ok = syn:start(),
  468. ok = rpc:call(SlaveNode, syn, start, []),
  469. timer:sleep(100),
  470. %% start processes
  471. Pid0 = syn_test_suite_helper:start_process(),
  472. Pid1 = syn_test_suite_helper:start_process(SlaveNode),
  473. %% inject into syn to simulate concurrent registration
  474. ok = rpc:call(SlaveNode, syn_registry, add_to_local_table, [ConflictingName, Pid1, SlaveNode, undefined]),
  475. %% register on master node to trigger conflict resolution
  476. ok = syn:register(ConflictingName, Pid0, node()),
  477. timer:sleep(1000),
  478. %% check metadata, resolution happens on master node
  479. {Pid1, SlaveNode} = syn:whereis(ConflictingName, with_meta),
  480. {Pid1, SlaveNode} = rpc:call(SlaveNode, syn, whereis, [ConflictingName, with_meta]),
  481. %% check that other processes are not alive because syn killed them
  482. false = is_process_alive(Pid0),
  483. true = rpc:call(SlaveNode, erlang, is_process_alive, [Pid1]).
  484. two_nodes_registration_race_condition_conflict_resolution_when_process_died(Config) ->
  485. ConflictingName = "COMMON",
  486. %% get slaves
  487. SlaveNode = proplists:get_value(slave_node, Config),
  488. %% use customer handler
  489. syn_test_suite_helper:use_custom_handler(),
  490. rpc:call(SlaveNode, syn_test_suite_helper, use_custom_handler, []),
  491. %% start syn on nodes
  492. ok = syn:start(),
  493. ok = rpc:call(SlaveNode, syn, start, []),
  494. timer:sleep(100),
  495. %% start processes
  496. Pid0 = syn_test_suite_helper:start_process(),
  497. Pid1 = syn_test_suite_helper:start_process(SlaveNode),
  498. %% inject into syn to simulate concurrent registration
  499. syn_registry:add_to_local_table(ConflictingName, Pid0, keep_this_one, undefined),
  500. %% kill process
  501. syn_test_suite_helper:kill_process(Pid0),
  502. %% register to trigger conflict resolution
  503. ok = rpc:call(SlaveNode, syn, register, [ConflictingName, Pid1, SlaveNode]),
  504. timer:sleep(1000),
  505. %% check
  506. {Pid1, SlaveNode} = syn:whereis(ConflictingName, with_meta),
  507. {Pid1, SlaveNode} = rpc:call(SlaveNode, syn, whereis, [ConflictingName, with_meta]),
  508. %% check that process is alive
  509. true = rpc:call(SlaveNode, erlang, is_process_alive, [Pid1]).
  510. two_nodes_registry_wait_for_syn_up(_Config) ->
  511. %% stop slave
  512. syn_test_suite_helper:stop_slave(syn_slave),
  513. %% start syn on local node
  514. ok = syn:start(),
  515. %% start process
  516. Pid = syn_test_suite_helper:start_process(),
  517. %% register
  518. ok = syn:register(<<"proc">>, Pid),
  519. %% start remote node and syn
  520. {ok, SlaveNode} = syn_test_suite_helper:start_slave(syn_slave),
  521. ok = rpc:call(SlaveNode, syn, start, []),
  522. timer:sleep(1000),
  523. %% check
  524. Pid = syn:whereis(<<"proc">>),
  525. Pid = rpc:call(SlaveNode, syn, whereis, [<<"proc">>]).
  526. three_nodes_partial_netsplit_consistency(Config) ->
  527. %% get slaves
  528. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  529. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  530. %% start syn on nodes
  531. ok = syn:start(),
  532. ok = rpc:call(SlaveNode1, syn, start, []),
  533. ok = rpc:call(SlaveNode2, syn, start, []),
  534. timer:sleep(100),
  535. %% start processes
  536. Pid0 = syn_test_suite_helper:start_process(),
  537. Pid0Changed = syn_test_suite_helper:start_process(),
  538. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  539. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  540. timer:sleep(100),
  541. %% retrieve
  542. undefined = syn:whereis(<<"proc0">>),
  543. undefined = syn:whereis(<<"proc0-changed">>),
  544. undefined = syn:whereis(<<"proc1">>),
  545. undefined = syn:whereis(<<"proc2">>),
  546. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  547. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  548. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  549. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  550. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  551. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  552. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  553. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]),
  554. %% register (mix nodes)
  555. ok = rpc:call(SlaveNode2, syn, register, [<<"proc0">>, Pid0]),
  556. ok = syn:register(<<"proc1">>, Pid1),
  557. ok = rpc:call(SlaveNode1, syn, register, [<<"proc2">>, Pid2]),
  558. ok = rpc:call(SlaveNode1, syn, register, [<<"proc0-changed">>, Pid0Changed]),
  559. timer:sleep(200),
  560. %% retrieve
  561. Pid0 = syn:whereis(<<"proc0">>),
  562. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  563. Pid1 = syn:whereis(<<"proc1">>),
  564. Pid2 = syn:whereis(<<"proc2">>),
  565. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  566. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  567. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  568. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  569. Pid0 = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  570. Pid0Changed = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  571. Pid1 = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  572. Pid2 = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]),
  573. %% disconnect slave 2 from main (slave 1 can still see slave 2)
  574. syn_test_suite_helper:disconnect_node(SlaveNode2),
  575. timer:sleep(500),
  576. %% retrieve
  577. Pid0 = syn:whereis(<<"proc0">>),
  578. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  579. Pid1 = syn:whereis(<<"proc1">>),
  580. undefined = syn:whereis(<<"proc2">>), %% main has lost slave 2 so 'proc2' is removed
  581. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  582. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  583. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  584. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]), %% slave 1 still has slave 2 so 'proc2' is still there
  585. %% disconnect slave 1
  586. syn_test_suite_helper:disconnect_node(SlaveNode1),
  587. timer:sleep(500),
  588. %% unregister proc0-changed
  589. ok = syn:unregister(<<"proc0-changed">>),
  590. %% retrieve
  591. Pid0 = syn:whereis(<<"proc0">>),
  592. undefined = syn:whereis(<<"proc0-changed">>),
  593. undefined = syn:whereis(<<"proc1">>),
  594. undefined = syn:whereis(<<"proc2">>),
  595. %% reconnect all
  596. syn_test_suite_helper:connect_node(SlaveNode1),
  597. syn_test_suite_helper:connect_node(SlaveNode2),
  598. timer:sleep(5000),
  599. %% retrieve
  600. Pid0 = syn:whereis(<<"proc0">>),
  601. undefined = syn:whereis(<<"proc0-changed">>),
  602. Pid1 = syn:whereis(<<"proc1">>),
  603. Pid2 = syn:whereis(<<"proc2">>),
  604. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  605. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  606. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  607. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  608. Pid0 = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  609. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  610. Pid1 = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  611. Pid2 = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]).
  612. three_nodes_full_netsplit_consistency(Config) ->
  613. %% get slaves
  614. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  615. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  616. %% start syn on nodes
  617. ok = syn:start(),
  618. ok = rpc:call(SlaveNode1, syn, start, []),
  619. ok = rpc:call(SlaveNode2, syn, start, []),
  620. timer:sleep(100),
  621. %% start processes
  622. Pid0 = syn_test_suite_helper:start_process(),
  623. Pid0Changed = syn_test_suite_helper:start_process(),
  624. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  625. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  626. timer:sleep(100),
  627. %% retrieve
  628. undefined = syn:whereis(<<"proc0">>),
  629. undefined = syn:whereis(<<"proc0-changed">>),
  630. undefined = syn:whereis(<<"proc1">>),
  631. undefined = syn:whereis(<<"proc2">>),
  632. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  633. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  634. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  635. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  636. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  637. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  638. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  639. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]),
  640. %% register (mix nodes)
  641. ok = rpc:call(SlaveNode2, syn, register, [<<"proc0">>, Pid0]),
  642. ok = rpc:call(SlaveNode2, syn, register, [<<"proc0-changed">>, Pid0Changed]),
  643. ok = syn:register(<<"proc1">>, Pid1),
  644. ok = rpc:call(SlaveNode1, syn, register, [<<"proc2">>, Pid2]),
  645. timer:sleep(200),
  646. %% retrieve
  647. Pid0 = syn:whereis(<<"proc0">>),
  648. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  649. Pid1 = syn:whereis(<<"proc1">>),
  650. Pid2 = syn:whereis(<<"proc2">>),
  651. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  652. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  653. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  654. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  655. Pid0 = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  656. Pid0Changed = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  657. Pid1 = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  658. Pid2 = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]),
  659. %% disconnect slave 2 from main (slave 1 can still see slave 2)
  660. syn_test_suite_helper:disconnect_node(SlaveNode2),
  661. timer:sleep(500),
  662. %% retrieve
  663. Pid0 = syn:whereis(<<"proc0">>),
  664. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  665. Pid1 = syn:whereis(<<"proc1">>),
  666. undefined = syn:whereis(<<"proc2">>), %% main has lost slave 2 so 'proc2' is removed
  667. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  668. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  669. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  670. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]), %% slave 1 still has slave 2 so 'proc2' is still there
  671. %% disconnect slave 2 from slave 1
  672. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  673. timer:sleep(500),
  674. %% retrieve
  675. Pid0 = syn:whereis(<<"proc0">>),
  676. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  677. Pid1 = syn:whereis(<<"proc1">>),
  678. undefined = syn:whereis(<<"proc2">>), %% main has lost slave 2 so 'proc2' is removed
  679. undefined = syn:whereis(<<"proc2">>, with_meta),
  680. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  681. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  682. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  683. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  684. %% disconnect slave 1
  685. syn_test_suite_helper:disconnect_node(SlaveNode1),
  686. timer:sleep(500),
  687. %% unregister
  688. ok = syn:unregister(<<"proc0-changed">>),
  689. %% retrieve
  690. Pid0 = syn:whereis(<<"proc0">>),
  691. undefined = syn:whereis(<<"proc0-changed">>),
  692. undefined = syn:whereis(<<"proc1">>),
  693. undefined = syn:whereis(<<"proc2">>),
  694. %% reconnect all
  695. syn_test_suite_helper:connect_node(SlaveNode1),
  696. syn_test_suite_helper:connect_node(SlaveNode2),
  697. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  698. timer:sleep(1500),
  699. %% retrieve
  700. Pid0 = syn:whereis(<<"proc0">>),
  701. undefined = syn:whereis(<<"proc0-changed">>),
  702. Pid1 = syn:whereis(<<"proc1">>),
  703. Pid2 = syn:whereis(<<"proc2">>),
  704. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  705. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  706. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  707. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  708. Pid0 = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  709. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  710. Pid1 = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  711. Pid2 = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]).
  712. three_nodes_start_syn_before_connecting_cluster_with_conflict(Config) ->
  713. ConflictingName = "COMMON",
  714. %% get slaves
  715. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  716. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  717. %% start processes
  718. Pid0 = syn_test_suite_helper:start_process(),
  719. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  720. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  721. %% start delayed
  722. start_syn_delayed_and_register_local_process(ConflictingName, Pid0, 1500),
  723. rpc:cast(SlaveNode1, ?MODULE, start_syn_delayed_and_register_local_process, [ConflictingName, Pid1, 1500]),
  724. rpc:cast(SlaveNode2, ?MODULE, start_syn_delayed_and_register_local_process, [ConflictingName, Pid2, 1500]),
  725. timer:sleep(500),
  726. %% disconnect all
  727. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  728. syn_test_suite_helper:disconnect_node(SlaveNode1),
  729. syn_test_suite_helper:disconnect_node(SlaveNode2),
  730. timer:sleep(1500),
  731. [] = nodes(),
  732. %% reconnect all
  733. syn_test_suite_helper:connect_node(SlaveNode1),
  734. syn_test_suite_helper:connect_node(SlaveNode2),
  735. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  736. timer:sleep(1500),
  737. %% count
  738. 1 = syn:registry_count(),
  739. 1 = rpc:call(SlaveNode1, syn, registry_count, []),
  740. 1 = rpc:call(SlaveNode2, syn, registry_count, []),
  741. %% retrieve
  742. true = lists:member(syn:whereis(ConflictingName), [Pid0, Pid1, Pid2]),
  743. true = lists:member(rpc:call(SlaveNode1, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  744. true = lists:member(rpc:call(SlaveNode2, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  745. %% check metadata
  746. case syn:whereis(ConflictingName, with_meta) of
  747. {Pid0, Meta} ->
  748. CurrentNode = node(),
  749. %% check that other nodes' data corresponds
  750. {Pid0, CurrentNode} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  751. {Pid0, CurrentNode} = rpc:call(SlaveNode2, syn, whereis, [ConflictingName, with_meta]),
  752. %% check that other processes are not alive because syn killed them
  753. true = is_process_alive(Pid0),
  754. false = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  755. false = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]);
  756. {Pid1, Meta} ->
  757. SlaveNode1 = Meta,
  758. %% check that other nodes' data corresponds
  759. {Pid1, Meta} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  760. {Pid1, Meta} = rpc:call(SlaveNode2, syn, whereis, [ConflictingName, with_meta]),
  761. %% check that other processes are not alive because syn killed them
  762. false = is_process_alive(Pid0),
  763. true = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  764. false = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]);
  765. {Pid2, Meta} ->
  766. SlaveNode2 = Meta,
  767. %% check that other nodes' data corresponds
  768. {Pid2, Meta} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  769. {Pid2, Meta} = rpc:call(SlaveNode2, syn, whereis, [ConflictingName, with_meta]),
  770. %% check that other processes are not alive because syn killed them
  771. false = is_process_alive(Pid0),
  772. false = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  773. true = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]);
  774. _ ->
  775. ok = no_process_is_registered_with_conflicting_name
  776. end.
  777. three_nodes_start_syn_before_connecting_cluster_with_custom_conflict_resolution(Config) ->
  778. ConflictingName = "COMMON",
  779. %% get slaves
  780. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  781. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  782. %% start processes
  783. Pid0 = syn_test_suite_helper:start_process(),
  784. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  785. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  786. %% start delayed
  787. start_syn_delayed_with_custom_handler_register_local_process(ConflictingName, Pid0, {node, node()}, 1500),
  788. rpc:cast(
  789. SlaveNode1,
  790. ?MODULE,
  791. start_syn_delayed_with_custom_handler_register_local_process,
  792. [ConflictingName, Pid1, keep_this_one, 1500])
  793. ,
  794. rpc:cast(
  795. SlaveNode2,
  796. ?MODULE,
  797. start_syn_delayed_with_custom_handler_register_local_process,
  798. [ConflictingName, Pid2, {node, SlaveNode2}, 1500]
  799. ),
  800. timer:sleep(500),
  801. %% disconnect all
  802. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  803. syn_test_suite_helper:disconnect_node(SlaveNode1),
  804. syn_test_suite_helper:disconnect_node(SlaveNode2),
  805. timer:sleep(1500),
  806. [] = nodes(),
  807. %% reconnect all
  808. syn_test_suite_helper:connect_node(SlaveNode1),
  809. syn_test_suite_helper:connect_node(SlaveNode2),
  810. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  811. timer:sleep(5000),
  812. %% count
  813. 1 = syn:registry_count(),
  814. 1 = rpc:call(SlaveNode1, syn, registry_count, []),
  815. 1 = rpc:call(SlaveNode2, syn, registry_count, []),
  816. %% retrieve
  817. true = lists:member(syn:whereis(ConflictingName), [Pid0, Pid1, Pid2]),
  818. true = lists:member(rpc:call(SlaveNode1, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  819. true = lists:member(rpc:call(SlaveNode2, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  820. %% check metadata that we kept the correct process on all nodes
  821. {Pid1, keep_this_one} = syn:whereis(ConflictingName, with_meta),
  822. {Pid1, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  823. {Pid1, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  824. %% check that other processes are still alive because we didn't kill them
  825. true = is_process_alive(Pid0),
  826. true = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  827. true = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]).
  828. three_nodes_registration_race_condition_custom_conflict_resolution(Config) ->
  829. ConflictingName = "COMMON",
  830. %% get slaves
  831. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  832. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  833. %% use customer handler
  834. syn_test_suite_helper:use_custom_handler(),
  835. rpc:call(SlaveNode1, syn_test_suite_helper, use_custom_handler, []),
  836. rpc:call(SlaveNode2, syn_test_suite_helper, use_custom_handler, []),
  837. %% start syn on nodes
  838. ok = syn:start(),
  839. ok = rpc:call(SlaveNode1, syn, start, []),
  840. ok = rpc:call(SlaveNode2, syn, start, []),
  841. timer:sleep(500),
  842. %% start processes
  843. Pid0 = syn_test_suite_helper:start_process(),
  844. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  845. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  846. %% inject into syn to simulate concurrent registration
  847. ok = rpc:call(SlaveNode1, syn_registry, add_to_local_table, [ConflictingName, Pid1, keep_this_one, undefined]),
  848. %% register on master node to trigger conflict resolution
  849. ok = syn:register(ConflictingName, Pid0, node()),
  850. timer:sleep(1000),
  851. %% retrieve
  852. true = lists:member(syn:whereis(ConflictingName), [Pid0, Pid1, Pid2]),
  853. true = lists:member(rpc:call(SlaveNode1, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  854. true = lists:member(rpc:call(SlaveNode2, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  855. %% check metadata that we kept the correct process on all nodes
  856. {Pid1, keep_this_one} = syn:whereis(ConflictingName, with_meta),
  857. {Pid1, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  858. {Pid1, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  859. %% check that other processes are still alive because we didn't kill them
  860. true = is_process_alive(Pid0),
  861. true = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  862. true = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]).
  863. %% ===================================================================
  864. %% Internal
  865. %% ===================================================================
  866. start_syn_delayed_and_register_local_process(Name, Pid, Ms) ->
  867. spawn(fun() ->
  868. lists:foreach(fun(Node) ->
  869. syn_test_suite_helper:disconnect_node(Node)
  870. end, nodes()),
  871. timer:sleep(Ms),
  872. [] = nodes(),
  873. %%
  874. syn:start(),
  875. ok = syn:register(Name, Pid, node())
  876. end).
  877. start_syn_delayed_with_custom_handler_register_local_process(Name, Pid, Meta, Ms) ->
  878. spawn(fun() ->
  879. lists:foreach(fun(Node) ->
  880. syn_test_suite_helper:disconnect_node(Node)
  881. end, nodes()),
  882. timer:sleep(Ms),
  883. [] = nodes(),
  884. %% use customer handler
  885. syn_test_suite_helper:use_custom_handler(),
  886. %%
  887. syn:start(),
  888. ok = syn:register(Name, Pid, Meta)
  889. end).