syn_test_suite_helper.erl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015 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([clean_after_test/0]).
  31. -export([start_process/0, start_process/1, start_process/2]).
  32. -export([kill_process/1]).
  33. -export([use_custom_handler/0]).
  34. -export([start_collecting_debug_data/0, send_debug_data/1, print_debug_data/0]).
  35. %% internal
  36. -export([process_main/0]).
  37. %% ===================================================================
  38. %% API
  39. %% ===================================================================
  40. start_slave(NodeShortName) ->
  41. CodePath = code:get_path(),
  42. {ok, Node} = ct_slave:start(NodeShortName, [
  43. {boot_timeout, 10}
  44. %% {erl_flags, ErlangFlags}
  45. ]),
  46. true = rpc:call(Node, code, set_path, [CodePath]),
  47. {ok, Node}.
  48. stop_slave(NodeShortName) ->
  49. {ok, _} = ct_slave:stop(NodeShortName).
  50. connect_node(Node) ->
  51. net_kernel:connect_node(Node).
  52. disconnect_node(Node) ->
  53. erlang:disconnect_node(Node).
  54. clean_after_test() ->
  55. Nodes = [node() | nodes()],
  56. %% shutdown
  57. lists:foreach(fun(Node) ->
  58. ok = rpc:call(Node, syn, stop, []),
  59. ok = rpc:call(Node, application, stop, [mnesia])
  60. end, Nodes),
  61. %% clean mnesia
  62. mnesia:delete_schema(Nodes).
  63. start_process() ->
  64. Pid = spawn(fun process_main/0),
  65. Pid.
  66. start_process(Node) when is_atom(Node) ->
  67. Pid = spawn(Node, fun process_main/0),
  68. Pid;
  69. start_process(Loop) when is_function(Loop) ->
  70. Pid = spawn(Loop),
  71. Pid.
  72. start_process(Node, Loop) ->
  73. Pid = spawn(Node, Loop),
  74. Pid.
  75. kill_process(Pid) ->
  76. exit(Pid, kill).
  77. use_custom_handler() ->
  78. application:set_env(syn, event_handler, syn_test_event_handler).
  79. start_collecting_debug_data() ->
  80. global:register_name(syn_debug_process, self()).
  81. send_debug_data(Message) ->
  82. global:send(syn_debug_process, Message).
  83. print_debug_data() ->
  84. receive
  85. Any ->
  86. ct:pal("~p", [Any]),
  87. print_debug_data()
  88. after 1000 ->
  89. ok
  90. end.
  91. %% ===================================================================
  92. %% Internal
  93. %% ===================================================================
  94. process_main() ->
  95. receive
  96. _ -> process_main()
  97. end.