syn_register_processes_SUITE.erl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. %% internal
  35. -export([process_main/0]).
  36. %% tests
  37. -export([
  38. single_node_when_mnesia_is_ram_find_by_key/1,
  39. single_node_when_mnesia_is_ram_find_by_pid/1,
  40. single_node_when_mnesia_is_ram_re_register_error/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_disc_find_by_key
  83. ]},
  84. {two_nodes_process_registration, [shuffle], [
  85. two_nodes_when_mnesia_is_ram_find_by_key,
  86. two_nodes_when_mnesia_is_disc_find_by_pid
  87. ]}
  88. ].
  89. %% -------------------------------------------------------------------
  90. %% Function: init_per_suite(Config0) ->
  91. %% Config1 | {skip,Reason} |
  92. %% {skip_and_save,Reason,Config1}
  93. %% Config0 = Config1 = [tuple()]
  94. %% Reason = term()
  95. %% -------------------------------------------------------------------
  96. init_per_suite(Config) ->
  97. %% config
  98. [
  99. {slave_node_short_name, syn_slave}
  100. | Config
  101. ].
  102. %% -------------------------------------------------------------------
  103. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  104. %% Config0 = Config1 = [tuple()]
  105. %% -------------------------------------------------------------------
  106. end_per_suite(_Config) -> ok.
  107. %% -------------------------------------------------------------------
  108. %% Function: init_per_group(GroupName, Config0) ->
  109. %% Config1 | {skip,Reason} |
  110. %% {skip_and_save,Reason,Config1}
  111. %% GroupName = atom()
  112. %% Config0 = Config1 = [tuple()]
  113. %% Reason = term()
  114. %% -------------------------------------------------------------------
  115. init_per_group(two_nodes_process_registration, Config) ->
  116. %% get slave node short name
  117. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  118. {ok, SlaveNodeName} = syn_test_suite_helper:start_slave(SlaveNodeShortName),
  119. %% config
  120. [
  121. {slave_node_name, SlaveNodeName}
  122. | Config
  123. ];
  124. init_per_group(_GroupName, Config) -> Config.
  125. %% -------------------------------------------------------------------
  126. %% Function: end_per_group(GroupName, Config0) ->
  127. %% void() | {save_config,Config1}
  128. %% GroupName = atom()
  129. %% Config0 = Config1 = [tuple()]
  130. %% -------------------------------------------------------------------
  131. end_per_group(two_nodes_mnesia_creation, Config) ->
  132. %% get slave node name
  133. SlaveNodeName = proplists:get_value(slave_node_name, Config),
  134. %% clean
  135. syn_test_suite_helper:clean_after_test(SlaveNodeName),
  136. %% stop slave
  137. syn_test_suite_helper:stop_slave(SlaveNodeName);
  138. end_per_group(_GroupName, _Config) ->
  139. syn_test_suite_helper:clean_after_test().
  140. %% ===================================================================
  141. %% Tests
  142. %% ===================================================================
  143. single_node_when_mnesia_is_ram_find_by_key(_Config) ->
  144. %% set schema location
  145. application:set_env(mnesia, schema_location, ram),
  146. %% start
  147. ok = syn:start(),
  148. %% start process
  149. Pid = start_process(),
  150. %% retrieve
  151. undefined = syn:find_by_key(<<"my proc">>),
  152. %% register
  153. ok = syn:register(<<"my proc">>, Pid),
  154. %% retrieve
  155. Pid = syn:find_by_key(<<"my proc">>),
  156. %% kill process
  157. kill_process(Pid),
  158. timer:sleep(100),
  159. %% retrieve
  160. undefined = syn:find_by_key(<<"my proc">>).
  161. single_node_when_mnesia_is_ram_find_by_pid(_Config) ->
  162. %% set schema location
  163. application:set_env(mnesia, schema_location, ram),
  164. %% start
  165. ok = syn:start(),
  166. %% start process
  167. Pid = start_process(),
  168. %% register
  169. ok = syn:register(<<"my proc">>, Pid),
  170. %% retrieve
  171. <<"my proc">> = syn:find_by_pid(Pid),
  172. %% kill process
  173. kill_process(Pid),
  174. timer:sleep(100),
  175. %% retrieve
  176. undefined = syn:find_by_pid(Pid).
  177. single_node_when_mnesia_is_ram_re_register_error(_Config) ->
  178. %% set schema location
  179. application:set_env(mnesia, schema_location, ram),
  180. %% start
  181. ok = syn:start(),
  182. %% start process
  183. Pid = start_process(),
  184. Pid2 = start_process(),
  185. %% register
  186. ok = syn:register(<<"my proc">>, Pid),
  187. {error, already_taken} = syn:register(<<"my proc">>, Pid2),
  188. %% retrieve
  189. Pid = syn:find_by_key(<<"my proc">>),
  190. %% kill process
  191. kill_process(Pid),
  192. timer:sleep(100),
  193. %% retrieve
  194. undefined = syn:find_by_key(<<"my proc">>),
  195. %% reuse
  196. ok = syn:register(<<"my proc">>, Pid2),
  197. %% retrieve
  198. Pid2 = syn:find_by_key(<<"my proc">>),
  199. %% kill process
  200. kill_process(Pid),
  201. timer:sleep(100),
  202. %% retrieve
  203. undefined = syn:find_by_pid(Pid).
  204. single_node_when_mnesia_is_disc_find_by_key(_Config) ->
  205. %% set schema location
  206. application:set_env(mnesia, schema_location, disc),
  207. %% create schema
  208. mnesia:create_schema([node()]),
  209. %% start
  210. ok = syn:start(),
  211. %% start process
  212. Pid = start_process(),
  213. %% retrieve
  214. undefined = syn:find_by_key(<<"my proc">>),
  215. %% register
  216. ok = syn:register(<<"my proc">>, Pid),
  217. %% retrieve
  218. Pid = syn:find_by_key(<<"my proc">>),
  219. %% kill process
  220. kill_process(Pid),
  221. timer:sleep(100),
  222. %% retrieve
  223. undefined = syn:find_by_key(<<"my proc">>).
  224. two_nodes_when_mnesia_is_ram_find_by_key(Config) ->
  225. %% get slave
  226. SlaveNodeName = proplists:get_value(slave_node_name, Config),
  227. %% set schema location
  228. application:set_env(mnesia, schema_location, ram),
  229. rpc:call(SlaveNodeName, mnesia, schema_location, [ram]),
  230. %% start
  231. ok = syn:start(),
  232. ok = rpc:call(SlaveNodeName, syn, start, []),
  233. timer:sleep(100),
  234. %% start process
  235. Pid = start_process(),
  236. %% retrieve
  237. undefined = syn:find_by_key(<<"my proc">>),
  238. undefined = rpc:call(SlaveNodeName, syn, find_by_key, [<<"my proc">>]),
  239. %% register
  240. ok = syn:register(<<"my proc">>, Pid),
  241. %% retrieve
  242. Pid = syn:find_by_key(<<"my proc">>),
  243. Pid = rpc:call(SlaveNodeName, syn, find_by_key, [<<"my proc">>]),
  244. %% kill process
  245. kill_process(Pid),
  246. timer:sleep(100),
  247. %% retrieve
  248. undefined = syn:find_by_key(<<"my proc">>),
  249. undefined = rpc:call(SlaveNodeName, syn, find_by_key, [<<"my proc">>]).
  250. two_nodes_when_mnesia_is_disc_find_by_pid(Config) ->
  251. %% get slave
  252. SlaveNodeName = proplists:get_value(slave_node_name, Config),
  253. %% set schema location
  254. application:set_env(mnesia, schema_location, disc),
  255. rpc:call(SlaveNodeName, mnesia, schema_location, [disc]),
  256. %% create schema
  257. mnesia:create_schema([node(), SlaveNodeName]),
  258. %% start
  259. ok = syn:start(),
  260. ok = rpc:call(SlaveNodeName, syn, start, []),
  261. timer:sleep(100),
  262. %% start process
  263. Pid = start_process(),
  264. %% register
  265. ok = syn:register(<<"my proc">>, Pid),
  266. %% retrieve
  267. <<"my proc">> = syn:find_by_pid(Pid),
  268. <<"my proc">> = rpc:call(SlaveNodeName, syn, find_by_pid, [Pid]),
  269. %% kill process
  270. kill_process(Pid),
  271. timer:sleep(100),
  272. %% retrieve
  273. undefined = syn:find_by_pid(Pid),
  274. undefined = rpc:call(SlaveNodeName, syn, find_by_pid, [Pid]).
  275. %% ===================================================================
  276. %% Internal
  277. %% ===================================================================
  278. start_process() ->
  279. Pid = spawn(?MODULE, process_main, []),
  280. Pid.
  281. kill_process(Pid) ->
  282. exit(Pid, kill).
  283. process_main() ->
  284. receive
  285. shutdown -> ok
  286. end.