syn_registry_SUITE.erl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015 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_when_mnesia_is_ram_find_by_key/1,
  35. single_node_when_mnesia_is_ram_find_by_key_with_meta/1,
  36. single_node_when_mnesia_is_ram_find_by_pid/1,
  37. single_node_when_mnesia_is_ram_find_by_pid_with_meta/1,
  38. single_node_when_mnesia_is_ram_with_gen_server_name/1,
  39. single_node_when_mnesia_is_ram_re_register_error/1,
  40. single_node_when_mnesia_is_ram_unregister/1,
  41. single_node_when_mnesia_is_ram_process_count/1,
  42. single_node_when_mnesia_is_ram_callback_on_process_exit/1,
  43. single_node_when_mnesia_is_disc_find_by_key/1
  44. ]).
  45. -export([
  46. two_nodes_when_mnesia_is_ram_find_by_key/1,
  47. two_nodes_when_mnesia_is_ram_find_by_key_with_meta/1,
  48. two_nodes_when_mnesia_is_ram_process_count/1,
  49. two_nodes_when_mnesia_is_ram_callback_on_process_exit/1,
  50. two_nodes_when_mnesia_is_disc_find_by_pid/1
  51. ]).
  52. %% internals
  53. -export([registry_process_exit_callback_dummy/4]).
  54. %% include
  55. -include_lib("common_test/include/ct.hrl").
  56. %% ===================================================================
  57. %% Callbacks
  58. %% ===================================================================
  59. %% -------------------------------------------------------------------
  60. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  61. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  62. %% GroupName = atom()
  63. %% TestCase = atom()
  64. %% Reason = term()
  65. %% -------------------------------------------------------------------
  66. all() ->
  67. [
  68. {group, single_node_process_registration},
  69. {group, two_nodes_process_registration}
  70. ].
  71. %% -------------------------------------------------------------------
  72. %% Function: groups() -> [Group]
  73. %% Group = {GroupName,Properties,GroupsAndTestCases}
  74. %% GroupName = atom()
  75. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  76. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  77. %% TestCase = atom()
  78. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  79. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  80. %% repeat_until_any_ok | repeat_until_any_fail
  81. %% N = integer() | forever
  82. %% -------------------------------------------------------------------
  83. groups() ->
  84. [
  85. {single_node_process_registration, [shuffle], [
  86. single_node_when_mnesia_is_ram_find_by_key,
  87. single_node_when_mnesia_is_ram_find_by_key_with_meta,
  88. single_node_when_mnesia_is_ram_find_by_pid,
  89. single_node_when_mnesia_is_ram_find_by_pid_with_meta,
  90. single_node_when_mnesia_is_ram_with_gen_server_name,
  91. single_node_when_mnesia_is_ram_re_register_error,
  92. single_node_when_mnesia_is_ram_unregister,
  93. single_node_when_mnesia_is_ram_process_count,
  94. single_node_when_mnesia_is_ram_callback_on_process_exit,
  95. single_node_when_mnesia_is_disc_find_by_key
  96. ]},
  97. {two_nodes_process_registration, [shuffle], [
  98. two_nodes_when_mnesia_is_ram_find_by_key,
  99. two_nodes_when_mnesia_is_ram_find_by_key_with_meta,
  100. two_nodes_when_mnesia_is_ram_process_count,
  101. two_nodes_when_mnesia_is_ram_callback_on_process_exit,
  102. two_nodes_when_mnesia_is_disc_find_by_pid
  103. ]}
  104. ].
  105. %% -------------------------------------------------------------------
  106. %% Function: init_per_suite(Config0) ->
  107. %% Config1 | {skip,Reason} |
  108. %% {skip_and_save,Reason,Config1}
  109. %% Config0 = Config1 = [tuple()]
  110. %% Reason = term()
  111. %% -------------------------------------------------------------------
  112. init_per_suite(Config) ->
  113. %% config
  114. [
  115. {slave_node_short_name, syn_slave}
  116. | Config
  117. ].
  118. %% -------------------------------------------------------------------
  119. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  120. %% Config0 = Config1 = [tuple()]
  121. %% -------------------------------------------------------------------
  122. end_per_suite(_Config) -> ok.
  123. %% -------------------------------------------------------------------
  124. %% Function: init_per_group(GroupName, Config0) ->
  125. %% Config1 | {skip,Reason} |
  126. %% {skip_and_save,Reason,Config1}
  127. %% GroupName = atom()
  128. %% Config0 = Config1 = [tuple()]
  129. %% Reason = term()
  130. %% -------------------------------------------------------------------
  131. init_per_group(two_nodes_process_registration, Config) ->
  132. %% start slave
  133. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  134. {ok, SlaveNode} = syn_test_suite_helper:start_slave(SlaveNodeShortName),
  135. %% config
  136. [
  137. {slave_node, SlaveNode}
  138. | Config
  139. ];
  140. init_per_group(_GroupName, Config) -> Config.
  141. %% -------------------------------------------------------------------
  142. %% Function: end_per_group(GroupName, Config0) ->
  143. %% void() | {save_config,Config1}
  144. %% GroupName = atom()
  145. %% Config0 = Config1 = [tuple()]
  146. %% -------------------------------------------------------------------
  147. end_per_group(two_nodes_process_registration, Config) ->
  148. %% get slave node name
  149. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  150. %% stop slave
  151. syn_test_suite_helper:stop_slave(SlaveNodeShortName);
  152. end_per_group(_GroupName, _Config) ->
  153. ok.
  154. % ----------------------------------------------------------------------------------------------------------
  155. % Function: init_per_testcase(TestCase, Config0) ->
  156. % Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  157. % TestCase = atom()
  158. % Config0 = Config1 = [tuple()]
  159. % Reason = term()
  160. % ----------------------------------------------------------------------------------------------------------
  161. init_per_testcase(_TestCase, Config) ->
  162. Config.
  163. % ----------------------------------------------------------------------------------------------------------
  164. % Function: end_per_testcase(TestCase, Config0) ->
  165. % void() | {save_config,Config1} | {fail,Reason}
  166. % TestCase = atom()
  167. % Config0 = Config1 = [tuple()]
  168. % Reason = term()
  169. % ----------------------------------------------------------------------------------------------------------
  170. end_per_testcase(_TestCase, Config) ->
  171. %% get slave
  172. SlaveNode = proplists:get_value(slave_node, Config),
  173. syn_test_suite_helper:clean_after_test(SlaveNode).
  174. %% ===================================================================
  175. %% Tests
  176. %% ===================================================================
  177. single_node_when_mnesia_is_ram_find_by_key(_Config) ->
  178. %% set schema location
  179. application:set_env(mnesia, schema_location, ram),
  180. %% start
  181. ok = syn:start(),
  182. ok = syn:init(),
  183. %% start process
  184. Pid = syn_test_suite_helper:start_process(),
  185. %% retrieve
  186. undefined = syn:find_by_key(<<"my proc">>),
  187. %% register
  188. ok = syn:register(<<"my proc">>, Pid),
  189. %% retrieve
  190. Pid = syn:find_by_key(<<"my proc">>),
  191. %% kill process
  192. syn_test_suite_helper:kill_process(Pid),
  193. timer:sleep(100),
  194. %% retrieve
  195. undefined = syn:find_by_key(<<"my proc">>).
  196. single_node_when_mnesia_is_ram_with_gen_server_name(_Config) ->
  197. %% set schema location
  198. application:set_env(mnesia, schema_location, ram),
  199. %% start
  200. ok = syn:start(),
  201. ok = syn:init(),
  202. %% retrieve
  203. undefined = syn:whereis_name(<<"my proc">>),
  204. %% register
  205. {ok, Pid} = syn_test_gen_server:start_link(self(), {via, syn, <<"my proc">>}),
  206. %% retrieve
  207. Pid = syn:whereis_name(<<"my proc">>),
  208. %% gen_server call messages
  209. call_received = gen_server:call({via, syn, <<"my proc">>}, message_is_ignored),
  210. %% gen_server cast messages
  211. gen_server:cast({via, syn, <<"my proc">>}, message_is_ignored),
  212. ok = receive
  213. cast_received -> ok
  214. after
  215. 1000 -> error_receiving_cast_message
  216. end,
  217. %% any other message
  218. syn:send(<<"my proc">>, message_is_ignored),
  219. ok = receive
  220. info_received -> ok
  221. after
  222. 1000 -> error_receiving_info_message
  223. end,
  224. %% stop process
  225. syn_test_gen_server:stop({via, syn, <<"my proc">>}),
  226. ok = receive
  227. stop_received -> ok
  228. after
  229. 1000 -> error_stopping_process
  230. end,
  231. %% wait for process to exit
  232. timer:sleep(100),
  233. %% retrieve
  234. undefined = syn:find_by_key(<<"my proc">>).
  235. single_node_when_mnesia_is_ram_find_by_key_with_meta(_Config) ->
  236. %% set schema location
  237. application:set_env(mnesia, schema_location, ram),
  238. %% start
  239. ok = syn:start(),
  240. ok = syn:init(),
  241. %% start process
  242. Pid1 = syn_test_suite_helper:start_process(),
  243. Pid2 = syn_test_suite_helper:start_process(),
  244. %% retrieve
  245. undefined = syn:find_by_key(<<"my proc 1">>, with_meta),
  246. undefined = syn:find_by_key(<<"my proc 2">>, with_meta),
  247. %% register
  248. Meta = [{some, 1}, {meta, <<"data">>}],
  249. ok = syn:register(<<"my proc 1">>, Pid1, Meta),
  250. ok = syn:register(<<"my proc 2">>, Pid2),
  251. %% retrieve
  252. {Pid1, Meta} = syn:find_by_key(<<"my proc 1">>, with_meta),
  253. {Pid2, undefined} = syn:find_by_key(<<"my proc 2">>, with_meta),
  254. %% kill process
  255. syn_test_suite_helper:kill_process(Pid1),
  256. syn_test_suite_helper:kill_process(Pid2),
  257. timer:sleep(100),
  258. %% retrieve
  259. undefined = syn:find_by_key(<<"my proc 1">>, with_meta),
  260. undefined = syn:find_by_key(<<"my proc 2">>, with_meta).
  261. single_node_when_mnesia_is_ram_find_by_pid(_Config) ->
  262. %% set schema location
  263. application:set_env(mnesia, schema_location, ram),
  264. %% start
  265. ok = syn:start(),
  266. ok = syn:init(),
  267. %% start process
  268. Pid = syn_test_suite_helper:start_process(),
  269. %% register
  270. ok = syn:register(<<"my proc">>, Pid),
  271. %% retrieve
  272. <<"my proc">> = syn:find_by_pid(Pid),
  273. %% kill process
  274. syn_test_suite_helper:kill_process(Pid),
  275. timer:sleep(100),
  276. %% retrieve
  277. undefined = syn:find_by_pid(Pid).
  278. single_node_when_mnesia_is_ram_find_by_pid_with_meta(_Config) ->
  279. %% set schema location
  280. application:set_env(mnesia, schema_location, ram),
  281. %% start
  282. ok = syn:start(),
  283. ok = syn:init(),
  284. %% start process
  285. Pid1 = syn_test_suite_helper:start_process(),
  286. Pid2 = syn_test_suite_helper:start_process(),
  287. %% retrieve
  288. undefined = syn:find_by_pid(Pid1, with_meta),
  289. undefined = syn:find_by_pid(Pid2, with_meta),
  290. %% register
  291. Meta = [{some, 1}, {meta, <<"data">>}],
  292. ok = syn:register(<<"my proc 1">>, Pid1, Meta),
  293. ok = syn:register(<<"my proc 2">>, Pid2),
  294. %% retrieve
  295. {<<"my proc 1">>, Meta} = syn:find_by_pid(Pid1, with_meta),
  296. {<<"my proc 2">>, undefined} = syn:find_by_pid(Pid2, with_meta),
  297. %% kill process
  298. syn_test_suite_helper:kill_process(Pid1),
  299. syn_test_suite_helper:kill_process(Pid2),
  300. timer:sleep(100),
  301. %% retrieve
  302. undefined = syn:find_by_pid(Pid1, with_meta),
  303. undefined = syn:find_by_pid(Pid2, with_meta).
  304. single_node_when_mnesia_is_ram_re_register_error(_Config) ->
  305. %% set schema location
  306. application:set_env(mnesia, schema_location, ram),
  307. %% start
  308. ok = syn:start(),
  309. ok = syn:init(),
  310. %% start process
  311. Pid = syn_test_suite_helper:start_process(),
  312. Pid2 = syn_test_suite_helper:start_process(),
  313. %% register
  314. ok = syn:register(<<"my proc">>, Pid),
  315. %% re-register same process
  316. ok = syn:register(<<"my proc">>, Pid, {with, meta}),
  317. %% register same process with another name
  318. {error, pid_already_registered} = syn:register(<<"my proc 2">>, Pid),
  319. %% register another process
  320. {error, taken} = syn:register(<<"my proc">>, Pid2),
  321. %% retrieve
  322. {Pid, {with, meta}} = syn:find_by_key(<<"my proc">>, with_meta),
  323. %% kill process
  324. syn_test_suite_helper:kill_process(Pid),
  325. timer:sleep(100),
  326. %% retrieve
  327. undefined = syn:find_by_key(<<"my proc">>),
  328. %% reuse
  329. ok = syn:register(<<"my proc">>, Pid2),
  330. %% retrieve
  331. Pid2 = syn:find_by_key(<<"my proc">>),
  332. %% kill process
  333. syn_test_suite_helper:kill_process(Pid),
  334. timer:sleep(100),
  335. %% retrieve
  336. undefined = syn:find_by_pid(Pid).
  337. single_node_when_mnesia_is_ram_unregister(_Config) ->
  338. %% set schema location
  339. application:set_env(mnesia, schema_location, ram),
  340. %% start
  341. ok = syn:start(),
  342. ok = syn:init(),
  343. %% start process
  344. Pid = syn_test_suite_helper:start_process(),
  345. %% unregister
  346. {error, undefined} = syn:unregister(<<"my proc">>),
  347. %% register
  348. ok = syn:register(<<"my proc">>, Pid),
  349. %% retrieve
  350. Pid = syn:find_by_key(<<"my proc">>),
  351. %% unregister
  352. ok = syn:unregister(<<"my proc">>),
  353. %% retrieve
  354. undefined = syn:find_by_key(<<"my proc">>),
  355. undefined = syn:find_by_pid(Pid),
  356. %% kill process
  357. syn_test_suite_helper:kill_process(Pid).
  358. single_node_when_mnesia_is_ram_process_count(_Config) ->
  359. %% set schema location
  360. application:set_env(mnesia, schema_location, ram),
  361. %% start
  362. ok = syn:start(),
  363. ok = syn:init(),
  364. %% count
  365. 0 = syn:registry_count(),
  366. %% start process
  367. Pid1 = syn_test_suite_helper:start_process(),
  368. Pid2 = syn_test_suite_helper:start_process(),
  369. Pid3 = syn_test_suite_helper:start_process(),
  370. %% register
  371. ok = syn:register(1, Pid1),
  372. ok = syn:register(2, Pid2),
  373. ok = syn:register(3, Pid3),
  374. %% count
  375. 3 = syn:registry_count(),
  376. %% kill processes
  377. syn_test_suite_helper:kill_process(Pid1),
  378. syn_test_suite_helper:kill_process(Pid2),
  379. syn_test_suite_helper:kill_process(Pid3),
  380. timer:sleep(100),
  381. %% count
  382. 0 = syn:registry_count().
  383. single_node_when_mnesia_is_ram_callback_on_process_exit(_Config) ->
  384. CurrentNode = node(),
  385. %% set schema location
  386. application:set_env(mnesia, schema_location, ram),
  387. %% load configuration variables from syn-test.config => this defines the callback
  388. syn_test_suite_helper:set_environment_variables(),
  389. %% start
  390. ok = syn:start(),
  391. ok = syn:init(),
  392. %% register global process
  393. ResultPid = self(),
  394. global:register_name(syn_register_process_SUITE_result, ResultPid),
  395. %% start process
  396. Pid = syn_test_suite_helper:start_process(),
  397. %% register
  398. Meta = {some, meta},
  399. ok = syn:register(<<"my proc">>, Pid, Meta),
  400. %% kill process
  401. syn_test_suite_helper:kill_process(Pid),
  402. %% check callback were triggered
  403. receive
  404. {exited, CurrentNode, <<"my proc">>, Pid, Meta, killed} -> ok
  405. after 2000 ->
  406. ok = registry_process_exit_callback_was_not_called_from_local_node
  407. end,
  408. %% unregister
  409. global:unregister_name(syn_register_process_SUITE_result).
  410. single_node_when_mnesia_is_disc_find_by_key(_Config) ->
  411. %% set schema location
  412. application:set_env(mnesia, schema_location, disc),
  413. %% create schema
  414. mnesia:create_schema([node()]),
  415. %% start
  416. ok = syn:start(),
  417. ok = syn:init(),
  418. %% start process
  419. Pid = syn_test_suite_helper:start_process(),
  420. %% retrieve
  421. undefined = syn:find_by_key(<<"my proc">>),
  422. %% register
  423. ok = syn:register(<<"my proc">>, Pid),
  424. %% retrieve
  425. Pid = syn:find_by_key(<<"my proc">>),
  426. %% kill process
  427. syn_test_suite_helper:kill_process(Pid),
  428. timer:sleep(100),
  429. %% retrieve
  430. undefined = syn:find_by_key(<<"my proc">>).
  431. two_nodes_when_mnesia_is_ram_find_by_key(Config) ->
  432. %% get slave
  433. SlaveNode = proplists:get_value(slave_node, Config),
  434. %% set schema location
  435. application:set_env(mnesia, schema_location, ram),
  436. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  437. %% start
  438. ok = syn:start(),
  439. ok = syn:init(),
  440. ok = rpc:call(SlaveNode, syn, start, []),
  441. ok = rpc:call(SlaveNode, syn, init, []),
  442. timer:sleep(100),
  443. %% start process
  444. Pid = syn_test_suite_helper:start_process(),
  445. %% retrieve
  446. undefined = syn:find_by_key(<<"my proc">>),
  447. undefined = rpc:call(SlaveNode, syn, find_by_key, [<<"my proc">>]),
  448. %% register
  449. ok = syn:register(<<"my proc">>, Pid),
  450. %% retrieve
  451. Pid = syn:find_by_key(<<"my proc">>),
  452. Pid = rpc:call(SlaveNode, syn, find_by_key, [<<"my proc">>]),
  453. %% kill process
  454. syn_test_suite_helper:kill_process(Pid),
  455. timer:sleep(100),
  456. %% retrieve
  457. undefined = syn:find_by_key(<<"my proc">>),
  458. undefined = rpc:call(SlaveNode, syn, find_by_key, [<<"my proc">>]).
  459. two_nodes_when_mnesia_is_ram_find_by_key_with_meta(Config) ->
  460. %% get slave
  461. SlaveNode = proplists:get_value(slave_node, Config),
  462. %% set schema location
  463. application:set_env(mnesia, schema_location, ram),
  464. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  465. %% start
  466. ok = syn:start(),
  467. ok = syn:init(),
  468. ok = rpc:call(SlaveNode, syn, start, []),
  469. ok = rpc:call(SlaveNode, syn, init, []),
  470. timer:sleep(100),
  471. %% start process
  472. Pid1 = syn_test_suite_helper:start_process(),
  473. Pid2 = syn_test_suite_helper:start_process(),
  474. %% retrieve
  475. undefined = syn:find_by_key(<<"my proc 1">>),
  476. undefined = rpc:call(SlaveNode, syn, find_by_key, [<<"my proc 1">>]),
  477. undefined = syn:find_by_key(<<"my proc 2">>),
  478. undefined = rpc:call(SlaveNode, syn, find_by_key, [<<"my proc 2">>]),
  479. %% register
  480. Meta = [{some, 1}, {meta, <<"data">>}],
  481. ok = syn:register(<<"my proc 1">>, Pid1, Meta),
  482. ok = syn:register(<<"my proc 2">>, Pid2),
  483. %% retrieve
  484. {Pid1, Meta} = syn:find_by_key(<<"my proc 1">>, with_meta),
  485. {Pid1, Meta} = rpc:call(SlaveNode, syn, find_by_key, [<<"my proc 1">>, with_meta]),
  486. {Pid2, undefined} = syn:find_by_key(<<"my proc 2">>, with_meta),
  487. {Pid2, undefined} = rpc:call(SlaveNode, syn, find_by_key, [<<"my proc 2">>, with_meta]),
  488. %% kill process
  489. syn_test_suite_helper:kill_process(Pid1),
  490. syn_test_suite_helper:kill_process(Pid2),
  491. timer:sleep(100),
  492. %% retrieve
  493. undefined = syn:find_by_key(<<"my proc 1">>),
  494. undefined = rpc:call(SlaveNode, syn, find_by_key, [<<"my proc 1">>]),
  495. undefined = syn:find_by_key(<<"my proc 2">>),
  496. undefined = rpc:call(SlaveNode, syn, find_by_key, [<<"my proc 2">>]).
  497. two_nodes_when_mnesia_is_ram_process_count(Config) ->
  498. %% get slave
  499. SlaveNode = proplists:get_value(slave_node, Config),
  500. CurrentNode = node(),
  501. %% set schema location
  502. application:set_env(mnesia, schema_location, ram),
  503. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  504. %% start
  505. ok = syn:start(),
  506. ok = syn:init(),
  507. ok = rpc:call(SlaveNode, syn, start, []),
  508. ok = rpc:call(SlaveNode, syn, init, []),
  509. timer:sleep(100),
  510. %% count
  511. 0 = syn:registry_count(),
  512. 0 = rpc:call(SlaveNode, syn, registry_count, []),
  513. 0 = syn:registry_count(CurrentNode),
  514. 0 = syn:registry_count(SlaveNode),
  515. 0 = rpc:call(SlaveNode, syn, registry_count, [CurrentNode]),
  516. 0 = rpc:call(SlaveNode, syn, registry_count, [SlaveNode]),
  517. %% start processes
  518. PidLocal1 = syn_test_suite_helper:start_process(),
  519. PidLocal2 = syn_test_suite_helper:start_process(),
  520. PidSlave = syn_test_suite_helper:start_process(SlaveNode),
  521. %% register
  522. ok = syn:register(1, PidLocal1),
  523. ok = syn:register(2, PidLocal2),
  524. ok = syn:register(3, PidSlave),
  525. timer:sleep(100),
  526. %% count
  527. 3 = syn:registry_count(),
  528. 3 = rpc:call(SlaveNode, syn, registry_count, []),
  529. 2 = syn:registry_count(CurrentNode),
  530. 1 = syn:registry_count(SlaveNode),
  531. 2 = rpc:call(SlaveNode, syn, registry_count, [CurrentNode]),
  532. 1 = rpc:call(SlaveNode, syn, registry_count, [SlaveNode]),
  533. %% kill processes
  534. syn_test_suite_helper:kill_process(PidLocal1),
  535. syn_test_suite_helper:kill_process(PidLocal2),
  536. syn_test_suite_helper:kill_process(PidSlave),
  537. timer:sleep(100),
  538. %% count
  539. 0 = syn:registry_count(),
  540. 0 = rpc:call(SlaveNode, syn, registry_count, []),
  541. 0 = syn:registry_count(CurrentNode),
  542. 0 = syn:registry_count(SlaveNode),
  543. 0 = rpc:call(SlaveNode, syn, registry_count, [CurrentNode]),
  544. 0 = rpc:call(SlaveNode, syn, registry_count, [SlaveNode]).
  545. two_nodes_when_mnesia_is_ram_callback_on_process_exit(Config) ->
  546. %% get slave
  547. SlaveNode = proplists:get_value(slave_node, Config),
  548. CurrentNode = node(),
  549. %% set schema location
  550. application:set_env(mnesia, schema_location, ram),
  551. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  552. %% load configuration variables from syn-test.config => this defines the callback
  553. syn_test_suite_helper:set_environment_variables(),
  554. syn_test_suite_helper:set_environment_variables(SlaveNode),
  555. %% start
  556. ok = syn:start(),
  557. ok = syn:init(),
  558. ok = rpc:call(SlaveNode, syn, start, []),
  559. ok = rpc:call(SlaveNode, syn, init, []),
  560. timer:sleep(100),
  561. %% register global process
  562. ResultPid = self(),
  563. global:register_name(syn_register_process_SUITE_result, ResultPid),
  564. %% start processes
  565. PidLocal = syn_test_suite_helper:start_process(),
  566. PidSlave = syn_test_suite_helper:start_process(SlaveNode),
  567. %% register
  568. Meta = {some, meta},
  569. ok = syn:register(<<"local">>, PidLocal, Meta),
  570. ok = syn:register(<<"slave">>, PidSlave),
  571. %% kill process
  572. syn_test_suite_helper:kill_process(PidLocal),
  573. syn_test_suite_helper:kill_process(PidSlave),
  574. %% check callback were triggered
  575. receive
  576. {exited, CurrentNode, <<"local">>, PidLocal, Meta, killed} -> ok
  577. after 2000 ->
  578. ok = registry_process_exit_callback_was_not_called_from_local_node
  579. end,
  580. receive
  581. {exited, SlaveNode, <<"slave">>, PidSlave, undefined, killed} -> ok
  582. after 2000 ->
  583. ok = registry_process_exit_callback_was_not_called_from_slave_node
  584. end,
  585. %% unregister
  586. global:unregister_name(syn_register_process_SUITE_result).
  587. two_nodes_when_mnesia_is_disc_find_by_pid(Config) ->
  588. %% get slave
  589. SlaveNode = proplists:get_value(slave_node, Config),
  590. %% set schema location
  591. application:set_env(mnesia, schema_location, disc),
  592. rpc:call(SlaveNode, mnesia, schema_location, [disc]),
  593. %% create schema
  594. mnesia:create_schema([node(), SlaveNode]),
  595. %% start
  596. ok = syn:start(),
  597. ok = syn:init(),
  598. ok = rpc:call(SlaveNode, syn, start, []),
  599. ok = rpc:call(SlaveNode, syn, init, []),
  600. timer:sleep(100),
  601. %% start process
  602. Pid = syn_test_suite_helper:start_process(),
  603. %% register
  604. ok = syn:register(<<"my proc">>, Pid),
  605. %% retrieve
  606. <<"my proc">> = syn:find_by_pid(Pid),
  607. <<"my proc">> = rpc:call(SlaveNode, syn, find_by_pid, [Pid]),
  608. %% kill process
  609. syn_test_suite_helper:kill_process(Pid),
  610. timer:sleep(100),
  611. %% retrieve
  612. undefined = syn:find_by_pid(Pid),
  613. undefined = rpc:call(SlaveNode, syn, find_by_pid, [Pid]).
  614. %% ===================================================================
  615. %% Internal
  616. %% ===================================================================
  617. registry_process_exit_callback_dummy(Key, Pid, Meta, Reason) ->
  618. global:send(syn_register_process_SUITE_result, {exited, node(), Key, Pid, Meta, Reason}).