From e0bb95ed1e7e1314697b51d10ae59cd09c557941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Fri, 11 Mar 2016 13:24:49 +0100 Subject: Reduce code duplication in called_node_dies/1 and friends --- lib/kernel/test/rpc_SUITE.erl | 50 ++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/lib/kernel/test/rpc_SUITE.erl b/lib/kernel/test/rpc_SUITE.erl index 77b85f39e0..0e9068fa2b 100644 --- a/lib/kernel/test/rpc_SUITE.erl +++ b/lib/kernel/test/rpc_SUITE.erl @@ -348,66 +348,54 @@ suicide(Mod, Func, Args) -> called_node_dies(Config) when is_list(Config) -> PA = filename:dirname(code:which(?MODULE)), - %% + node_rep( fun (Tag, Call, Args) -> {Tag,{badrpc,nodedown}} = {Tag,apply(rpc, Call, Args)} end, "rpc_SUITE_called_node_dies_1", PA, ?MODULE, suicide, [erlang,halt,[]]), + node_rep( fun (Tag, Call, Args) -> {Tag,{badrpc,nodedown}} = {Tag,apply(rpc, Call, Args)} end, "rpc_SUITE_called_node_dies_2", PA, ?MODULE, suicide, [init,stop,[]]), + node_rep( fun (Tag, Call, Args=[_|_]) -> {Tag,{'EXIT',{killed,_}}} = {Tag,catch {noexit,apply(rpc, Call, Args)}} end, "rpc_SUITE_called_node_dies_3", PA, ?MODULE, suicide, [erlang,exit,[rex,kill]]), + node_rep( - fun %% Cannot block call rpc - will hang - (_Tag, block_call, _Args) -> ok; + fun (_Tag, block_call, _Args) -> + %% Cannot block call rpc - will hang + ok; (Tag, Call, Args=[_|_]) -> {Tag,{'EXIT',{normal,_}}} = {Tag,catch {noexit,apply(rpc, Call, Args)}} end, "rpc_SUITE_called_node_dies_4", PA, ?MODULE, suicide, [rpc,stop,[]]), - %% + ok. node_rep(Fun, Name, PA, M, F, A) -> - {ok, Na} = test_server:start_node(list_to_atom(Name++"_a"), slave, - [{args, "-pa " ++ PA}]), - Fun(a, call, [Na, M, F, A]), - catch test_server:stop_node(Na), - {ok, Nb} = test_server:start_node(list_to_atom(Name++"_b"), slave, - [{args, "-pa " ++ PA}]), - Fun(b, call, [Nb, M, F, A, infinity]), - catch test_server:stop_node(Nb), - {ok, Nc} = test_server:start_node(list_to_atom(Name++"_c"), slave, - [{args, "-pa " ++ PA}]), - Fun(c, call, [Nc, M, F, A, infinity]), - catch test_server:stop_node(Nc), - %% - {ok, Nd} = test_server:start_node(list_to_atom(Name++"_d"), slave, - [{args, "-pa " ++ PA}]), - Fun(d, block_call, [Nd, M, F, A]), - catch test_server:stop_node(Nd), - {ok, Ne} = test_server:start_node(list_to_atom(Name++"_e"), slave, - [{args, "-pa " ++ PA}]), - Fun(e, block_call, [Ne, M, F, A, infinity]), - catch test_server:stop_node(Ne), - {ok, Nf} = test_server:start_node(list_to_atom(Name++"_f"), slave, - [{args, "-pa " ++ PA}]), - Fun(f, block_call, [Nf, M, F, A, infinity]), - catch test_server:stop_node(Nf), + node_rep_call(a, call, [M,F,A], Fun, Name, PA), + node_rep_call(b, call, [M,F,A,infinity], Fun, Name, PA), + node_rep_call(c, block_call, [M,F,A], Fun, Name, PA), + node_rep_call(d, block_call, [M,F,A,infinity], Fun, Name, PA). + +node_rep_call(Tag, Call, Args, Fun, Name0, PA) -> + Name = list_to_atom(Name0 ++ "_" ++ atom_to_list(Tag)), + {ok, N} = test_server:start_node(Name, slave, + [{args, "-pa " ++ PA}]), + Fun(Tag, Call, [N|Args]), + catch test_server:stop_node(N), ok. - - %% OTP-3766. called_throws(Config) when is_list(Config) -> PA = filename:dirname(code:which(?MODULE)), -- cgit v1.2.3 From cccf762d93640b707f8e1515604cebcfbe379c04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Fri, 11 Mar 2016 13:13:59 +0100 Subject: Don't throw exceptions from rpc:call() and rpc:block_call() The documentation for rpc:call() says: Evaluates apply(Module, Function, Args) on the node Node and returns the corresponding value Res, or {badrpc, Reason} if the call fails. What is not said that rpc:call() can generate an exception if the 'rex' process on the other node is killed: (kalle@host)1> Rex = rpc:call(arne@host, erlang, whereis, [rex]). <6937.14.0> (kalle@host)2> rpc:call(arne@host, erlang, exit, [Rex,kill]). ** exception exit: {killed, {gen_server,call, [{rex,arne@host}, {call,erlang,exit,[<6937.14.0>,kill],<0.33.0>}, infinity]}} in function rpc:rpc_check/1 (rpc.erl, line 361) On the other hand, if the other node shuts down for some other reason, we'll get a {badrpc,nodedown} result: (kalle@host)5> rpc:call(arne@host, erlang, halt, []). {badrpc,nodedown} There does not seem to be any reason to handle the two cases differently. If the 'rex' process is terminated on the other node, it will shut down shortly thereafter. Therefore, change rpc:call() and rpc:block_call() to always return {badrpc,Reason} is the call fails: (kalle@host)1> Rex = rpc:call(arne@host, erlang, whereis, [rex]). <6937.14.0> (kalle@host)2> rpc:call(arne@host, erlang, exit, [Rex,kill]). {badrpc,{'EXIT',{killed,{gen_server,call, [{rex,arne@host}, {call,erlang,exit,[<7126.14.0>,kill],<0.33.0>}, infinity]}}}} --- lib/kernel/src/rpc.erl | 8 ++++++-- lib/kernel/test/rpc_SUITE.erl | 26 +++++++++++--------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/kernel/src/rpc.erl b/lib/kernel/src/rpc.erl index d3db8eb80a..b9284487d2 100644 --- a/lib/kernel/src/rpc.erl +++ b/lib/kernel/src/rpc.erl @@ -357,8 +357,12 @@ do_call(Node, Request, Timeout) -> rpc_check_t({'EXIT', {timeout,_}}) -> {badrpc, timeout}; rpc_check_t(X) -> rpc_check(X). -rpc_check({'EXIT', {{nodedown,_},_}}) -> {badrpc, nodedown}; -rpc_check({'EXIT', X}) -> exit(X); +rpc_check({'EXIT', {{nodedown,_},_}}) -> + {badrpc, nodedown}; +rpc_check({'EXIT', _}=Exit) -> + %% Should only happen if the rex process on the other node + %% died. + {badrpc, Exit}; rpc_check(X) -> X. diff --git a/lib/kernel/test/rpc_SUITE.erl b/lib/kernel/test/rpc_SUITE.erl index 0e9068fa2b..101cff7ed2 100644 --- a/lib/kernel/test/rpc_SUITE.erl +++ b/lib/kernel/test/rpc_SUITE.erl @@ -350,34 +350,30 @@ called_node_dies(Config) when is_list(Config) -> PA = filename:dirname(code:which(?MODULE)), node_rep( - fun (Tag, Call, Args) -> - {Tag,{badrpc,nodedown}} = - {Tag,apply(rpc, Call, Args)} + fun (Call, Args) -> + {badrpc,nodedown} = apply(rpc, Call, Args) end, "rpc_SUITE_called_node_dies_1", PA, ?MODULE, suicide, [erlang,halt,[]]), node_rep( - fun (Tag, Call, Args) -> - {Tag,{badrpc,nodedown}} = - {Tag,apply(rpc, Call, Args)} + fun (Call, Args) -> + {badrpc,nodedown} = apply(rpc, Call, Args) end, "rpc_SUITE_called_node_dies_2", PA, ?MODULE, suicide, [init,stop,[]]), node_rep( - fun (Tag, Call, Args=[_|_]) -> - {Tag,{'EXIT',{killed,_}}} = - {Tag,catch {noexit,apply(rpc, Call, Args)}} + fun (Call, Args=[_|_]) -> + {badrpc,{'EXIT',{killed,_}}} = apply(rpc, Call, Args) end, "rpc_SUITE_called_node_dies_3", PA, ?MODULE, suicide, [erlang,exit,[rex,kill]]), node_rep( - fun (_Tag, block_call, _Args) -> + fun (block_call, _Args) -> %% Cannot block call rpc - will hang ok; - (Tag, Call, Args=[_|_]) -> - {Tag,{'EXIT',{normal,_}}} = - {Tag,catch {noexit,apply(rpc, Call, Args)}} - end, "rpc_SUITE_called_node_dies_4", + (Call, Args=[_|_]) -> + {badrpc,{'EXIT',{normal,_}}} = apply(rpc, Call, Args) + end, "rpc_SUITE_called_node_dies_4", PA, ?MODULE, suicide, [rpc,stop,[]]), ok. @@ -392,7 +388,7 @@ node_rep_call(Tag, Call, Args, Fun, Name0, PA) -> Name = list_to_atom(Name0 ++ "_" ++ atom_to_list(Tag)), {ok, N} = test_server:start_node(Name, slave, [{args, "-pa " ++ PA}]), - Fun(Tag, Call, [N|Args]), + Fun(Call, [N|Args]), catch test_server:stop_node(N), ok. -- cgit v1.2.3