syn_groups_consistency_SUITE.erl 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2016 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. -module(syn_groups_consistency_SUITE).
  26. %% callbacks
  27. -export([all/0]).
  28. -export([init_per_suite/1, end_per_suite/1]).
  29. -export([groups/0, init_per_group/2, end_per_group/2]).
  30. -export([init_per_testcase/2, end_per_testcase/2]).
  31. %% tests
  32. -export([
  33. two_nodes_netsplit_when_there_are_no_conflicts/1
  34. ]).
  35. %% include
  36. -include_lib("common_test/include/ct.hrl").
  37. %% ===================================================================
  38. %% Callbacks
  39. %% ===================================================================
  40. %% -------------------------------------------------------------------
  41. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  42. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  43. %% GroupName = atom()
  44. %% TestCase = atom()
  45. %% Reason = term()
  46. %% -------------------------------------------------------------------
  47. all() ->
  48. [
  49. {group, two_nodes_netsplits}
  50. ].
  51. %% -------------------------------------------------------------------
  52. %% Function: groups() -> [Group]
  53. %% Group = {GroupName,Properties,GroupsAndTestCases}
  54. %% GroupName = atom()
  55. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  56. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  57. %% TestCase = atom()
  58. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  59. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  60. %% repeat_until_any_ok | repeat_until_any_fail
  61. %% N = integer() | forever
  62. %% -------------------------------------------------------------------
  63. groups() ->
  64. [
  65. {two_nodes_netsplits, [shuffle], [
  66. two_nodes_netsplit_when_there_are_no_conflicts
  67. ]}
  68. ].
  69. %% -------------------------------------------------------------------
  70. %% Function: init_per_suite(Config0) ->
  71. %% Config1 | {skip,Reason} |
  72. %% {skip_and_save,Reason,Config1}
  73. %% Config0 = Config1 = [tuple()]
  74. %% Reason = term()
  75. %% -------------------------------------------------------------------
  76. init_per_suite(Config) ->
  77. %% init
  78. SlaveNodeShortName = syn_slave,
  79. %% start slave
  80. {ok, SlaveNode} = syn_test_suite_helper:start_slave(SlaveNodeShortName),
  81. %% config
  82. [
  83. {slave_node_short_name, SlaveNodeShortName},
  84. {slave_node, SlaveNode}
  85. | Config
  86. ].
  87. %% -------------------------------------------------------------------
  88. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  89. %% Config0 = Config1 = [tuple()]
  90. %% -------------------------------------------------------------------
  91. end_per_suite(Config) ->
  92. %% get slave node name
  93. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  94. %% stop slave
  95. syn_test_suite_helper:stop_slave(SlaveNodeShortName).
  96. %% -------------------------------------------------------------------
  97. %% Function: init_per_group(GroupName, Config0) ->
  98. %% Config1 | {skip,Reason} |
  99. %% {skip_and_save,Reason,Config1}
  100. %% GroupName = atom()
  101. %% Config0 = Config1 = [tuple()]
  102. %% Reason = term()
  103. %% -------------------------------------------------------------------
  104. init_per_group(_GroupName, Config) -> Config.
  105. %% -------------------------------------------------------------------
  106. %% Function: end_per_group(GroupName, Config0) ->
  107. %% void() | {save_config,Config1}
  108. %% GroupName = atom()
  109. %% Config0 = Config1 = [tuple()]
  110. %% -------------------------------------------------------------------
  111. end_per_group(_GroupName, _Config) -> ok.
  112. % ----------------------------------------------------------------------------------------------------------
  113. % Function: init_per_testcase(TestCase, Config0) ->
  114. % Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  115. % TestCase = atom()
  116. % Config0 = Config1 = [tuple()]
  117. % Reason = term()
  118. % ----------------------------------------------------------------------------------------------------------
  119. init_per_testcase(_TestCase, Config) ->
  120. %% get slave
  121. SlaveNode = proplists:get_value(slave_node, Config),
  122. %% set schema location
  123. application:set_env(mnesia, schema_location, ram),
  124. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  125. %% return
  126. Config.
  127. % ----------------------------------------------------------------------------------------------------------
  128. % Function: end_per_testcase(TestCase, Config0) ->
  129. % void() | {save_config,Config1} | {fail,Reason}
  130. % TestCase = atom()
  131. % Config0 = Config1 = [tuple()]
  132. % Reason = term()
  133. % ----------------------------------------------------------------------------------------------------------
  134. end_per_testcase(_TestCase, Config) ->
  135. %% get slave
  136. SlaveNode = proplists:get_value(slave_node, Config),
  137. syn_test_suite_helper:clean_after_test(SlaveNode).
  138. %% ===================================================================
  139. %% Tests
  140. %% ===================================================================
  141. two_nodes_netsplit_when_there_are_no_conflicts(Config) ->
  142. %% get slave
  143. SlaveNode = proplists:get_value(slave_node, Config),
  144. CurrentNode = node(),
  145. %% set schema location
  146. application:set_env(mnesia, schema_location, ram),
  147. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  148. %% start syn
  149. ok = syn:start(),
  150. ok = syn:init(),
  151. ok = rpc:call(SlaveNode, syn, start, []),
  152. ok = rpc:call(SlaveNode, syn, init, []),
  153. timer:sleep(100),
  154. %% start processes
  155. LocalPid = syn_test_suite_helper:start_process(),
  156. SlavePidLocal = syn_test_suite_helper:start_process(SlaveNode),
  157. SlavePidSlave = syn_test_suite_helper:start_process(SlaveNode),
  158. %% join
  159. ok = syn:join({group, tuple_name}, LocalPid),
  160. ok = syn:join({group, tuple_name}, SlavePidLocal), %% joined on local node
  161. ok = rpc:call(SlaveNode, syn, join, [{group, tuple_name}, SlavePidSlave]), %% joined on slave node
  162. timer:sleep(100),
  163. %% check tables
  164. 3 = mnesia:table_info(syn_groups_table, size),
  165. 3 = rpc:call(SlaveNode, mnesia, table_info, [syn_groups_table, size]),
  166. LocalActiveReplicas = mnesia:table_info(syn_groups_table, active_replicas),
  167. 2 = length(LocalActiveReplicas),
  168. true = lists:member(SlaveNode, LocalActiveReplicas),
  169. true = lists:member(CurrentNode, LocalActiveReplicas),
  170. SlaveActiveReplicas = rpc:call(SlaveNode, mnesia, table_info, [syn_groups_table, active_replicas]),
  171. 2 = length(SlaveActiveReplicas),
  172. true = lists:member(SlaveNode, SlaveActiveReplicas),
  173. true = lists:member(CurrentNode, SlaveActiveReplicas),
  174. %% simulate net split
  175. syn_test_suite_helper:disconnect_node(SlaveNode),
  176. timer:sleep(1000),
  177. %% check tables
  178. 1 = mnesia:table_info(syn_groups_table, size),
  179. [CurrentNode] = mnesia:table_info(syn_groups_table, active_replicas),
  180. %% reconnect
  181. syn_test_suite_helper:connect_node(SlaveNode),
  182. timer:sleep(1000),
  183. %% check tables
  184. 3 = mnesia:table_info(syn_groups_table, size),
  185. 3 = rpc:call(SlaveNode, mnesia, table_info, [syn_groups_table, size]),
  186. LocalActiveReplicasAfter = mnesia:table_info(syn_groups_table, active_replicas),
  187. 2 = length(LocalActiveReplicasAfter),
  188. true = lists:member(SlaveNode, LocalActiveReplicasAfter),
  189. true = lists:member(CurrentNode, LocalActiveReplicasAfter),
  190. SlaveActiveReplicasAfter = rpc:call(SlaveNode, mnesia, table_info, [syn_groups_table, active_replicas]),
  191. 2 = length(SlaveActiveReplicasAfter),
  192. true = lists:member(SlaveNode, SlaveActiveReplicasAfter),
  193. true = lists:member(CurrentNode, SlaveActiveReplicasAfter),
  194. %% check grouos
  195. 3 = length(syn:get_members({group, tuple_name})),
  196. true = syn:member(LocalPid, {group, tuple_name}),
  197. true = syn:member(SlavePidLocal, {group, tuple_name}),
  198. true = syn:member(SlavePidSlave, {group, tuple_name}),
  199. 3 = length(rpc:call(SlaveNode, syn, get_members, [{group, tuple_name}])),
  200. true = rpc:call(SlaveNode, syn, member, [LocalPid, {group, tuple_name}]),
  201. true = rpc:call(SlaveNode, syn, member, [SlavePidLocal, {group, tuple_name}]),
  202. true = rpc:call(SlaveNode, syn, member, [SlavePidSlave, {group, tuple_name}]).