syn_netsplits_SUITE.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. two_nodes_netsplit_kill_resolution_when_there_are_conflicts/1,
  38. two_nodes_netsplit_message_resolution_when_there_are_conflicts/1
  39. ]).
  40. %% internal
  41. -export([process_reply_main/0]).
  42. %% include
  43. -include_lib("common_test/include/ct.hrl").
  44. %% ===================================================================
  45. %% Callbacks
  46. %% ===================================================================
  47. %% -------------------------------------------------------------------
  48. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  49. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  50. %% GroupName = atom()
  51. %% TestCase = atom()
  52. %% Reason = term()
  53. %% -------------------------------------------------------------------
  54. all() ->
  55. [
  56. {group, two_nodes_netsplits}
  57. ].
  58. %% -------------------------------------------------------------------
  59. %% Function: groups() -> [Group]
  60. %% Group = {GroupName,Properties,GroupsAndTestCases}
  61. %% GroupName = atom()
  62. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  63. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  64. %% TestCase = atom()
  65. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  66. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  67. %% repeat_until_any_ok | repeat_until_any_fail
  68. %% N = integer() | forever
  69. %% -------------------------------------------------------------------
  70. groups() ->
  71. [
  72. {two_nodes_netsplits, [shuffle], [
  73. two_nodes_netsplit_when_there_are_no_conflicts,
  74. two_nodes_netsplit_kill_resolution_when_there_are_conflicts,
  75. two_nodes_netsplit_message_resolution_when_there_are_conflicts
  76. ]}
  77. ].
  78. %% -------------------------------------------------------------------
  79. %% Function: init_per_suite(Config0) ->
  80. %% Config1 | {skip,Reason} |
  81. %% {skip_and_save,Reason,Config1}
  82. %% Config0 = Config1 = [tuple()]
  83. %% Reason = term()
  84. %% -------------------------------------------------------------------
  85. init_per_suite(Config) ->
  86. %% init
  87. SlaveNodeShortName = syn_slave,
  88. %% start slave
  89. {ok, SlaveNode} = syn_test_suite_helper:start_slave(SlaveNodeShortName),
  90. %% config
  91. [
  92. {slave_node_short_name, SlaveNodeShortName},
  93. {slave_node, SlaveNode}
  94. | Config
  95. ].
  96. %% -------------------------------------------------------------------
  97. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  98. %% Config0 = Config1 = [tuple()]
  99. %% -------------------------------------------------------------------
  100. end_per_suite(Config) ->
  101. %% get slave node name
  102. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  103. %% stop slave
  104. syn_test_suite_helper:stop_slave(SlaveNodeShortName).
  105. %% -------------------------------------------------------------------
  106. %% Function: init_per_group(GroupName, Config0) ->
  107. %% Config1 | {skip,Reason} |
  108. %% {skip_and_save,Reason,Config1}
  109. %% GroupName = atom()
  110. %% Config0 = Config1 = [tuple()]
  111. %% Reason = term()
  112. %% -------------------------------------------------------------------
  113. init_per_group(_GroupName, Config) -> Config.
  114. %% -------------------------------------------------------------------
  115. %% Function: end_per_group(GroupName, Config0) ->
  116. %% void() | {save_config,Config1}
  117. %% GroupName = atom()
  118. %% Config0 = Config1 = [tuple()]
  119. %% -------------------------------------------------------------------
  120. end_per_group(_GroupName, _Config) -> ok.
  121. % ----------------------------------------------------------------------------------------------------------
  122. % Function: init_per_testcase(TestCase, Config0) ->
  123. % Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  124. % TestCase = atom()
  125. % Config0 = Config1 = [tuple()]
  126. % Reason = term()
  127. % ----------------------------------------------------------------------------------------------------------
  128. init_per_testcase(_TestCase, Config) ->
  129. %% get slave
  130. SlaveNode = proplists:get_value(slave_node, Config),
  131. %% set schema location
  132. application:set_env(mnesia, schema_location, ram),
  133. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  134. %% return
  135. Config.
  136. % ----------------------------------------------------------------------------------------------------------
  137. % Function: end_per_testcase(TestCase, Config0) ->
  138. % void() | {save_config,Config1} | {fail,Reason}
  139. % TestCase = atom()
  140. % Config0 = Config1 = [tuple()]
  141. % Reason = term()
  142. % ----------------------------------------------------------------------------------------------------------
  143. end_per_testcase(_TestCase, Config) ->
  144. %% get slave
  145. SlaveNode = proplists:get_value(slave_node, Config),
  146. syn_test_suite_helper:clean_after_test(SlaveNode).
  147. %% ===================================================================
  148. %% Tests
  149. %% ===================================================================
  150. two_nodes_netsplit_when_there_are_no_conflicts(Config) ->
  151. %% get slave
  152. SlaveNode = proplists:get_value(slave_node, Config),
  153. CurrentNode = node(),
  154. %% start syn
  155. ok = syn:start(),
  156. ok = rpc:call(SlaveNode, syn, start, []),
  157. timer:sleep(100),
  158. %% start processes
  159. LocalPid = syn_test_suite_helper:start_process(),
  160. SlavePidLocal = syn_test_suite_helper:start_process(SlaveNode),
  161. SlavePidSlave = syn_test_suite_helper:start_process(SlaveNode),
  162. %% register
  163. ok = syn:register(local_pid, LocalPid),
  164. ok = syn:register(slave_pid_local, SlavePidLocal), %% slave registered on local node
  165. ok = rpc:call(SlaveNode, syn, register, [slave_pid_slave, SlavePidSlave]), %% slave registered on slave node
  166. timer:sleep(100),
  167. %% check tables
  168. 3 = mnesia:table_info(syn_processes_table, size),
  169. 3 = rpc:call(SlaveNode, mnesia, table_info, [syn_processes_table, size]),
  170. LocalActiveReplicas = mnesia:table_info(syn_processes_table, active_replicas),
  171. 2 = length(LocalActiveReplicas),
  172. true = lists:member(SlaveNode, LocalActiveReplicas),
  173. true = lists:member(CurrentNode, LocalActiveReplicas),
  174. SlaveActiveReplicas = rpc:call(SlaveNode, mnesia, table_info, [syn_processes_table, active_replicas]),
  175. 2 = length(SlaveActiveReplicas),
  176. true = lists:member(SlaveNode, SlaveActiveReplicas),
  177. true = lists:member(CurrentNode, SlaveActiveReplicas),
  178. %% simulate net split
  179. syn_test_suite_helper:disconnect_node(SlaveNode),
  180. timer:sleep(1000),
  181. %% check tables
  182. 1 = mnesia:table_info(syn_processes_table, size),
  183. [CurrentNode] = mnesia:table_info(syn_processes_table, active_replicas),
  184. %% reconnect
  185. syn_test_suite_helper:connect_node(SlaveNode),
  186. timer:sleep(1000),
  187. %% check tables
  188. 3 = mnesia:table_info(syn_processes_table, size),
  189. 3 = rpc:call(SlaveNode, mnesia, table_info, [syn_processes_table, size]),
  190. LocalActiveReplicas2 = mnesia:table_info(syn_processes_table, active_replicas),
  191. 2 = length(LocalActiveReplicas2),
  192. true = lists:member(SlaveNode, LocalActiveReplicas2),
  193. true = lists:member(CurrentNode, LocalActiveReplicas2),
  194. SlaveActiveReplicas2 = rpc:call(SlaveNode, mnesia, table_info, [syn_processes_table, active_replicas]),
  195. 2 = length(SlaveActiveReplicas2),
  196. true = lists:member(SlaveNode, SlaveActiveReplicas2),
  197. true = lists:member(CurrentNode, SlaveActiveReplicas2),
  198. %% check processes
  199. LocalPid = syn:find_by_key(local_pid),
  200. SlavePidLocal = syn:find_by_key(slave_pid_local),
  201. SlavePidSlave = syn:find_by_key(slave_pid_slave),
  202. LocalPid = rpc:call(SlaveNode, syn, find_by_key, [local_pid]),
  203. SlavePidLocal = rpc:call(SlaveNode, syn, find_by_key, [slave_pid_local]),
  204. SlavePidSlave = rpc:call(SlaveNode, syn, find_by_key, [slave_pid_slave]),
  205. %% kill processes
  206. syn_test_suite_helper:kill_process(LocalPid),
  207. syn_test_suite_helper:kill_process(SlavePidLocal),
  208. syn_test_suite_helper:kill_process(SlavePidSlave).
  209. two_nodes_netsplit_kill_resolution_when_there_are_conflicts(Config) ->
  210. %% get slave
  211. SlaveNode = proplists:get_value(slave_node, Config),
  212. CurrentNode = node(),
  213. %% start syn
  214. ok = syn:start(),
  215. ok = rpc:call(SlaveNode, syn, start, []),
  216. timer:sleep(100),
  217. %% start processes
  218. LocalPid = syn_test_suite_helper:start_process(),
  219. SlavePid = syn_test_suite_helper:start_process(SlaveNode),
  220. %% register
  221. ok = syn:register(conflicting_key, SlavePid),
  222. timer:sleep(100),
  223. %% check tables
  224. 1 = mnesia:table_info(syn_processes_table, size),
  225. 1 = rpc:call(SlaveNode, mnesia, table_info, [syn_processes_table, size]),
  226. %% check process
  227. SlavePid = syn:find_by_key(conflicting_key),
  228. %% simulate net split
  229. syn_test_suite_helper:disconnect_node(SlaveNode),
  230. timer:sleep(1000),
  231. %% check tables
  232. 0 = mnesia:table_info(syn_processes_table, size),
  233. [CurrentNode] = mnesia:table_info(syn_processes_table, active_replicas),
  234. %% now register the local pid with the same key
  235. ok = syn:register(conflicting_key, LocalPid),
  236. %% check process
  237. LocalPid = syn:find_by_key(conflicting_key),
  238. %% reconnect
  239. syn_test_suite_helper:connect_node(SlaveNode),
  240. timer:sleep(1000),
  241. %% check tables
  242. 1 = mnesia:table_info(syn_processes_table, size),
  243. 1 = rpc:call(SlaveNode, mnesia, table_info, [syn_processes_table, size]),
  244. %% check process
  245. FoundPid = syn:find_by_key(conflicting_key),
  246. true = lists:member(FoundPid, [LocalPid, SlavePid]),
  247. %% kill processes
  248. syn_test_suite_helper:kill_process(LocalPid),
  249. syn_test_suite_helper:kill_process(SlavePid).
  250. two_nodes_netsplit_message_resolution_when_there_are_conflicts(Config) ->
  251. %% get slave
  252. SlaveNode = proplists:get_value(slave_node, Config),
  253. CurrentNode = node(),
  254. %% load configuration variables from syn-test.config => this sets the netsplit_send_message_to_process option
  255. syn_test_suite_helper:set_environment_variables(),
  256. syn_test_suite_helper:set_environment_variables(SlaveNode),
  257. %% start syn
  258. ok = syn:start(),
  259. ok = rpc:call(SlaveNode, syn, start, []),
  260. timer:sleep(100),
  261. %% start processes
  262. LocalPid = syn_test_suite_helper:start_process(fun process_reply_main/0),
  263. SlavePid = syn_test_suite_helper:start_process(SlaveNode, fun process_reply_main/0),
  264. %% register global process
  265. ResultPid = self(),
  266. global:register_name(syn_netsplits_SUITE_result, ResultPid),
  267. %% register
  268. ok = syn:register(conflicting_key, SlavePid),
  269. timer:sleep(100),
  270. %% check tables
  271. 1 = mnesia:table_info(syn_processes_table, size),
  272. 1 = rpc:call(SlaveNode, mnesia, table_info, [syn_processes_table, size]),
  273. %% check process
  274. SlavePid = syn:find_by_key(conflicting_key),
  275. %% simulate net split
  276. syn_test_suite_helper:disconnect_node(SlaveNode),
  277. timer:sleep(1000),
  278. %% check tables
  279. 0 = mnesia:table_info(syn_processes_table, size),
  280. [CurrentNode] = mnesia:table_info(syn_processes_table, active_replicas),
  281. %% now register the local pid with the same key
  282. ok = syn:register(conflicting_key, LocalPid),
  283. %% check process
  284. LocalPid = syn:find_by_key(conflicting_key),
  285. %% reconnect
  286. syn_test_suite_helper:connect_node(SlaveNode),
  287. timer:sleep(1000),
  288. %% check tables
  289. 1 = mnesia:table_info(syn_processes_table, size),
  290. 1 = rpc:call(SlaveNode, mnesia, table_info, [syn_processes_table, size]),
  291. %% check process
  292. FoundPid = syn:find_by_key(conflicting_key),
  293. true = lists:member(FoundPid, [LocalPid, SlavePid]),
  294. %% check message received from killed pid
  295. KilledPid = lists:nth(1, lists:delete(FoundPid, [LocalPid, SlavePid])),
  296. receive
  297. {exited, KilledPid} -> ok
  298. after 2000 ->
  299. ok = conflicting_process_did_not_receive_message
  300. end,
  301. %% kill processes
  302. syn_test_suite_helper:kill_process(LocalPid),
  303. syn_test_suite_helper:kill_process(SlavePid).
  304. %% ===================================================================
  305. %% Internal
  306. %% ===================================================================
  307. process_reply_main() ->
  308. receive
  309. shutdown ->
  310. timer:sleep(100), %% wait for global processes to propagate
  311. global:send(syn_netsplits_SUITE_result, {exited, self()})
  312. end.