emqx_cth_peer.erl 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2023-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. %% @doc Common Test Helper proxy module for slave -> peer migration.
  17. %% OTP 26 has slave module deprecated, use peer instead.
  18. -module(emqx_cth_peer).
  19. -export([start/2, start/3, start/4]).
  20. -export([start_link/2, start_link/3, start_link/4]).
  21. -export([stop/1]).
  22. start(Name, Args) ->
  23. start(Name, Args, []).
  24. start(Name, Args, Envs) ->
  25. start(Name, Args, Envs, timer:seconds(20)).
  26. start(Name, Args, Envs, Timeout) when is_atom(Name) ->
  27. do_start(Name, Args, Envs, Timeout, start).
  28. start_link(Name, Args) ->
  29. start_link(Name, Args, []).
  30. start_link(Name, Args, Envs) ->
  31. start_link(Name, Args, Envs, timer:seconds(20)).
  32. start_link(Name, Args, Envs, Timeout) when is_atom(Name) ->
  33. do_start(Name, Args, Envs, Timeout, start_link).
  34. do_start(Name0, Args, Envs, Timeout, Func) when is_atom(Name0) ->
  35. {Name, Host} = parse_node_name(Name0),
  36. {ok, Pid, Node} = peer:Func(#{
  37. name => Name,
  38. host => Host,
  39. args => Args,
  40. env => Envs,
  41. wait_boot => Timeout,
  42. longnames => true,
  43. shutdown => {halt, 1000}
  44. }),
  45. true = register(Node, Pid),
  46. {ok, Node}.
  47. stop(Node) when is_atom(Node) ->
  48. Pid = whereis(Node),
  49. case is_pid(Pid) of
  50. true ->
  51. unlink(Pid),
  52. ok = peer:stop(Pid);
  53. false ->
  54. ct:pal("The control process for node ~p is unexpetedly down", [Node]),
  55. ok
  56. end.
  57. parse_node_name(NodeName) ->
  58. case string:tokens(atom_to_list(NodeName), "@") of
  59. [Name, Host] ->
  60. {list_to_atom(Name), Host};
  61. [_] ->
  62. {NodeName, host()}
  63. end.
  64. host() ->
  65. [_Name, Host] = string:tokens(atom_to_list(node()), "@"),
  66. Host.