syn_register_processes_SUITE.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. %% internal
  36. -export([process_main/0]).
  37. %% tests
  38. -export([
  39. single_node_when_mnesia_is_ram_find_by_key/1,
  40. single_node_when_mnesia_is_ram_find_by_pid/1,
  41. single_node_when_mnesia_is_ram_re_register_error/1,
  42. single_node_when_mnesia_is_ram_unregister/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_disc_find_by_pid/1
  48. ]).
  49. %% include
  50. -include_lib("common_test/include/ct.hrl").
  51. %% ===================================================================
  52. %% Callbacks
  53. %% ===================================================================
  54. %% -------------------------------------------------------------------
  55. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  56. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  57. %% GroupName = atom()
  58. %% TestCase = atom()
  59. %% Reason = term()
  60. %% -------------------------------------------------------------------
  61. all() ->
  62. [
  63. {group, single_node_process_registration},
  64. {group, two_nodes_process_registration}
  65. ].
  66. %% -------------------------------------------------------------------
  67. %% Function: groups() -> [Group]
  68. %% Group = {GroupName,Properties,GroupsAndTestCases}
  69. %% GroupName = atom()
  70. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  71. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  72. %% TestCase = atom()
  73. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  74. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  75. %% repeat_until_any_ok | repeat_until_any_fail
  76. %% N = integer() | forever
  77. %% -------------------------------------------------------------------
  78. groups() ->
  79. [
  80. {single_node_process_registration, [shuffle], [
  81. single_node_when_mnesia_is_ram_find_by_key,
  82. single_node_when_mnesia_is_ram_find_by_pid,
  83. single_node_when_mnesia_is_ram_re_register_error,
  84. single_node_when_mnesia_is_ram_unregister,
  85. single_node_when_mnesia_is_disc_find_by_key
  86. ]},
  87. {two_nodes_process_registration, [shuffle], [
  88. two_nodes_when_mnesia_is_ram_find_by_key,
  89. two_nodes_when_mnesia_is_disc_find_by_pid
  90. ]}
  91. ].
  92. %% -------------------------------------------------------------------
  93. %% Function: init_per_suite(Config0) ->
  94. %% Config1 | {skip,Reason} |
  95. %% {skip_and_save,Reason,Config1}
  96. %% Config0 = Config1 = [tuple()]
  97. %% Reason = term()
  98. %% -------------------------------------------------------------------
  99. init_per_suite(Config) ->
  100. %% config
  101. [
  102. {slave_node_short_name, syn_slave}
  103. | Config
  104. ].
  105. %% -------------------------------------------------------------------
  106. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  107. %% Config0 = Config1 = [tuple()]
  108. %% -------------------------------------------------------------------
  109. end_per_suite(_Config) -> ok.
  110. %% -------------------------------------------------------------------
  111. %% Function: init_per_group(GroupName, Config0) ->
  112. %% Config1 | {skip,Reason} |
  113. %% {skip_and_save,Reason,Config1}
  114. %% GroupName = atom()
  115. %% Config0 = Config1 = [tuple()]
  116. %% Reason = term()
  117. %% -------------------------------------------------------------------
  118. init_per_group(two_nodes_process_registration, Config) ->
  119. %% get slave node short name
  120. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  121. {ok, SlaveNodeName} = syn_test_suite_helper:start_slave(SlaveNodeShortName),
  122. %% config
  123. [
  124. {slave_node_name, SlaveNodeName}
  125. | Config
  126. ];
  127. init_per_group(_GroupName, Config) -> Config.
  128. %% -------------------------------------------------------------------
  129. %% Function: end_per_group(GroupName, Config0) ->
  130. %% void() | {save_config,Config1}
  131. %% GroupName = atom()
  132. %% Config0 = Config1 = [tuple()]
  133. %% -------------------------------------------------------------------
  134. end_per_group(two_nodes_mnesia_creation, Config) ->
  135. %% get slave node name
  136. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  137. %% stop slave
  138. syn_test_suite_helper:stop_slave(SlaveNodeShortName);
  139. end_per_group(_GroupName, _Config) ->
  140. ok.
  141. % ----------------------------------------------------------------------------------------------------------
  142. % Function: init_per_testcase(TestCase, Config0) ->
  143. % Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  144. % TestCase = atom()
  145. % Config0 = Config1 = [tuple()]
  146. % Reason = term()
  147. % ----------------------------------------------------------------------------------------------------------
  148. init_per_testcase(_TestCase, Config) ->
  149. Config.
  150. % ----------------------------------------------------------------------------------------------------------
  151. % Function: end_per_testcase(TestCase, Config0) ->
  152. % void() | {save_config,Config1} | {fail,Reason}
  153. % TestCase = atom()
  154. % Config0 = Config1 = [tuple()]
  155. % Reason = term()
  156. % ----------------------------------------------------------------------------------------------------------
  157. end_per_testcase(_TestCase, Config) ->
  158. %% get slave
  159. case proplists:get_value(slave_node_name, Config) of
  160. undefined -> syn_test_suite_helper:clean_after_test();
  161. SlaveNodeName -> syn_test_suite_helper:clean_after_test(SlaveNodeName)
  162. end.
  163. %% ===================================================================
  164. %% Tests
  165. %% ===================================================================
  166. single_node_when_mnesia_is_ram_find_by_key(_Config) ->
  167. %% set schema location
  168. application:set_env(mnesia, schema_location, ram),
  169. %% start
  170. ok = syn:start(),
  171. %% start process
  172. Pid = start_process(),
  173. %% retrieve
  174. undefined = syn:find_by_key(<<"my proc">>),
  175. %% register
  176. ok = syn:register(<<"my proc">>, Pid),
  177. %% retrieve
  178. Pid = syn:find_by_key(<<"my proc">>),
  179. %% kill process
  180. kill_process(Pid),
  181. timer:sleep(100),
  182. %% retrieve
  183. undefined = syn:find_by_key(<<"my proc">>).
  184. single_node_when_mnesia_is_ram_find_by_pid(_Config) ->
  185. %% set schema location
  186. application:set_env(mnesia, schema_location, ram),
  187. %% start
  188. ok = syn:start(),
  189. %% start process
  190. Pid = start_process(),
  191. %% register
  192. ok = syn:register(<<"my proc">>, Pid),
  193. %% retrieve
  194. <<"my proc">> = syn:find_by_pid(Pid),
  195. %% kill process
  196. kill_process(Pid),
  197. timer:sleep(100),
  198. %% retrieve
  199. undefined = syn:find_by_pid(Pid).
  200. single_node_when_mnesia_is_ram_re_register_error(_Config) ->
  201. %% set schema location
  202. application:set_env(mnesia, schema_location, ram),
  203. %% start
  204. ok = syn:start(),
  205. %% start process
  206. Pid = start_process(),
  207. Pid2 = start_process(),
  208. %% register
  209. ok = syn:register(<<"my proc">>, Pid),
  210. {error, taken} = syn:register(<<"my proc">>, Pid2),
  211. %% retrieve
  212. Pid = syn:find_by_key(<<"my proc">>),
  213. %% kill process
  214. kill_process(Pid),
  215. timer:sleep(100),
  216. %% retrieve
  217. undefined = syn:find_by_key(<<"my proc">>),
  218. %% reuse
  219. ok = syn:register(<<"my proc">>, Pid2),
  220. %% retrieve
  221. Pid2 = syn:find_by_key(<<"my proc">>),
  222. %% kill process
  223. kill_process(Pid),
  224. timer:sleep(100),
  225. %% retrieve
  226. undefined = syn:find_by_pid(Pid).
  227. single_node_when_mnesia_is_ram_unregister(_Config) ->
  228. %% set schema location
  229. application:set_env(mnesia, schema_location, ram),
  230. %% start
  231. ok = syn:start(),
  232. %% start process
  233. Pid = start_process(),
  234. %% unregister
  235. {error, undefined} = syn:unregister(<<"my proc">>),
  236. %% register
  237. ok = syn:register(<<"my proc">>, Pid),
  238. %% retrieve
  239. Pid = syn:find_by_key(<<"my proc">>),
  240. %% unregister
  241. ok = syn:unregister(<<"my proc">>),
  242. %% retrieve
  243. undefined = syn:find_by_key(<<"my proc">>),
  244. undefined = syn:find_by_pid(Pid),
  245. %% kill process
  246. kill_process(Pid).
  247. single_node_when_mnesia_is_disc_find_by_key(_Config) ->
  248. %% set schema location
  249. application:set_env(mnesia, schema_location, disc),
  250. %% create schema
  251. mnesia:create_schema([node()]),
  252. %% start
  253. ok = syn:start(),
  254. %% start process
  255. Pid = start_process(),
  256. %% retrieve
  257. undefined = syn:find_by_key(<<"my proc">>),
  258. %% register
  259. ok = syn:register(<<"my proc">>, Pid),
  260. %% retrieve
  261. Pid = syn:find_by_key(<<"my proc">>),
  262. %% kill process
  263. kill_process(Pid),
  264. timer:sleep(100),
  265. %% retrieve
  266. undefined = syn:find_by_key(<<"my proc">>).
  267. two_nodes_when_mnesia_is_ram_find_by_key(Config) ->
  268. %% get slave
  269. SlaveNodeName = proplists:get_value(slave_node_name, Config),
  270. %% set schema location
  271. application:set_env(mnesia, schema_location, ram),
  272. rpc:call(SlaveNodeName, mnesia, schema_location, [ram]),
  273. %% start
  274. ok = syn:start(),
  275. ok = rpc:call(SlaveNodeName, syn, start, []),
  276. timer:sleep(100),
  277. %% start process
  278. Pid = start_process(),
  279. %% retrieve
  280. undefined = syn:find_by_key(<<"my proc">>),
  281. undefined = rpc:call(SlaveNodeName, syn, find_by_key, [<<"my proc">>]),
  282. %% register
  283. ok = syn:register(<<"my proc">>, Pid),
  284. %% retrieve
  285. Pid = syn:find_by_key(<<"my proc">>),
  286. Pid = rpc:call(SlaveNodeName, syn, find_by_key, [<<"my proc">>]),
  287. %% kill process
  288. kill_process(Pid),
  289. timer:sleep(100),
  290. %% retrieve
  291. undefined = syn:find_by_key(<<"my proc">>),
  292. undefined = rpc:call(SlaveNodeName, syn, find_by_key, [<<"my proc">>]).
  293. two_nodes_when_mnesia_is_disc_find_by_pid(Config) ->
  294. %% get slave
  295. SlaveNodeName = proplists:get_value(slave_node_name, Config),
  296. %% set schema location
  297. application:set_env(mnesia, schema_location, disc),
  298. rpc:call(SlaveNodeName, mnesia, schema_location, [disc]),
  299. %% create schema
  300. mnesia:create_schema([node(), SlaveNodeName]),
  301. %% start
  302. ok = syn:start(),
  303. ok = rpc:call(SlaveNodeName, syn, start, []),
  304. timer:sleep(100),
  305. %% start process
  306. Pid = start_process(),
  307. %% register
  308. ok = syn:register(<<"my proc">>, Pid),
  309. %% retrieve
  310. <<"my proc">> = syn:find_by_pid(Pid),
  311. <<"my proc">> = rpc:call(SlaveNodeName, syn, find_by_pid, [Pid]),
  312. %% kill process
  313. kill_process(Pid),
  314. timer:sleep(100),
  315. %% retrieve
  316. undefined = syn:find_by_pid(Pid),
  317. undefined = rpc:call(SlaveNodeName, syn, find_by_pid, [Pid]).
  318. %% ===================================================================
  319. %% Internal
  320. %% ===================================================================
  321. start_process() ->
  322. Pid = spawn(?MODULE, process_main, []),
  323. Pid.
  324. kill_process(Pid) ->
  325. exit(Pid, kill).
  326. process_main() ->
  327. receive
  328. shutdown -> ok
  329. end.