syn_groups_SUITE.erl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2019 Roberto Ostinelli <roberto@ostinelli.net> and Neato Robotics, Inc.
  7. %%
  8. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  9. %% of this software and associated documentation files (the "Software"), to deal
  10. %% in the Software without restriction, including without limitation the rights
  11. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. %% copies of the Software, and to permit persons to whom the Software is
  13. %% furnished to do so, subject to the following conditions:
  14. %%
  15. %% The above copyright notice and this permission notice shall be included in
  16. %% all copies or substantial portions of the Software.
  17. %%
  18. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. %% THE SOFTWARE.
  25. %% ==========================================================================================================
  26. -module(syn_groups_SUITE).
  27. %% callbacks
  28. -export([all/0]).
  29. -export([init_per_suite/1, end_per_suite/1]).
  30. -export([groups/0, init_per_group/2, end_per_group/2]).
  31. -export([init_per_testcase/2, end_per_testcase/2]).
  32. %% tests
  33. -export([
  34. single_node_join_and_monitor/1,
  35. single_node_join_and_leave/1
  36. ]).
  37. %% include
  38. -include_lib("common_test/include/ct.hrl").
  39. %% ===================================================================
  40. %% Callbacks
  41. %% ===================================================================
  42. %% -------------------------------------------------------------------
  43. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  44. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  45. %% GroupName = atom()
  46. %% TestCase = atom()
  47. %% Reason = term()
  48. %% -------------------------------------------------------------------
  49. all() ->
  50. [
  51. {group, single_node_groups}
  52. ].
  53. %% -------------------------------------------------------------------
  54. %% Function: groups() -> [Group]
  55. %% Group = {GroupName,Properties,GroupsAndTestCases}
  56. %% GroupName = atom()
  57. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  58. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  59. %% TestCase = atom()
  60. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  61. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  62. %% repeat_until_any_ok | repeat_until_any_fail
  63. %% N = integer() | forever
  64. %% -------------------------------------------------------------------
  65. groups() ->
  66. [
  67. {single_node_groups, [shuffle], [
  68. single_node_join_and_monitor,
  69. single_node_join_and_leave
  70. ]}
  71. ].
  72. %% -------------------------------------------------------------------
  73. %% Function: init_per_suite(Config0) ->
  74. %% Config1 | {skip,Reason} |
  75. %% {skip_and_save,Reason,Config1}
  76. %% Config0 = Config1 = [tuple()]
  77. %% Reason = term()
  78. %% -------------------------------------------------------------------
  79. init_per_suite(Config) ->
  80. Config.
  81. %% -------------------------------------------------------------------
  82. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  83. %% Config0 = Config1 = [tuple()]
  84. %% -------------------------------------------------------------------
  85. end_per_suite(_Config) ->
  86. ok.
  87. %% -------------------------------------------------------------------
  88. %% Function: init_per_group(GroupName, Config0) ->
  89. %% Config1 | {skip,Reason} |
  90. %% {skip_and_save,Reason,Config1}
  91. %% GroupName = atom()
  92. %% Config0 = Config1 = [tuple()]
  93. %% Reason = term()
  94. %% -------------------------------------------------------------------
  95. init_per_group(two_nodes_process_registration, Config) ->
  96. %% start slave
  97. {ok, SlaveNode} = syn_test_suite_helper:start_slave(syn_slave),
  98. %% config
  99. [{slave_node, SlaveNode} | Config];
  100. init_per_group(three_nodes_process_registration, Config) ->
  101. %% start slave
  102. {ok, SlaveNode1} = syn_test_suite_helper:start_slave(syn_slave_1),
  103. {ok, SlaveNode2} = syn_test_suite_helper:start_slave(syn_slave_2),
  104. %% config
  105. [{slave_node_1, SlaveNode1}, {slave_node_2, SlaveNode2} | Config];
  106. init_per_group(_GroupName, Config) ->
  107. Config.
  108. %% -------------------------------------------------------------------
  109. %% Function: end_per_group(GroupName, Config0) ->
  110. %% void() | {save_config,Config1}
  111. %% GroupName = atom()
  112. %% Config0 = Config1 = [tuple()]
  113. %% -------------------------------------------------------------------
  114. end_per_group(two_nodes_process_registration, Config) ->
  115. SlaveNode = proplists:get_value(slave_node, Config),
  116. syn_test_suite_helper:connect_node(SlaveNode),
  117. syn_test_suite_helper:stop_slave(syn_slave),
  118. timer:sleep(1000);
  119. end_per_group(three_nodes_process_registration, Config) ->
  120. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  121. syn_test_suite_helper:connect_node(SlaveNode1),
  122. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  123. syn_test_suite_helper:connect_node(SlaveNode2),
  124. syn_test_suite_helper:stop_slave(syn_slave_1),
  125. syn_test_suite_helper:stop_slave(syn_slave_2),
  126. timer:sleep(1000);
  127. end_per_group(_GroupName, _Config) ->
  128. ok.
  129. %% -------------------------------------------------------------------
  130. %% Function: init_per_testcase(TestCase, Config0) ->
  131. %% Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  132. %% TestCase = atom()
  133. %% Config0 = Config1 = [tuple()]
  134. %% Reason = term()
  135. %% -------------------------------------------------------------------
  136. init_per_testcase(_TestCase, Config) ->
  137. Config.
  138. %% -------------------------------------------------------------------
  139. %% Function: end_per_testcase(TestCase, Config0) ->
  140. %% void() | {save_config,Config1} | {fail,Reason}
  141. %% TestCase = atom()
  142. %% Config0 = Config1 = [tuple()]
  143. %% Reason = term()
  144. %% -------------------------------------------------------------------
  145. end_per_testcase(_, _Config) ->
  146. syn_test_suite_helper:clean_after_test().
  147. %% ===================================================================
  148. %% Tests
  149. %% ===================================================================
  150. single_node_join_and_monitor(_Config) ->
  151. GroupName = "my group",
  152. %% start
  153. ok = syn:start(),
  154. %% start processes
  155. Pid = syn_test_suite_helper:start_process(),
  156. PidWithMeta = syn_test_suite_helper:start_process(),
  157. %% retrieve
  158. [] = syn:get_members(GroupName),
  159. [] = syn:get_members(GroupName, with_meta),
  160. false = syn:member(Pid, GroupName),
  161. false = syn:member(PidWithMeta, GroupName),
  162. %% join
  163. ok = syn:join(GroupName, Pid),
  164. ok = syn:join(GroupName, PidWithMeta, {with, meta}),
  165. %% retrieve
  166. true = syn:member(Pid, GroupName),
  167. true = syn:member(PidWithMeta, GroupName),
  168. true = lists:sort([Pid, PidWithMeta]) =:= lists:sort(syn:get_members(GroupName)),
  169. true = lists:sort([{Pid, undefined}, {PidWithMeta, {with, meta}}]) =:= lists:sort(syn:get_members(GroupName, with_meta)),
  170. %% re-join
  171. ok = syn:join(GroupName, PidWithMeta, {with2, meta2}),
  172. ct:pal("HERE ~p",[lists:sort(syn:get_members(GroupName, with_meta))]),
  173. true = lists:sort([{Pid, undefined}, {PidWithMeta, {with2, meta2}}]) =:= lists:sort(syn:get_members(GroupName, with_meta)),
  174. %% kill process
  175. syn_test_suite_helper:kill_process(Pid),
  176. syn_test_suite_helper:kill_process(PidWithMeta),
  177. timer:sleep(100),
  178. %% retrieve
  179. [] = syn:get_members(GroupName),
  180. [] = syn:get_members(GroupName, with_meta),
  181. false = syn:member(Pid, GroupName),
  182. false = syn:member(PidWithMeta, GroupName).
  183. single_node_join_and_leave(_Config) ->
  184. GroupName = "my group",
  185. %% start
  186. ok = syn:start(),
  187. %% start processes
  188. Pid = syn_test_suite_helper:start_process(),
  189. PidWithMeta = syn_test_suite_helper:start_process(),
  190. %% retrieve
  191. [] = syn:get_members(GroupName),
  192. [] = syn:get_members(GroupName, with_meta),
  193. false = syn:member(Pid, GroupName),
  194. false = syn:member(PidWithMeta, GroupName),
  195. %% join
  196. ok = syn:join(GroupName, Pid),
  197. ok = syn:join(GroupName, PidWithMeta, {with, meta}),
  198. %% retrieve
  199. true = syn:member(Pid, GroupName),
  200. true = syn:member(PidWithMeta, GroupName),
  201. true = lists:sort([Pid, PidWithMeta]) =:= lists:sort(syn:get_members(GroupName)),
  202. true = lists:sort([{Pid, undefined}, {PidWithMeta, {with, meta}}]) =:= lists:sort(syn:get_members(GroupName, with_meta)),
  203. %% leave
  204. ok = syn:leave(GroupName, Pid),
  205. ok = syn:leave(GroupName, PidWithMeta),
  206. timer:sleep(100),
  207. {error, not_in_group} = syn:leave(GroupName, Pid),
  208. {error, not_in_group} = syn:leave(GroupName, PidWithMeta),
  209. %% retrieve
  210. [] = syn:get_members(GroupName),
  211. [] = syn:get_members(GroupName, with_meta),
  212. false = syn:member(Pid, GroupName),
  213. false = syn:member(PidWithMeta, GroupName).