syn_registry_SUITE.erl 39 KB

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