syn_test_suite_helper.erl 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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([send_error_logger_to_disk/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, [{boot_timeout, 10}]),
  43. true = rpc:call(Node, code, set_path, [CodePath]),
  44. {ok, Node}.
  45. stop_slave(NodeShortName) ->
  46. {ok, _} = ct_slave:stop(NodeShortName).
  47. connect_node(Node) ->
  48. net_kernel:connect_node(Node).
  49. disconnect_node(Node) ->
  50. erlang:disconnect_node(Node).
  51. clean_after_test() ->
  52. Nodes = [node() | nodes()],
  53. %% shutdown
  54. lists:foreach(fun(Node) ->
  55. rpc:call(Node, syn, stop, []),
  56. rpc:call(Node, application, stop, [mnesia]),
  57. %% clean env
  58. rpc:call(Node, application, unset_env, [syn, event_handler])
  59. end, Nodes),
  60. %% clean mnesia
  61. mnesia:delete_schema(Nodes).
  62. start_process() ->
  63. Pid = spawn(fun process_main/0),
  64. Pid.
  65. start_process(Node) when is_atom(Node) ->
  66. Pid = spawn(Node, fun process_main/0),
  67. Pid;
  68. start_process(Loop) when is_function(Loop) ->
  69. Pid = spawn(Loop),
  70. Pid.
  71. start_process(Node, Loop) ->
  72. Pid = spawn(Node, Loop),
  73. Pid.
  74. kill_process(Pid) ->
  75. exit(Pid, kill).
  76. use_custom_handler() ->
  77. application:set_env(syn, event_handler, syn_test_event_handler).
  78. send_error_logger_to_disk() ->
  79. error_logger:logfile({open, atom_to_list(node())}).
  80. %% ===================================================================
  81. %% Internal
  82. %% ===================================================================
  83. process_main() ->
  84. receive
  85. _ -> process_main()
  86. end.