syn_registry_SUITE.erl 41 KB

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