syn_consistency_SUITE.erl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. %% ==========================================================================================================
  2. %% Syn - A global process registry.
  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_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. two_nodes_netsplit_kill_resolution_when_there_are_conflicts/1,
  35. two_nodes_netsplit_callback_resolution_when_there_are_conflicts/1
  36. ]).
  37. -export([
  38. three_nodes_netsplit_kill_resolution_when_there_are_conflicts/1
  39. ]).
  40. %% internal
  41. -export([process_reply_main/0]).
  42. -export([conflicting_process_callback_dummy/3]).
  43. %% include
  44. -include_lib("common_test/include/ct.hrl").
  45. %% ===================================================================
  46. %% Callbacks
  47. %% ===================================================================
  48. %% -------------------------------------------------------------------
  49. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  50. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  51. %% GroupName = atom()
  52. %% TestCase = atom()
  53. %% Reason = term()
  54. %% -------------------------------------------------------------------
  55. all() ->
  56. [
  57. {group, two_nodes_netsplits},
  58. {group, three_nodes_netsplits}
  59. ].
  60. %% -------------------------------------------------------------------
  61. %% Function: groups() -> [Group]
  62. %% Group = {GroupName,Properties,GroupsAndTestCases}
  63. %% GroupName = atom()
  64. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  65. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  66. %% TestCase = atom()
  67. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  68. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  69. %% repeat_until_any_ok | repeat_until_any_fail
  70. %% N = integer() | forever
  71. %% -------------------------------------------------------------------
  72. groups() ->
  73. [
  74. {two_nodes_netsplits, [shuffle], [
  75. two_nodes_netsplit_when_there_are_no_conflicts,
  76. two_nodes_netsplit_kill_resolution_when_there_are_conflicts,
  77. two_nodes_netsplit_callback_resolution_when_there_are_conflicts
  78. ]},
  79. {three_nodes_netsplits, [shuffle], [
  80. three_nodes_netsplit_kill_resolution_when_there_are_conflicts
  81. ]}
  82. ].
  83. %% -------------------------------------------------------------------
  84. %% Function: init_per_suite(Config0) ->
  85. %% Config1 | {skip,Reason} |
  86. %% {skip_and_save,Reason,Config1}
  87. %% Config0 = Config1 = [tuple()]
  88. %% Reason = term()
  89. %% -------------------------------------------------------------------
  90. init_per_suite(Config) ->
  91. %% init
  92. SlaveNodeShortName = syn_slave,
  93. %% start slave
  94. {ok, SlaveNode} = syn_test_suite_helper:start_slave(SlaveNodeShortName),
  95. %% config
  96. [
  97. {slave_node_short_name, SlaveNodeShortName},
  98. {slave_node, SlaveNode}
  99. | Config
  100. ].
  101. %% -------------------------------------------------------------------
  102. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  103. %% Config0 = Config1 = [tuple()]
  104. %% -------------------------------------------------------------------
  105. end_per_suite(Config) ->
  106. %% get slave node name
  107. SlaveNodeShortName = proplists:get_value(slave_node_short_name, Config),
  108. %% stop slave
  109. syn_test_suite_helper:stop_slave(SlaveNodeShortName).
  110. %% -------------------------------------------------------------------
  111. %% Function: init_per_group(GroupName, Config0) ->
  112. %% Config1 | {skip,Reason} |
  113. %% {skip_and_save,Reason,Config1}
  114. %% GroupName = atom()
  115. %% Config0 = Config1 = [tuple()]
  116. %% Reason = term()
  117. %% -------------------------------------------------------------------
  118. init_per_group(three_nodes_netsplits, Config) ->
  119. %% init
  120. SlaveNode2ShortName = syn_slave_2,
  121. %% start slave 2
  122. {ok, SlaveNode2} = syn_test_suite_helper:start_slave(SlaveNode2ShortName),
  123. %% config
  124. [
  125. {slave_node_2_short_name, SlaveNode2ShortName},
  126. {slave_node_2, SlaveNode2}
  127. | Config
  128. ];
  129. init_per_group(_GroupName, Config) -> Config.
  130. %% -------------------------------------------------------------------
  131. %% Function: end_per_group(GroupName, Config0) ->
  132. %% void() | {save_config,Config1}
  133. %% GroupName = atom()
  134. %% Config0 = Config1 = [tuple()]
  135. %% -------------------------------------------------------------------
  136. end_per_group(three_nodes_netsplits, Config) ->
  137. %% get slave node 2 name
  138. SlaveNode2ShortName = proplists:get_value(slave_node_2_short_name, Config),
  139. %% stop slave
  140. syn_test_suite_helper:stop_slave(SlaveNode2ShortName);
  141. end_per_group(_GroupName, _Config) -> ok.
  142. % ----------------------------------------------------------------------------------------------------------
  143. % Function: init_per_testcase(TestCase, Config0) ->
  144. % Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  145. % TestCase = atom()
  146. % Config0 = Config1 = [tuple()]
  147. % Reason = term()
  148. % ----------------------------------------------------------------------------------------------------------
  149. init_per_testcase(_TestCase, Config) ->
  150. %% get slave
  151. SlaveNode = proplists:get_value(slave_node, Config),
  152. %% set schema location
  153. application:set_env(mnesia, schema_location, ram),
  154. rpc:call(SlaveNode, mnesia, schema_location, [ram]),
  155. %% return
  156. Config.
  157. % ----------------------------------------------------------------------------------------------------------
  158. % Function: end_per_testcase(TestCase, Config0) ->
  159. % void() | {save_config,Config1} | {fail,Reason}
  160. % TestCase = atom()
  161. % Config0 = Config1 = [tuple()]
  162. % Reason = term()
  163. % ----------------------------------------------------------------------------------------------------------
  164. end_per_testcase(_TestCase, Config) ->
  165. %% get slave
  166. SlaveNode = proplists:get_value(slave_node, Config),
  167. syn_test_suite_helper:clean_after_test(SlaveNode).
  168. %% ===================================================================
  169. %% Tests
  170. %% ===================================================================
  171. two_nodes_netsplit_when_there_are_no_conflicts(Config) ->
  172. %% get slave
  173. SlaveNode = proplists:get_value(slave_node, Config),
  174. CurrentNode = node(),
  175. %% start syn
  176. ok = syn:start(),
  177. ok = syn:init(),
  178. ok = rpc:call(SlaveNode, syn, start, []),
  179. ok = rpc:call(SlaveNode, syn, init, []),
  180. timer:sleep(100),
  181. %% start processes
  182. LocalPid = syn_test_suite_helper:start_process(),
  183. SlavePidLocal = syn_test_suite_helper:start_process(SlaveNode),
  184. SlavePidSlave = syn_test_suite_helper:start_process(SlaveNode),
  185. %% register
  186. ok = syn:register(local_pid, LocalPid),
  187. ok = syn:register(slave_pid_local, SlavePidLocal), %% slave registered on local node
  188. ok = rpc:call(SlaveNode, syn, register, [slave_pid_slave, SlavePidSlave]), %% slave registered on slave node
  189. timer:sleep(100),
  190. %% check tables
  191. 3 = mnesia:table_info(syn_global_table, size),
  192. 3 = rpc:call(SlaveNode, mnesia, table_info, [syn_global_table, size]),
  193. LocalActiveReplicas = mnesia:table_info(syn_global_table, active_replicas),
  194. 2 = length(LocalActiveReplicas),
  195. true = lists:member(SlaveNode, LocalActiveReplicas),
  196. true = lists:member(CurrentNode, LocalActiveReplicas),
  197. SlaveActiveReplicas = rpc:call(SlaveNode, mnesia, table_info, [syn_global_table, active_replicas]),
  198. 2 = length(SlaveActiveReplicas),
  199. true = lists:member(SlaveNode, SlaveActiveReplicas),
  200. true = lists:member(CurrentNode, SlaveActiveReplicas),
  201. %% simulate net split
  202. syn_test_suite_helper:disconnect_node(SlaveNode),
  203. timer:sleep(1000),
  204. %% check tables
  205. 1 = mnesia:table_info(syn_global_table, size),
  206. [CurrentNode] = mnesia:table_info(syn_global_table, active_replicas),
  207. %% reconnect
  208. syn_test_suite_helper:connect_node(SlaveNode),
  209. timer:sleep(1000),
  210. %% check tables
  211. 3 = mnesia:table_info(syn_global_table, size),
  212. 3 = rpc:call(SlaveNode, mnesia, table_info, [syn_global_table, size]),
  213. LocalActiveReplicas2 = mnesia:table_info(syn_global_table, active_replicas),
  214. 2 = length(LocalActiveReplicas2),
  215. true = lists:member(SlaveNode, LocalActiveReplicas2),
  216. true = lists:member(CurrentNode, LocalActiveReplicas2),
  217. SlaveActiveReplicas2 = rpc:call(SlaveNode, mnesia, table_info, [syn_global_table, active_replicas]),
  218. 2 = length(SlaveActiveReplicas2),
  219. true = lists:member(SlaveNode, SlaveActiveReplicas2),
  220. true = lists:member(CurrentNode, SlaveActiveReplicas2),
  221. %% check processes
  222. LocalPid = syn:find_by_key(local_pid),
  223. SlavePidLocal = syn:find_by_key(slave_pid_local),
  224. SlavePidSlave = syn:find_by_key(slave_pid_slave),
  225. LocalPid = rpc:call(SlaveNode, syn, find_by_key, [local_pid]),
  226. SlavePidLocal = rpc:call(SlaveNode, syn, find_by_key, [slave_pid_local]),
  227. SlavePidSlave = rpc:call(SlaveNode, syn, find_by_key, [slave_pid_slave]),
  228. %% kill processes
  229. syn_test_suite_helper:kill_process(LocalPid),
  230. syn_test_suite_helper:kill_process(SlavePidLocal),
  231. syn_test_suite_helper:kill_process(SlavePidSlave).
  232. two_nodes_netsplit_kill_resolution_when_there_are_conflicts(Config) ->
  233. %% get slave
  234. SlaveNode = proplists:get_value(slave_node, Config),
  235. CurrentNode = node(),
  236. %% start syn
  237. ok = syn:start(),
  238. ok = syn:init(),
  239. ok = rpc:call(SlaveNode, syn, start, []),
  240. ok = rpc:call(SlaveNode, syn, init, []),
  241. timer:sleep(100),
  242. %% start processes
  243. LocalPid = syn_test_suite_helper:start_process(),
  244. SlavePid = syn_test_suite_helper:start_process(SlaveNode),
  245. %% register
  246. ok = syn:register(conflicting_key, SlavePid),
  247. timer:sleep(100),
  248. %% check tables
  249. 1 = mnesia:table_info(syn_global_table, size),
  250. 1 = rpc:call(SlaveNode, mnesia, table_info, [syn_global_table, size]),
  251. %% check process
  252. SlavePid = syn:find_by_key(conflicting_key),
  253. %% simulate net split
  254. syn_test_suite_helper:disconnect_node(SlaveNode),
  255. timer:sleep(1000),
  256. %% check tables
  257. 0 = mnesia:table_info(syn_global_table, size),
  258. [CurrentNode] = mnesia:table_info(syn_global_table, active_replicas),
  259. %% now register the local pid with the same key
  260. ok = syn:register(conflicting_key, LocalPid),
  261. %% check process
  262. LocalPid = syn:find_by_key(conflicting_key),
  263. %% reconnect
  264. syn_test_suite_helper:connect_node(SlaveNode),
  265. timer:sleep(1000),
  266. %% check tables
  267. 1 = mnesia:table_info(syn_global_table, size),
  268. 1 = rpc:call(SlaveNode, mnesia, table_info, [syn_global_table, size]),
  269. %% check process
  270. FoundPid = syn:find_by_key(conflicting_key),
  271. true = lists:member(FoundPid, [LocalPid, SlavePid]),
  272. %% kill processes
  273. syn_test_suite_helper:kill_process(LocalPid),
  274. syn_test_suite_helper:kill_process(SlavePid),
  275. %% unregister
  276. global:unregister_name(syn_consistency_SUITE_result).
  277. two_nodes_netsplit_callback_resolution_when_there_are_conflicts(Config) ->
  278. %% get slave
  279. SlaveNode = proplists:get_value(slave_node, Config),
  280. CurrentNode = node(),
  281. %% load configuration variables from syn-test.config => this sets the conflicting_process_callback option
  282. syn_test_suite_helper:set_environment_variables(),
  283. syn_test_suite_helper:set_environment_variables(SlaveNode),
  284. %% start syn
  285. ok = syn:start(),
  286. ok = syn:init(),
  287. ok = rpc:call(SlaveNode, syn, start, []),
  288. ok = rpc:call(SlaveNode, syn, init, []),
  289. timer:sleep(100),
  290. %% start processes
  291. LocalPid = syn_test_suite_helper:start_process(fun process_reply_main/0),
  292. SlavePid = syn_test_suite_helper:start_process(SlaveNode, fun process_reply_main/0),
  293. %% register global process
  294. ResultPid = self(),
  295. global:register_name(syn_consistency_SUITE_result, ResultPid),
  296. %% register
  297. Meta = {some, meta, data},
  298. ok = syn:register(conflicting_key, SlavePid, Meta),
  299. timer:sleep(100),
  300. %% check tables
  301. 1 = mnesia:table_info(syn_global_table, size),
  302. 1 = rpc:call(SlaveNode, mnesia, table_info, [syn_global_table, size]),
  303. %% check process
  304. SlavePid = syn:find_by_key(conflicting_key),
  305. %% simulate net split
  306. syn_test_suite_helper:disconnect_node(SlaveNode),
  307. timer:sleep(1000),
  308. %% check tables
  309. 0 = mnesia:table_info(syn_global_table, size),
  310. [CurrentNode] = mnesia:table_info(syn_global_table, active_replicas),
  311. %% now register the local pid with the same key
  312. ok = syn:register(conflicting_key, LocalPid, Meta),
  313. %% check process
  314. LocalPid = syn:find_by_key(conflicting_key),
  315. %% reconnect
  316. syn_test_suite_helper:connect_node(SlaveNode),
  317. timer:sleep(1000),
  318. %% check tables
  319. 1 = mnesia:table_info(syn_global_table, size),
  320. 1 = rpc:call(SlaveNode, mnesia, table_info, [syn_global_table, size]),
  321. %% check process
  322. FoundPid = syn:find_by_key(conflicting_key),
  323. true = lists:member(FoundPid, [LocalPid, SlavePid]),
  324. %% check message received from killed pid
  325. KilledPid = lists:nth(1, lists:delete(FoundPid, [LocalPid, SlavePid])),
  326. receive
  327. {exited, KilledPid, Meta} -> ok
  328. after 2000 ->
  329. ok = conflicting_process_did_not_receive_message
  330. end,
  331. %% kill processes
  332. syn_test_suite_helper:kill_process(LocalPid),
  333. syn_test_suite_helper:kill_process(SlavePid),
  334. %% unregister
  335. global:unregister_name(syn_consistency_SUITE_result).
  336. three_nodes_netsplit_kill_resolution_when_there_are_conflicts(Config) ->
  337. %% get slaves
  338. SlaveNode = proplists:get_value(slave_node, Config),
  339. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  340. CurrentNode = node(),
  341. %% start syn
  342. ok = syn:start(),
  343. ok = syn:init(),
  344. ok = rpc:call(SlaveNode, syn, start, []),
  345. ok = rpc:call(SlaveNode, syn, init, []),
  346. ok = rpc:call(SlaveNode2, syn, start, []),
  347. ok = rpc:call(SlaveNode2, syn, init, []),
  348. timer:sleep(100),
  349. %% start processes
  350. LocalPid = syn_test_suite_helper:start_process(),
  351. SlavePid = syn_test_suite_helper:start_process(SlaveNode),
  352. Slave2Pid = syn_test_suite_helper:start_process(SlaveNode2),
  353. %% register
  354. ok = syn:register(conflicting_key, SlavePid),
  355. ok = syn:register(slave_2_process, Slave2Pid),
  356. timer:sleep(100),
  357. %% check tables
  358. 2 = mnesia:table_info(syn_global_table, size),
  359. 2 = rpc:call(SlaveNode, mnesia, table_info, [syn_global_table, size]),
  360. 2 = rpc:call(SlaveNode2, mnesia, table_info, [syn_global_table, size]),
  361. %% check process
  362. SlavePid = syn:find_by_key(conflicting_key),
  363. %% simulate net split
  364. syn_test_suite_helper:disconnect_node(SlaveNode),
  365. timer:sleep(1000),
  366. %% check tables
  367. 1 = mnesia:table_info(syn_global_table, size),
  368. 1 = rpc:call(SlaveNode2, mnesia, table_info, [syn_global_table, size]),
  369. ActiveReplicaseDuringNetsplit = mnesia:table_info(syn_global_table, active_replicas),
  370. true = lists:member(CurrentNode, ActiveReplicaseDuringNetsplit),
  371. true = lists:member(SlaveNode2, ActiveReplicaseDuringNetsplit),
  372. %% now register the local pid with the same conflicting key
  373. ok = syn:register(conflicting_key, LocalPid),
  374. %% check process
  375. LocalPid = syn:find_by_key(conflicting_key),
  376. %% reconnect
  377. syn_test_suite_helper:connect_node(SlaveNode),
  378. timer:sleep(1000),
  379. %% check tables
  380. 2 = mnesia:table_info(syn_global_table, size),
  381. 2 = rpc:call(SlaveNode, mnesia, table_info, [syn_global_table, size]),
  382. 2 = rpc:call(SlaveNode2, mnesia, table_info, [syn_global_table, size]),
  383. %% check processes
  384. FoundPid = syn:find_by_key(conflicting_key),
  385. true = lists:member(FoundPid, [LocalPid, SlavePid]),
  386. Slave2Pid = syn:find_by_key(slave_2_process),
  387. Slave2Pid = rpc:call(SlaveNode, syn, find_by_key, [slave_2_process]),
  388. Slave2Pid = rpc:call(SlaveNode2, syn, find_by_key, [slave_2_process]),
  389. %% kill processes
  390. syn_test_suite_helper:kill_process(LocalPid),
  391. syn_test_suite_helper:kill_process(SlavePid),
  392. syn_test_suite_helper:kill_process(Slave2Pid).
  393. %% ===================================================================
  394. %% Internal
  395. %% ===================================================================
  396. process_reply_main() ->
  397. receive
  398. {shutdown, Meta} ->
  399. timer:sleep(500), %% wait for global processes to propagate
  400. global:send(syn_consistency_SUITE_result, {exited, self(), Meta})
  401. end.
  402. conflicting_process_callback_dummy(_Key, Pid, Meta) ->
  403. Pid ! {shutdown, Meta}.