syn_test_suite_helper.erl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. %% ==========================================================================================================
  2. %% Syn - A global process registry.
  3. %%
  4. %% Copyright (C) 2015, Roberto Ostinelli <roberto@ostinelli.net>.
  5. %% All rights reserved.
  6. %%
  7. %% The MIT License (MIT)
  8. %%
  9. %% Copyright (c) 2015 Roberto Ostinelli
  10. %%
  11. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  12. %% of this software and associated documentation files (the "Software"), to deal
  13. %% in the Software without restriction, including without limitation the rights
  14. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. %% copies of the Software, and to permit persons to whom the Software is
  16. %% furnished to do so, subject to the following conditions:
  17. %%
  18. %% The above copyright notice and this permission notice shall be included in
  19. %% all copies or substantial portions of the Software.
  20. %%
  21. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. %% THE SOFTWARE.
  28. %% ==========================================================================================================
  29. -module(syn_test_suite_helper).
  30. %% API
  31. -export([set_environment_variables/0, set_environment_variables/1]).
  32. -export([start_slave/1, stop_slave/1]).
  33. -export([connect_node/1, disconnect_node/1]).
  34. -export([clean_after_test/0, clean_after_test/1]).
  35. -export([start_process/0, start_process/1, start_process/2]).
  36. -export([kill_process/1]).
  37. %% internal
  38. -export([process_main/0]).
  39. %% macros
  40. -define(SYN_TEST_CONFIG_FILENAME, "syn-test.config").
  41. %% ===================================================================
  42. %% API
  43. %% ===================================================================
  44. set_environment_variables() ->
  45. set_environment_variables(node()).
  46. set_environment_variables(Node) ->
  47. % read config file
  48. ConfigFilePath = filename:join([filename:dirname(code:which(?MODULE)), ?SYN_TEST_CONFIG_FILENAME]),
  49. {ok, [AppsConfig]} = file:consult(ConfigFilePath),
  50. % loop to set variables
  51. F = fun({AppName, AppConfig}) ->
  52. set_environment_for_app(Node, AppName, AppConfig)
  53. end,
  54. lists:foreach(F, AppsConfig).
  55. start_slave(NodeShortName) ->
  56. EbinFilePath = filename:join([filename:dirname(code:lib_dir(syn, ebin)), "ebin"]),
  57. TestFilePath = filename:join([filename:dirname(code:lib_dir(syn, ebin)), "test"]),
  58. %% start slave
  59. {ok, Node} = ct_slave:start(NodeShortName, [
  60. {boot_timeout, 10},
  61. {erl_flags, lists:concat(["-pa ", EbinFilePath, " ", TestFilePath])}
  62. ]),
  63. {ok, Node}.
  64. stop_slave(NodeShortName) ->
  65. {ok, _} = ct_slave:stop(NodeShortName).
  66. connect_node(Node) ->
  67. net_kernel:connect_node(Node).
  68. disconnect_node(Node) ->
  69. erlang:disconnect_node(Node).
  70. clean_after_test() ->
  71. %% delete table
  72. {atomic, ok} = mnesia:delete_table(syn_processes_table),
  73. %% stop mnesia
  74. mnesia:stop(),
  75. %% delete schema
  76. mnesia:delete_schema([node()]),
  77. %% stop syn
  78. syn:stop().
  79. clean_after_test(undefined) ->
  80. clean_after_test();
  81. clean_after_test(Node) ->
  82. %% delete table
  83. {atomic, ok} = mnesia:delete_table(syn_processes_table),
  84. %% stop mnesia
  85. mnesia:stop(),
  86. rpc:call(Node, mnesia, stop, []),
  87. %% delete schema
  88. mnesia:delete_schema([node(), Node]),
  89. %% stop syn
  90. syn:stop(),
  91. rpc:call(Node, syn, stop, []).
  92. start_process() ->
  93. Pid = spawn(fun process_main/0),
  94. Pid.
  95. start_process(Node) when is_atom(Node) ->
  96. Pid = spawn(Node, fun process_main/0),
  97. Pid;
  98. start_process(Loop) when is_function(Loop) ->
  99. Pid = spawn(Loop),
  100. Pid.
  101. start_process(Node, Loop)->
  102. Pid = spawn(Node, Loop),
  103. Pid.
  104. kill_process(Pid) ->
  105. exit(Pid, kill).
  106. %% ===================================================================
  107. %% Internal
  108. %% ===================================================================
  109. set_environment_for_app(Node, AppName, AppConfig) ->
  110. F = fun({Key, Val}) ->
  111. ok = rpc:call(Node, application, set_env, [AppName, Key, Val])
  112. end,
  113. lists:foreach(F, AppConfig).
  114. process_main() ->
  115. receive
  116. _ -> process_main()
  117. end.