syn_register_processes_SUITE.erl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. %% ==========================================================================================================
  2. %% Syn - A global process registry.
  3. %%
  4. %% Copyright (C) 2015, Roberto Ostinelli <roberto@ostinelli.net>.
  5. %% All rights reserved.
  6. %%
  7. %% The MIT License (MIT)
  8. %%
  9. %% Copyright (c) 2015 Roberto Ostinelli
  10. %%
  11. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  12. %% of this software and associated documentation files (the "Software"), to deal
  13. %% in the Software without restriction, including without limitation the rights
  14. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. %% copies of the Software, and to permit persons to whom the Software is
  16. %% furnished to do so, subject to the following conditions:
  17. %%
  18. %% The above copyright notice and this permission notice shall be included in
  19. %% all copies or substantial portions of the Software.
  20. %%
  21. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. %% THE SOFTWARE.
  28. %% ==========================================================================================================
  29. -module(syn_register_processes_SUITE).
  30. %% callbacks
  31. -export([all/0]).
  32. -export([init_per_suite/1, end_per_suite/1]).
  33. -export([groups/0, init_per_group/2, end_per_group/2]).
  34. -export([init_per_testcase/2, end_per_testcase/2]).
  35. %% tests
  36. -export([
  37. single_node_when_mnesia_is_ram_find_by_key/1,
  38. single_node_when_mnesia_is_ram_find_by_pid/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_process_count/1,
  48. two_nodes_when_mnesia_is_disc_find_by_pid/1
  49. ]).
  50. %% include
  51. -include_lib("common_test/include/ct.hrl").
  52. %% ===================================================================
  53. %% Callbacks
  54. %% ===================================================================
  55. %% -------------------------------------------------------------------
  56. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  57. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  58. %% GroupName = atom()
  59. %% TestCase = atom()
  60. %% Reason = term()
  61. %% -------------------------------------------------------------------
  62. all() ->
  63. [
  64. {group, single_node_process_registration},
  65. {group, two_nodes_process_registration}
  66. ].
  67. %% -------------------------------------------------------------------
  68. %% Function: groups() -> [Group]
  69. %% Group = {GroupName,Properties,GroupsAndTestCases}
  70. %% GroupName = atom()
  71. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  72. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  73. %% TestCase = atom()
  74. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  75. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  76. %% repeat_until_any_ok | repeat_until_any_fail
  77. %% N = integer() | forever
  78. %% -------------------------------------------------------------------
  79. groups() ->
  80. [
  81. {single_node_process_registration, [shuffle], [
  82. single_node_when_mnesia_is_ram_find_by_key,
  83. single_node_when_mnesia_is_ram_find_by_pid,
  84. single_node_when_mnesia_is_ram_re_register_error,
  85. single_node_when_mnesia_is_ram_unregister,
  86. single_node_when_mnesia_is_ram_process_count,
  87. single_node_when_mnesia_is_ram_callback_on_process_exit,
  88. single_node_when_mnesia_is_disc_find_by_key
  89. ]},
  90. {two_nodes_process_registration, [shuffle], [
  91. two_nodes_when_mnesia_is_ram_find_by_key,
  92. two_nodes_when_mnesia_is_ram_process_count,
  93. two_nodes_when_mnesia_is_disc_find_by_pid
  94. ]}
  95. ].
  96. %% -------------------------------------------------------------------
  97. %% Function: init_per_suite(Config0) ->
  98. %% Config1 | {skip,Reason} |
  99. %% {skip_and_save,Reason,Config1}
  100. %% Config0 = Config1 = [tuple()]
  101. %% Reason = term()
  102. %% -------------------------------------------------------------------
  103. init_per_suite(Config) ->
  104. %% config
  105. [
  106. {slave_node_short_name, syn_slave}
  107. | Config
  108. ].
  109. %% -------------------------------------------------------------------
  110. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  111. %% Config0 = Config1 = [tuple()]
  112. %% -------------------------------------------------------------------
  113. end_per_suite(_Config) -> ok.
  114. %% -------------------------------------------------------------------
  115. %% Function: init_per_group(GroupName, Config0) ->
  116. %% Config1 | {skip,Reason} |
  117. %% {skip_and_save,Reason,Config1}
  118. %% GroupName = atom()
  119. %% Config0 = Config1 = [tuple()]
  120. %% Reason = term()
  121. %% -------------------------------------------------------------------
  122. init_per_group(two_nodes_process_registration, Config) ->
  123. %% start slave
  124. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  125. {ok, SlaveNodeName} = syn_test_suite_helper:start_slave(SlaveNodeShortName),
  126. %% config
  127. [
  128. {slave_node_name, SlaveNodeName}
  129. | Config
  130. ];
  131. init_per_group(_GroupName, Config) -> Config.
  132. %% -------------------------------------------------------------------
  133. %% Function: end_per_group(GroupName, Config0) ->
  134. %% void() | {save_config,Config1}
  135. %% GroupName = atom()
  136. %% Config0 = Config1 = [tuple()]
  137. %% -------------------------------------------------------------------
  138. end_per_group(two_nodes_process_registration, Config) ->
  139. %% get slave node name
  140. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  141. %% stop slave
  142. syn_test_suite_helper:stop_slave(SlaveNodeShortName);
  143. end_per_group(_GroupName, _Config) ->
  144. ok.
  145. % ----------------------------------------------------------------------------------------------------------
  146. % Function: init_per_testcase(TestCase, Config0) ->
  147. % Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  148. % TestCase = atom()
  149. % Config0 = Config1 = [tuple()]
  150. % Reason = term()
  151. % ----------------------------------------------------------------------------------------------------------
  152. init_per_testcase(_TestCase, Config) ->
  153. Config.
  154. % ----------------------------------------------------------------------------------------------------------
  155. % Function: end_per_testcase(TestCase, Config0) ->
  156. % void() | {save_config,Config1} | {fail,Reason}
  157. % TestCase = atom()
  158. % Config0 = Config1 = [tuple()]
  159. % Reason = term()
  160. % ----------------------------------------------------------------------------------------------------------
  161. end_per_testcase(_TestCase, Config) ->
  162. %% get slave
  163. SlaveNodeName = proplists:get_value(slave_node_name, Config),
  164. syn_test_suite_helper:clean_after_test(SlaveNodeName).
  165. %% ===================================================================
  166. %% Tests
  167. %% ===================================================================
  168. single_node_when_mnesia_is_ram_find_by_key(_Config) ->
  169. %% set schema location
  170. application:set_env(mnesia, schema_location, ram),
  171. %% start
  172. ok = syn:start(),
  173. %% start process
  174. Pid = syn_test_suite_helper:start_process(),
  175. %% retrieve
  176. undefined = syn:find_by_key(<<"my proc">>),
  177. %% register
  178. ok = syn:register(<<"my proc">>, Pid),
  179. %% retrieve
  180. Pid = syn:find_by_key(<<"my proc">>),
  181. %% kill process
  182. syn_test_suite_helper:kill_process(Pid),
  183. timer:sleep(100),
  184. %% retrieve
  185. undefined = syn:find_by_key(<<"my proc">>).
  186. single_node_when_mnesia_is_ram_find_by_pid(_Config) ->
  187. %% set schema location
  188. application:set_env(mnesia, schema_location, ram),
  189. %% start
  190. ok = syn:start(),
  191. %% start process
  192. Pid = syn_test_suite_helper:start_process(),
  193. %% register
  194. ok = syn:register(<<"my proc">>, Pid),
  195. %% retrieve
  196. <<"my proc">> = syn:find_by_pid(Pid),
  197. %% kill process
  198. syn_test_suite_helper:kill_process(Pid),
  199. timer:sleep(100),
  200. %% retrieve
  201. undefined = syn:find_by_pid(Pid).
  202. single_node_when_mnesia_is_ram_re_register_error(_Config) ->
  203. %% set schema location
  204. application:set_env(mnesia, schema_location, ram),
  205. %% start
  206. ok = syn:start(),
  207. %% start process
  208. Pid = syn_test_suite_helper:start_process(),
  209. Pid2 = syn_test_suite_helper:start_process(),
  210. %% register
  211. ok = syn:register(<<"my proc">>, Pid),
  212. {error, taken} = syn:register(<<"my proc">>, Pid2),
  213. %% retrieve
  214. Pid = syn:find_by_key(<<"my proc">>),
  215. %% kill process
  216. syn_test_suite_helper:kill_process(Pid),
  217. timer:sleep(100),
  218. %% retrieve
  219. undefined = syn:find_by_key(<<"my proc">>),
  220. %% reuse
  221. ok = syn:register(<<"my proc">>, Pid2),
  222. %% retrieve
  223. Pid2 = syn:find_by_key(<<"my proc">>),
  224. %% kill process
  225. syn_test_suite_helper:kill_process(Pid),
  226. timer:sleep(100),
  227. %% retrieve
  228. undefined = syn:find_by_pid(Pid).
  229. single_node_when_mnesia_is_ram_unregister(_Config) ->
  230. %% set schema location
  231. application:set_env(mnesia, schema_location, ram),
  232. %% start
  233. ok = syn:start(),
  234. %% start process
  235. Pid = syn_test_suite_helper:start_process(),
  236. %% unregister
  237. {error, undefined} = syn:unregister(<<"my proc">>),
  238. %% register
  239. ok = syn:register(<<"my proc">>, Pid),
  240. %% retrieve
  241. Pid = syn:find_by_key(<<"my proc">>),
  242. %% unregister
  243. ok = syn:unregister(<<"my proc">>),
  244. %% retrieve
  245. undefined = syn:find_by_key(<<"my proc">>),
  246. undefined = syn:find_by_pid(Pid),
  247. %% kill process
  248. syn_test_suite_helper:kill_process(Pid).
  249. single_node_when_mnesia_is_ram_process_count(_Config) ->
  250. %% set schema location
  251. application:set_env(mnesia, schema_location, ram),
  252. %% start
  253. ok = syn:start(),
  254. %% count
  255. 0 = syn:count(),
  256. %% start process
  257. Pid1 = syn_test_suite_helper:start_process(),
  258. Pid2 = syn_test_suite_helper:start_process(),
  259. Pid3 = syn_test_suite_helper:start_process(),
  260. %% register
  261. ok = syn:register(1, Pid1),
  262. ok = syn:register(2, Pid2),
  263. ok = syn:register(3, Pid3),
  264. %% count
  265. 3 = syn:count(),
  266. %% kill processes
  267. syn_test_suite_helper:kill_process(Pid1),
  268. syn_test_suite_helper:kill_process(Pid2),
  269. syn_test_suite_helper:kill_process(Pid3),
  270. timer:sleep(100),
  271. %% count
  272. 0 = syn:count().
  273. single_node_when_mnesia_is_ram_callback_on_process_exit(_Config) ->
  274. %% set schema location
  275. application:set_env(mnesia, schema_location, ram),
  276. %% start
  277. ok = syn:start(),
  278. %% define callback
  279. Self = self(),
  280. CallbackFun = fun(Key, Pid, Reason) ->
  281. Self ! {exited, Key, Pid, Reason}
  282. end,
  283. syn:options([
  284. {process_exit_callback, CallbackFun}
  285. ]),
  286. %% start process
  287. Pid = syn_test_suite_helper:start_process(),
  288. %% register
  289. ok = syn:register(<<"my proc">>, Pid),
  290. %% kill process
  291. syn_test_suite_helper:kill_process(Pid),
  292. %% check callback was triggered
  293. receive
  294. {exited, <<"my proc">>, Pid, killed} -> ok
  295. after 2000 ->
  296. ok = process_exit_callback_was_not_called
  297. end.
  298. single_node_when_mnesia_is_disc_find_by_key(_Config) ->
  299. %% set schema location
  300. application:set_env(mnesia, schema_location, disc),
  301. %% create schema
  302. mnesia:create_schema([node()]),
  303. %% start
  304. ok = syn:start(),
  305. %% start process
  306. Pid = syn_test_suite_helper:start_process(),
  307. %% retrieve
  308. undefined = syn:find_by_key(<<"my proc">>),
  309. %% register
  310. ok = syn:register(<<"my proc">>, Pid),
  311. %% retrieve
  312. Pid = syn:find_by_key(<<"my proc">>),
  313. %% kill process
  314. syn_test_suite_helper:kill_process(Pid),
  315. timer:sleep(100),
  316. %% retrieve
  317. undefined = syn:find_by_key(<<"my proc">>).
  318. two_nodes_when_mnesia_is_ram_find_by_key(Config) ->
  319. %% get slave
  320. SlaveNodeName = proplists:get_value(slave_node_name, Config),
  321. %% set schema location
  322. application:set_env(mnesia, schema_location, ram),
  323. rpc:call(SlaveNodeName, mnesia, schema_location, [ram]),
  324. %% start
  325. ok = syn:start(),
  326. ok = rpc:call(SlaveNodeName, syn, start, []),
  327. timer:sleep(100),
  328. %% start process
  329. Pid = syn_test_suite_helper:start_process(),
  330. %% retrieve
  331. undefined = syn:find_by_key(<<"my proc">>),
  332. undefined = rpc:call(SlaveNodeName, syn, find_by_key, [<<"my proc">>]),
  333. %% register
  334. ok = syn:register(<<"my proc">>, Pid),
  335. %% retrieve
  336. Pid = syn:find_by_key(<<"my proc">>),
  337. Pid = rpc:call(SlaveNodeName, syn, find_by_key, [<<"my proc">>]),
  338. %% kill process
  339. syn_test_suite_helper:kill_process(Pid),
  340. timer:sleep(100),
  341. %% retrieve
  342. undefined = syn:find_by_key(<<"my proc">>),
  343. undefined = rpc:call(SlaveNodeName, syn, find_by_key, [<<"my proc">>]).
  344. two_nodes_when_mnesia_is_ram_process_count(Config) ->
  345. %% get slave
  346. SlaveNodeName = proplists:get_value(slave_node_name, Config),
  347. CurrentNode = node(),
  348. %% set schema location
  349. application:set_env(mnesia, schema_location, ram),
  350. rpc:call(SlaveNodeName, mnesia, schema_location, [ram]),
  351. %% start
  352. ok = syn:start(),
  353. ok = rpc:call(SlaveNodeName, syn, start, []),
  354. timer:sleep(100),
  355. %% count
  356. 0 = syn:count(),
  357. 0 = rpc:call(SlaveNodeName, syn, count, []),
  358. 0 = syn:count(CurrentNode),
  359. 0 = syn:count(SlaveNodeName),
  360. 0 = rpc:call(SlaveNodeName, syn, count, [CurrentNode]),
  361. 0 = rpc:call(SlaveNodeName, syn, count, [SlaveNodeName]),
  362. %% start processes
  363. PidLocal1 = syn_test_suite_helper:start_process(),
  364. PidLocal2 = syn_test_suite_helper:start_process(),
  365. PidSlave = syn_test_suite_helper:start_process(SlaveNodeName),
  366. %% register
  367. ok = syn:register(1, PidLocal1),
  368. ok = syn:register(2, PidLocal2),
  369. ok = syn:register(3, PidSlave),
  370. timer:sleep(100),
  371. %% count
  372. 3 = syn:count(),
  373. 3 = rpc:call(SlaveNodeName, syn, count, []),
  374. 2 = syn:count(CurrentNode),
  375. 1 = syn:count(SlaveNodeName),
  376. 2 = rpc:call(SlaveNodeName, syn, count, [CurrentNode]),
  377. 1 = rpc:call(SlaveNodeName, syn, count, [SlaveNodeName]),
  378. %% kill processes
  379. syn_test_suite_helper:kill_process(PidLocal1),
  380. syn_test_suite_helper:kill_process(PidLocal2),
  381. syn_test_suite_helper:kill_process(PidSlave),
  382. timer:sleep(100),
  383. %% count
  384. 0 = syn:count(),
  385. 0 = rpc:call(SlaveNodeName, syn, count, []),
  386. 0 = syn:count(CurrentNode),
  387. 0 = syn:count(SlaveNodeName),
  388. 0 = rpc:call(SlaveNodeName, syn, count, [CurrentNode]),
  389. 0 = rpc:call(SlaveNodeName, syn, count, [SlaveNodeName]).
  390. two_nodes_when_mnesia_is_disc_find_by_pid(Config) ->
  391. %% get slave
  392. SlaveNodeName = proplists:get_value(slave_node_name, Config),
  393. %% set schema location
  394. application:set_env(mnesia, schema_location, disc),
  395. rpc:call(SlaveNodeName, mnesia, schema_location, [disc]),
  396. %% create schema
  397. mnesia:create_schema([node(), SlaveNodeName]),
  398. %% start
  399. ok = syn:start(),
  400. ok = rpc:call(SlaveNodeName, syn, start, []),
  401. timer:sleep(100),
  402. %% start process
  403. Pid = syn_test_suite_helper:start_process(),
  404. %% register
  405. ok = syn:register(<<"my proc">>, Pid),
  406. %% retrieve
  407. <<"my proc">> = syn:find_by_pid(Pid),
  408. <<"my proc">> = rpc:call(SlaveNodeName, syn, find_by_pid, [Pid]),
  409. %% kill process
  410. syn_test_suite_helper:kill_process(Pid),
  411. timer:sleep(100),
  412. %% retrieve
  413. undefined = syn:find_by_pid(Pid),
  414. undefined = rpc:call(SlaveNodeName, syn, find_by_pid, [Pid]).