syn_netsplits_SUITE.erl 13 KB

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