Skip to content

Commit

Permalink
Address deprecation warnings in OTP25 (#294)
Browse files Browse the repository at this point in the history
Address the deprecation of the old slave module in OTP 25.x. To do so, two new macros ?START_PEER_NODE/1 and ?STOP_PEER_NODE/1 have been added, which handle the node setup and teardown properly depending on the OTP version PropEr was compiled with.

I also took the chance to improve the naming in the codebase around said nodes, and to make the code a bid cleaner.

Fixes #293
  • Loading branch information
pablocostass authored May 31, 2022
1 parent 564283d commit cfc29e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
22 changes: 22 additions & 0 deletions include/proper_internal.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@

-include("proper_common.hrl").

%%------------------------------------------------------------------------------
%% Peer node module definition
%%------------------------------------------------------------------------------

-if (?OTP_RELEASE >= 25).
-define(CASE_START_PEER_NODE(Name),
case peer:start_link(#{name => Name}) of
{ok, Pid, Node} ->
register(Node, Pid),
_ = update_worker_node_ref({Node, {already_running, false}}),
Node;).
-define(STOP_PEER_NODE(Name),
peer:stop(Name)).
-else.
-define(CASE_START_PEER_NODE(Name),
case slave:start_link(_HostName = list_to_atom(net_adm:localhost()), Name) of
{ok, Node} ->
_ = update_worker_node_ref({Node, {already_running, false}}),
Node;).
-define(STOP_PEER_NODE(Name),
slave:stop(Node)).
-endif.

%%------------------------------------------------------------------------------
%% Random generator selection
Expand Down
27 changes: 13 additions & 14 deletions src/proper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2400,14 +2400,8 @@ update_worker_node_ref(NodeName) ->
%% @doc Starts a remote node to ensure the testing will not
%% crash the BEAM, and loads on it all the needed code.
-spec start_node(node()) -> node().
start_node(SlaveName) ->
[] = os:cmd("epmd -daemon"),
HostName = list_to_atom(net_adm:localhost()),
_ = net_kernel:start([proper_master, shortnames]),
case slave:start_link(HostName, SlaveName) of
{ok, Node} ->
_ = update_worker_node_ref({Node, {already_running, false}}),
Node;
start_node(Name) ->
?CASE_START_PEER_NODE(Name) %% the 'ok' case of this case statement is in the macro
{error, {already_running, Node}} ->
_ = update_worker_node_ref({Node, {already_running, true}}),
Node
Expand Down Expand Up @@ -2466,20 +2460,25 @@ ensure_code_loaded(Nodes) ->
%% @doc Starts multiple (NumNodes) remote nodes.
-spec start_nodes(non_neg_integer()) -> [node()].
start_nodes(NumNodes) ->
[start_node(list_to_atom("proper_slave_" ++ integer_to_list(N)))
[] = os:cmd("epmd -daemon"),
_ = net_kernel:start([proper_leader, shortnames]),
[start_node(list_to_atom("proper_follower_" ++ integer_to_list(N)))
|| N <- lists:seq(1, NumNodes)].

-spec stop_node({node(), {already_running, boolean()}}) -> ok.
stop_node({Node, {already_running, false}}) ->
?STOP_PEER_NODE(Node);
stop_node({_Node, {already_running, true}}) ->
ok.

%% @private
%% @doc Stops all the registered (started) nodes.
%% @doc Stops all the registered (started by us) nodes.
-spec stop_nodes() -> 'ok'.
stop_nodes() ->
case get(worker_nodes) of
undefined -> ok;
Nodes ->
StopFun = fun({Node, {already_running, false}}) -> slave:stop(Node);
({_Node, {already_running, true}}) -> ok
end,
lists:foreach(StopFun, Nodes),
lists:foreach(fun stop_node/1, Nodes),
_ = net_kernel:stop(),
erase(worker_nodes),
ok
Expand Down

0 comments on commit cfc29e7

Please sign in to comment.