syn_test_suite_helper.erl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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([clean_after_test/0]).
  32. -export([start_process/0, start_process/1, start_process/2]).
  33. -export([kill_process/1]).
  34. -export([wait_cluster_connected/1]).
  35. %% internal
  36. -export([process_main/0]).
  37. %% ===================================================================
  38. %% API
  39. %% ===================================================================
  40. start_slave(NodeShortName) ->
  41. {ok, Node} = ct_slave:start(NodeShortName, [{boot_timeout, 10}]),
  42. CodePath = code:get_path(),
  43. true = rpc:call(Node, code, set_path, [CodePath]),
  44. {ok, Node}.
  45. start_slave(NodeShortName, Host, Username, Password) ->
  46. {ok, Node} = ct_slave:start(Host, NodeShortName, [
  47. {boot_timeout, 10},
  48. {username, Username},
  49. {password, Password}
  50. ]),
  51. CodePath = code:get_path(),
  52. true = rpc:call(Node, code, set_path, [CodePath]),
  53. {ok, Node}.
  54. stop_slave(NodeShortName) ->
  55. {ok, _} = ct_slave:stop(NodeShortName).
  56. stop_slave(Host, NodeShortName) ->
  57. {ok, _} = ct_slave:stop(Host, NodeShortName).
  58. connect_node(Node) ->
  59. net_kernel:connect_node(Node).
  60. disconnect_node(Node) ->
  61. erlang:disconnect_node(Node).
  62. clean_after_test() ->
  63. Nodes = [node() | nodes()],
  64. %% shutdown
  65. lists:foreach(fun(Node) ->
  66. %% close syn
  67. rpc:call(Node, application, stop, [syn])
  68. end, Nodes).
  69. start_process() ->
  70. Pid = spawn(fun process_main/0),
  71. Pid.
  72. start_process(Node) when is_atom(Node) ->
  73. Pid = spawn(Node, fun process_main/0),
  74. Pid;
  75. start_process(Loop) when is_function(Loop) ->
  76. Pid = spawn(Loop),
  77. Pid.
  78. start_process(Node, Loop) ->
  79. Pid = spawn(Node, Loop),
  80. Pid.
  81. kill_process(Pid) when is_pid(Pid) ->
  82. exit(Pid, kill);
  83. kill_process(RegisteredName) when is_atom(RegisteredName) ->
  84. exit(whereis(RegisteredName), kill).
  85. wait_cluster_connected(Nodes) ->
  86. wait_cluster_connected(Nodes, os:system_time(millisecond)).
  87. wait_cluster_connected(Nodes, StartAt) ->
  88. AllSynced = lists:all(fun(Node) ->
  89. RemoteNodes = rpc:call(Node, erlang, nodes, []),
  90. AllNodes = [Node | RemoteNodes],
  91. lists:sort(AllNodes) == lists:sort(Nodes)
  92. end, Nodes),
  93. case AllSynced of
  94. true ->
  95. ok;
  96. false ->
  97. case os:system_time(millisecond) - StartAt > 5000 of
  98. true ->
  99. {error, {could_not_init_cluster, Nodes}};
  100. false ->
  101. timer:sleep(1000),
  102. wait_cluster_connected(Nodes, StartAt)
  103. end
  104. end.
  105. %% ===================================================================
  106. %% Internal
  107. %% ===================================================================
  108. process_main() ->
  109. receive
  110. _ -> process_main()
  111. end.