syn_test_suite_helper.erl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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([set_environment_variables/0, set_environment_variables/1]).
  29. -export([start_slave/1, stop_slave/1]).
  30. -export([connect_node/1, disconnect_node/1]).
  31. -export([clean_after_test/0, clean_after_test/1]).
  32. -export([start_process/0, start_process/1, start_process/2]).
  33. -export([kill_process/1]).
  34. %% internal
  35. -export([process_main/0]).
  36. %% macros
  37. -define(SYN_TEST_CONFIG_FILENAME, "syn-test.config").
  38. %% ===================================================================
  39. %% API
  40. %% ===================================================================
  41. set_environment_variables() ->
  42. set_environment_variables(node()).
  43. set_environment_variables(Node) ->
  44. % read config file
  45. ConfigFilePath = filename:join([filename:dirname(code:which(?MODULE)), ?SYN_TEST_CONFIG_FILENAME]),
  46. {ok, [AppsConfig]} = file:consult(ConfigFilePath),
  47. % loop to set variables
  48. F = fun({AppName, AppConfig}) ->
  49. set_environment_for_app(Node, AppName, AppConfig)
  50. end,
  51. lists:foreach(F, AppsConfig).
  52. start_slave(NodeShortName) ->
  53. EbinFilePath = filename:join([filename:dirname(code:lib_dir(syn, ebin)), "ebin"]),
  54. TestFilePath = filename:join([filename:dirname(code:lib_dir(syn, ebin)), "test"]),
  55. %% start slave
  56. {ok, Node} = ct_slave:start(NodeShortName, [
  57. {boot_timeout, 10},
  58. {erl_flags, lists:concat(["-pa ", EbinFilePath, " ", TestFilePath])}
  59. ]),
  60. {ok, Node}.
  61. stop_slave(NodeShortName) ->
  62. {ok, _} = ct_slave:stop(NodeShortName).
  63. connect_node(Node) ->
  64. net_kernel:connect_node(Node).
  65. disconnect_node(Node) ->
  66. erlang:disconnect_node(Node).
  67. clean_after_test() ->
  68. %% delete table
  69. {atomic, ok} = mnesia:delete_table(syn_registry_table),
  70. %% stop mnesia
  71. mnesia:stop(),
  72. %% delete schema
  73. mnesia:delete_schema([node()]),
  74. %% stop syn
  75. syn:stop().
  76. clean_after_test(undefined) ->
  77. clean_after_test();
  78. clean_after_test(Node) ->
  79. %% delete table
  80. {atomic, ok} = mnesia:delete_table(syn_registry_table),
  81. %% stop mnesia
  82. mnesia:stop(),
  83. rpc:call(Node, mnesia, stop, []),
  84. %% delete schema
  85. mnesia:delete_schema([node(), Node]),
  86. %% stop syn
  87. syn:stop(),
  88. rpc:call(Node, syn, stop, []).
  89. start_process() ->
  90. Pid = spawn(fun process_main/0),
  91. Pid.
  92. start_process(Node) when is_atom(Node) ->
  93. Pid = spawn(Node, fun process_main/0),
  94. Pid;
  95. start_process(Loop) when is_function(Loop) ->
  96. Pid = spawn(Loop),
  97. Pid.
  98. start_process(Node, Loop)->
  99. Pid = spawn(Node, Loop),
  100. Pid.
  101. kill_process(Pid) ->
  102. exit(Pid, kill).
  103. %% ===================================================================
  104. %% Internal
  105. %% ===================================================================
  106. set_environment_for_app(Node, AppName, AppConfig) ->
  107. F = fun({Key, Val}) ->
  108. ok = rpc:call(Node, application, set_env, [AppName, Key, Val])
  109. end,
  110. lists:foreach(F, AppConfig).
  111. process_main() ->
  112. receive
  113. _ -> process_main()
  114. end.