syn_test_suite_helper.erl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, start_slave/4]).
  29. -export([stop_slave/1, stop_slave/2]).
  30. -export([connect_node/1, disconnect_node/1]).
  31. -export([use_custom_handler/0]).
  32. -export([clean_after_test/0]).
  33. -export([start_process/0, start_process/1, start_process/2]).
  34. -export([kill_process/1]).
  35. -export([flush_inbox/0]).
  36. -export([wait_cluster_mesh_connected/1]).
  37. -export([assert_scope_subcluster/3]).
  38. -export([send_error_logger_to_disk/0]).
  39. %% internal
  40. -export([process_main/0]).
  41. %% ===================================================================
  42. %% API
  43. %% ===================================================================
  44. start_slave(NodeShortName) ->
  45. {ok, Node} = ct_slave:start(NodeShortName, [{boot_timeout, 10}]),
  46. CodePath = code:get_path(),
  47. true = rpc:call(Node, code, set_path, [CodePath]),
  48. {ok, Node}.
  49. start_slave(NodeShortName, Host, Username, Password) ->
  50. {ok, Node} = ct_slave:start(Host, NodeShortName, [
  51. {boot_timeout, 10},
  52. {username, Username},
  53. {password, Password}
  54. ]),
  55. CodePath = code:get_path(),
  56. true = rpc:call(Node, code, set_path, [CodePath]),
  57. {ok, Node}.
  58. stop_slave(NodeShortName) ->
  59. {ok, _} = ct_slave:stop(NodeShortName).
  60. stop_slave(Host, NodeShortName) ->
  61. {ok, _} = ct_slave:stop(Host, NodeShortName).
  62. connect_node(Node) ->
  63. net_kernel:connect_node(Node).
  64. disconnect_node(Node) ->
  65. erlang:disconnect_node(Node).
  66. use_custom_handler() ->
  67. application:set_env(syn, event_handler, syn_test_event_handler).
  68. clean_after_test() ->
  69. Nodes = [node() | nodes()],
  70. %% shutdown
  71. lists:foreach(fun(Node) ->
  72. %% close syn
  73. rpc:call(Node, application, stop, [syn]),
  74. %% clean env
  75. rpc:call(Node, application, unset_env, [syn, event_handler])
  76. end, Nodes).
  77. start_process() ->
  78. Pid = spawn(fun process_main/0),
  79. Pid.
  80. start_process(Node) when is_atom(Node) ->
  81. Pid = spawn(Node, fun process_main/0),
  82. Pid;
  83. start_process(Loop) when is_function(Loop) ->
  84. Pid = spawn(Loop),
  85. Pid.
  86. start_process(Node, Loop) ->
  87. Pid = spawn(Node, Loop),
  88. Pid.
  89. kill_process(Pid) when is_pid(Pid) ->
  90. exit(Pid, kill);
  91. kill_process(RegisteredName) when is_atom(RegisteredName) ->
  92. exit(whereis(RegisteredName), kill).
  93. flush_inbox() ->
  94. receive
  95. _ -> flush_inbox()
  96. after
  97. 0 -> ok
  98. end.
  99. wait_cluster_mesh_connected(Nodes) ->
  100. wait_cluster_mesh_connected(Nodes, os:system_time(millisecond)).
  101. wait_cluster_mesh_connected(Nodes, StartAt) ->
  102. AllSynced = lists:all(fun(Node) ->
  103. RemoteNodes = rpc:call(Node, erlang, nodes, []),
  104. AllNodes = [Node | RemoteNodes],
  105. lists:sort(AllNodes) == lists:sort(Nodes)
  106. end, Nodes),
  107. case AllSynced of
  108. true ->
  109. ok;
  110. false ->
  111. case os:system_time(millisecond) - StartAt > 5000 of
  112. true ->
  113. {error, {could_not_init_cluster, Nodes}};
  114. false ->
  115. timer:sleep(100),
  116. wait_cluster_mesh_connected(Nodes, StartAt)
  117. end
  118. end.
  119. assert_scope_subcluster(Node, Scope, ExpectedNodes) ->
  120. NodesMap = rpc:call(Node, syn_registry, get_subcluster_nodes, [Scope]),
  121. Nodes = maps:keys(NodesMap),
  122. ExpectedCount = length(ExpectedNodes),
  123. %% count nodes
  124. case length(Nodes) of
  125. ExpectedCount ->
  126. ok;
  127. _ ->
  128. ct:fail("~n\tInvalid subcluster~n\tExpected: ~p~n\tActual: ~p~n\tLine: ~p~n",
  129. [ExpectedNodes, Nodes, get_line_from_stacktrace(2)]
  130. )
  131. end,
  132. %% loop nodes
  133. lists:foreach(fun(RemoteNode) ->
  134. case lists:member(RemoteNode, Nodes) of
  135. true ->
  136. ok;
  137. _ ->
  138. ct:fail("~n\tInvalid subcluster~n\tExpected: ~p~n\tActual: ~p~n\tLine: ~p~n",
  139. [ExpectedNodes, Nodes, get_line_from_stacktrace(3)]
  140. )
  141. end
  142. end, ExpectedNodes).
  143. send_error_logger_to_disk() ->
  144. error_logger:logfile({open, atom_to_list(node())}).
  145. %% ===================================================================
  146. %% Internal
  147. %% ===================================================================
  148. process_main() ->
  149. receive
  150. _ -> process_main()
  151. end.
  152. get_line_from_stacktrace(Position) ->
  153. {current_stacktrace, Stacktrace} = process_info(self(), current_stacktrace),
  154. {_, _, _, FileInfo} = lists:nth(Position, Stacktrace),
  155. proplists:get_value(line, FileInfo).