syn_register_processes_SUITE.erl 12 KB

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