syn_test_suite_helper.erl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2021 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. %% ==========================================================================================================
  26. -module(syn_test_suite_helper).
  27. %% API
  28. -export([start_slave/1, stop_slave/1]).
  29. -export([connect_node/1, disconnect_node/1]).
  30. -export([use_custom_handler/0]).
  31. -export([clean_after_test/0]).
  32. -export([start_process/0, start_process/1, start_process/2]).
  33. -export([kill_process/1]).
  34. -export([flush_inbox/0]).
  35. -export([wait_cluster_mesh_connected/1]).
  36. -export([wait_process_name_ready/1, wait_process_name_ready/2]).
  37. -export([assert_cluster/2]).
  38. -export([assert_scope_subcluster/3]).
  39. -export([assert_received_messages/1]).
  40. -export([assert_empty_queue/1]).
  41. -export([assert_wait/2]).
  42. -export([send_error_logger_to_disk/0]).
  43. %% internal
  44. -export([process_main/0]).
  45. %% ===================================================================
  46. %% API
  47. %% ===================================================================
  48. start_slave(NodeShortName) ->
  49. {ok, Node} = ct_slave:start(NodeShortName, [
  50. {boot_timeout, 10},
  51. {erl_flags, "-connect_all false"}
  52. ]),
  53. CodePath = code:get_path(),
  54. true = rpc:call(Node, code, set_path, [CodePath]),
  55. {ok, Node}.
  56. stop_slave(NodeShortName) ->
  57. {ok, _} = ct_slave:stop(NodeShortName).
  58. connect_node(Node) ->
  59. net_kernel:connect_node(Node).
  60. disconnect_node(Node) ->
  61. erlang:disconnect_node(Node).
  62. use_custom_handler() ->
  63. application:set_env(syn, event_handler, syn_test_event_handler).
  64. clean_after_test() ->
  65. Nodes = [node() | nodes()],
  66. %% shutdown
  67. lists:foreach(fun(Node) ->
  68. %% close syn
  69. rpc:call(Node, application, stop, [syn]),
  70. %% clean env
  71. rpc:call(Node, application, unset_env, [syn, event_handler])
  72. end, Nodes).
  73. start_process() ->
  74. Pid = spawn(fun process_main/0),
  75. Pid.
  76. start_process(Node) when is_atom(Node) ->
  77. Pid = spawn(Node, fun process_main/0),
  78. Pid;
  79. start_process(Loop) when is_function(Loop) ->
  80. Pid = spawn(Loop),
  81. Pid.
  82. start_process(Node, Loop) ->
  83. Pid = spawn(Node, Loop),
  84. Pid.
  85. kill_process(RegisteredName) when is_atom(RegisteredName) ->
  86. case whereis(RegisteredName) of
  87. undefined -> ok;
  88. Pid -> kill_process(Pid)
  89. end;
  90. kill_process(Pid) when is_pid(Pid) ->
  91. case rpc:call(node(Pid), erlang, is_process_alive, [Pid]) of
  92. true ->
  93. MRef = monitor(process, Pid),
  94. exit(Pid, kill),
  95. receive
  96. {'DOWN', MRef, process, Pid, _Reason} -> ok
  97. after 5000 ->
  98. ct:fail("~n\tCould not kill process ~p~n", [Pid])
  99. end;
  100. false ->
  101. ok
  102. end.
  103. flush_inbox() ->
  104. receive
  105. _ -> flush_inbox()
  106. after 0 ->
  107. ok
  108. end.
  109. wait_cluster_mesh_connected(Nodes) ->
  110. wait_cluster_mesh_connected(Nodes, os:system_time(millisecond)).
  111. wait_cluster_mesh_connected(Nodes, StartAt) ->
  112. AllSynced = lists:all(fun(Node) ->
  113. RemoteNodes = rpc:call(Node, erlang, nodes, []),
  114. AllNodes = [Node | RemoteNodes],
  115. lists:sort(AllNodes) == lists:sort(Nodes)
  116. end, Nodes),
  117. case AllSynced of
  118. true ->
  119. ok;
  120. false ->
  121. case os:system_time(millisecond) - StartAt > 5000 of
  122. true ->
  123. {error, {could_not_init_cluster, Nodes}};
  124. false ->
  125. timer:sleep(50),
  126. wait_cluster_mesh_connected(Nodes, StartAt)
  127. end
  128. end.
  129. wait_process_name_ready(Name) ->
  130. wait_process_name_ready(Name, os:system_time(millisecond)).
  131. wait_process_name_ready(Name, StartAt) ->
  132. timer:sleep(50),
  133. case whereis(Name) of
  134. undefined ->
  135. case os:system_time(millisecond) - StartAt > 5000 of
  136. true ->
  137. ct:fail("~n\tProcess with name ~p didn't come alive~n", [Name]);
  138. false ->
  139. wait_process_name_ready(Name, StartAt)
  140. end;
  141. Pid ->
  142. case process_info(Pid, status) of
  143. {status, waiting} ->
  144. ok;
  145. Other ->
  146. case os:system_time(millisecond) - StartAt > 5000 of
  147. true ->
  148. ct:fail("~n\tProcess with name ~p didn't come ready~n\tStatus: ~p~n", [Name, Other]);
  149. false ->
  150. wait_process_name_ready(Name, StartAt)
  151. end
  152. end
  153. end.
  154. assert_cluster(Node, ExpectedNodes) ->
  155. assert_cluster(Node, ExpectedNodes, os:system_time(millisecond)).
  156. assert_cluster(Node, ExpectedNodes, StartAt) ->
  157. Nodes = rpc:call(Node, erlang, nodes, []),
  158. case do_assert_cluster(Nodes, ExpectedNodes, StartAt) of
  159. continue -> assert_cluster(Node, ExpectedNodes, StartAt);
  160. _ -> ok
  161. end.
  162. assert_scope_subcluster(Node, Scope, ExpectedNodes) ->
  163. assert_scope_subcluster(Node, Scope, ExpectedNodes, os:system_time(millisecond)).
  164. assert_scope_subcluster(Node, Scope, ExpectedNodes, StartAt) ->
  165. NodesMap = rpc:call(Node, syn_registry, get_subcluster_nodes, [Scope]),
  166. Nodes = maps:keys(NodesMap),
  167. case do_assert_cluster(Nodes, ExpectedNodes, StartAt) of
  168. continue -> assert_scope_subcluster(Node, Scope, ExpectedNodes, StartAt);
  169. _ -> ok
  170. end.
  171. assert_received_messages(Messages) ->
  172. assert_received_messages(Messages, []).
  173. assert_received_messages([], UnexpectedMessages) ->
  174. do_assert_received_messages([], UnexpectedMessages);
  175. assert_received_messages(Messages, UnexpectedMessages) ->
  176. receive
  177. Message ->
  178. case lists:member(Message, Messages) of
  179. true ->
  180. Messages1 = lists:delete(Message, Messages),
  181. assert_received_messages(Messages1, UnexpectedMessages);
  182. false ->
  183. assert_received_messages(Messages, [Message | UnexpectedMessages])
  184. end
  185. after 5000 ->
  186. do_assert_received_messages(Messages, UnexpectedMessages)
  187. end.
  188. assert_empty_queue(Pid) when is_pid(Pid) ->
  189. case process_info(Pid, message_queue_len) of
  190. {message_queue_len, 0} ->
  191. ok;
  192. _ ->
  193. {messages, Messages} = process_info(Pid, messages),
  194. ct:fail("~n\tMessage queue was not empty, got:~n\t~p~n", [Messages])
  195. end.
  196. assert_wait(ExpectedResult, Fun) ->
  197. assert_wait(ExpectedResult, Fun, os:system_time(millisecond)).
  198. assert_wait(ExpectedResult, Fun, StartAt) ->
  199. case Fun() of
  200. ExpectedResult ->
  201. ok;
  202. Result ->
  203. case os:system_time(millisecond) - StartAt > 5000 of
  204. true ->
  205. ct:fail("~n\tExpected: ~p~n\tActual: ~p~n", [ExpectedResult, Result]);
  206. false ->
  207. timer:sleep(50),
  208. assert_wait(ExpectedResult, Fun, StartAt)
  209. end
  210. end.
  211. send_error_logger_to_disk() ->
  212. error_logger:logfile({open, atom_to_list(node())}).
  213. %% ===================================================================
  214. %% Internal
  215. %% ===================================================================
  216. process_main() ->
  217. receive
  218. _ -> process_main()
  219. end.
  220. do_assert_cluster(Nodes, ExpectedNodes, StartAt) ->
  221. ExpectedCount = length(ExpectedNodes),
  222. %% count nodes
  223. case length(Nodes) of
  224. ExpectedCount ->
  225. %% loop nodes
  226. RemainingNodes = lists:filter(fun(N) -> not lists:member(N, ExpectedNodes) end, Nodes),
  227. case length(RemainingNodes) of
  228. 0 ->
  229. ok;
  230. _ ->
  231. case os:system_time(millisecond) - StartAt > 5000 of
  232. true ->
  233. ct:fail("~n\tInvalid subcluster~n\tExpected: ~p~n\tActual: ~p~n\tLine: ~p~n",
  234. [ExpectedNodes, Nodes, get_line_from_stacktrace()]
  235. );
  236. false ->
  237. timer:sleep(50),
  238. continue
  239. end
  240. end;
  241. _ ->
  242. case os:system_time(millisecond) - StartAt > 5000 of
  243. true ->
  244. ct:fail("~n\tInvalid subcluster~n\tExpected: ~p~n\tActual: ~p~n\tLine: ~p~n",
  245. [ExpectedNodes, Nodes, get_line_from_stacktrace()]
  246. );
  247. false ->
  248. timer:sleep(50),
  249. continue
  250. end
  251. end.
  252. do_assert_received_messages([], []) ->
  253. ok;
  254. do_assert_received_messages(MissingMessages, UnexpectedMessages) ->
  255. ct:fail("~n\tReceive messages error~n\tMissing: ~p~n\tUnexpected: ~p~n",
  256. [lists:reverse(MissingMessages), lists:reverse(UnexpectedMessages)]
  257. ).
  258. get_line_from_stacktrace() ->
  259. {current_stacktrace, Stacktrace} = process_info(self(), current_stacktrace),
  260. [{_, _, _, FileInfo} | _] = lists:dropwhile(fun({Module, _Method, _Arity, _FileInfo}) ->
  261. Module =:= ?MODULE end, Stacktrace),
  262. proplists:get_value(line, FileInfo).