syn_register_processes_SUITE.erl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. %% tests
  35. -export([
  36. single_node_when_mnesia_is_ram_find_by_key/1,
  37. single_node_when_mnesia_is_ram_find_by_pid/1
  38. ]).
  39. %% include
  40. -include_lib("common_test/include/ct.hrl").
  41. %% ===================================================================
  42. %% Callbacks
  43. %% ===================================================================
  44. %% -------------------------------------------------------------------
  45. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  46. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  47. %% GroupName = atom()
  48. %% TestCase = atom()
  49. %% Reason = term()
  50. %% -------------------------------------------------------------------
  51. all() ->
  52. [
  53. {group, single_node_process_registration}
  54. ].
  55. %% -------------------------------------------------------------------
  56. %% Function: groups() -> [Group]
  57. %% Group = {GroupName,Properties,GroupsAndTestCases}
  58. %% GroupName = atom()
  59. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  60. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  61. %% TestCase = atom()
  62. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  63. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  64. %% repeat_until_any_ok | repeat_until_any_fail
  65. %% N = integer() | forever
  66. %% -------------------------------------------------------------------
  67. groups() ->
  68. [
  69. {single_node_process_registration, [shuffle], [
  70. single_node_when_mnesia_is_ram_find_by_key,
  71. single_node_when_mnesia_is_ram_find_by_pid
  72. ]}
  73. ].
  74. %% -------------------------------------------------------------------
  75. %% Function: init_per_suite(Config0) ->
  76. %% Config1 | {skip,Reason} |
  77. %% {skip_and_save,Reason,Config1}
  78. %% Config0 = Config1 = [tuple()]
  79. %% Reason = term()
  80. %% -------------------------------------------------------------------
  81. init_per_suite(Config) ->
  82. %% config
  83. [
  84. {slave_node_short_name, syn_slave}
  85. | Config
  86. ].
  87. %% -------------------------------------------------------------------
  88. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  89. %% Config0 = Config1 = [tuple()]
  90. %% -------------------------------------------------------------------
  91. end_per_suite(_Config) -> ok.
  92. %% -------------------------------------------------------------------
  93. %% Function: init_per_group(GroupName, Config0) ->
  94. %% Config1 | {skip,Reason} |
  95. %% {skip_and_save,Reason,Config1}
  96. %% GroupName = atom()
  97. %% Config0 = Config1 = [tuple()]
  98. %% Reason = term()
  99. %% -------------------------------------------------------------------
  100. init_per_group(_GroupName, Config) -> Config.
  101. %% -------------------------------------------------------------------
  102. %% Function: end_per_group(GroupName, Config0) ->
  103. %% void() | {save_config,Config1}
  104. %% GroupName = atom()
  105. %% Config0 = Config1 = [tuple()]
  106. %% -------------------------------------------------------------------
  107. end_per_group(_GroupName, _Config) ->
  108. clean_after_test().
  109. %% ===================================================================
  110. %% Tests
  111. %% ===================================================================
  112. single_node_when_mnesia_is_ram_find_by_key(_Config) ->
  113. %% set schema location
  114. application:set_env(mnesia, schema_location, ram),
  115. %% start
  116. ok = syn:start(),
  117. %% start process
  118. Pid = start_process(),
  119. %% retrieve
  120. undefined = syn:find_by_key(<<"my proc">>),
  121. %% register
  122. syn:register(<<"my proc">>, Pid),
  123. %% retrieve
  124. Pid = syn:find_by_key(<<"my proc">>),
  125. %% kill process
  126. kill_process(Pid),
  127. timer:sleep(100),
  128. %% retrieve
  129. undefined = syn:find_by_key(<<"my proc">>).
  130. single_node_when_mnesia_is_ram_find_by_pid(_Config) ->
  131. %% set schema location
  132. application:set_env(mnesia, schema_location, ram),
  133. %% start
  134. ok = syn:start(),
  135. %% start process
  136. Pid = start_process(),
  137. %% register
  138. syn:register(<<"my proc">>, Pid),
  139. %% retrieve
  140. <<"my proc">> = syn:find_by_pid(Pid),
  141. %% kill process
  142. kill_process(Pid),
  143. timer:sleep(100),
  144. %% retrieve
  145. undefined = syn:find_by_pid(Pid).
  146. %% ===================================================================
  147. %% Internal
  148. %% ===================================================================
  149. clean_after_test() ->
  150. %% stop mnesia
  151. mnesia:stop(),
  152. %% delete schema
  153. mnesia:delete_schema([node()]),
  154. %% stop syn
  155. syn:stop().
  156. start_process() ->
  157. Pid = spawn(?MODULE, process_main, []),
  158. Pid.
  159. kill_process(Pid) ->
  160. exit(Pid, kill).
  161. process_main() ->
  162. receive
  163. shutdown -> ok
  164. end.