syn_netsplits_SUITE.erl 9.1 KB

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