Skip to content

Commit

Permalink
Merge pull request #170 from esl/ct-free-rpc-call
Browse files Browse the repository at this point in the history
CT-free RPC call
  • Loading branch information
kzemek authored May 14, 2018
2 parents 1a59e26 + 29a6000 commit 2e57435
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
21 changes: 0 additions & 21 deletions src/escalus_ct.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
get_config/1,
log_stanza/3]).

%% What about that?
-export([rpc_call/6]).

-define(APPNAME, escalus).

-spec add_log_link(any(), any(), any()) -> ok | false.
Expand Down Expand Up @@ -69,24 +66,6 @@ interpret_config_file_path(RelPath) ->
error({escalus_error, beam_not_loaded})
end.

rpc_call(Node, Module, Function, Args, TimeOut, Cookie) ->
case is_ct_available() of
true ->
Result = ct_rpc:call(Node, Module, Function, Args, TimeOut, Cookie),
case Result of
{badrpc, Reason} ->
ct:pal("issue=rpc_call_failed "
"node=~p function=~p:~p reason=~p",
[Node, Module, Function, Reason]);
_ ->
ok
end,
Result;
false ->
%% TODO: don't error out, should be easy to simulate ct_rpc:call/6
error({escalus_error, common_test_unavailable})
end.

-spec log_stanza(undefined | binary(), in | out, exml_stream:element()) -> ok.
log_stanza(undefined, _, _) -> ok;
log_stanza(Jid, Direction, Stanza) ->
Expand Down
19 changes: 16 additions & 3 deletions src/escalus_ejabberd.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
-module(escalus_ejabberd).

-behaviour(escalus_user_db).

%% escalus_user_db callbacks
-export([start/1,
stop/1,
create_users/2,
delete_users/2]).

-behaviour(escalus_server).
-export([pre_story/1,
post_story/1,
name/0]).

-export([rpc/3,
remote_display/1,
remote_format/1,
Expand Down Expand Up @@ -55,7 +58,7 @@
rpc(M, F, A) ->
Node = escalus_ct:get_config(ejabberd_node),
Cookie = escalus_ct:get_config(ejabberd_cookie),
escalus_ct:rpc_call(Node, M, F, A, 3000, Cookie).
escalus_rpc:call(Node, M, F, A, 3000, Cookie).

remote_display(String) ->
Line = [$\n, [$- || _ <- String], $\n],
Expand Down Expand Up @@ -216,6 +219,16 @@ delete_users(Config, Users) ->
end, Users),
Config.

%%--------------------------------------------------------------------
%% escalus_server callbacks
%%--------------------------------------------------------------------

pre_story(Config) -> Config.

post_story(Config) -> Config.

name() -> ?MODULE.

%%--------------------------------------------------------------------
%% Helpers
%%--------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/escalus_mongooseim.erl
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ check_metric_change({{Metric, Changes}, Before, After}, Acc) when is_list(Change
check_change(Before, After, Change) when is_atom(Change) ->
erlang:apply(erlang, Change, [After, Before]);
check_change(Before, After, Change) when is_integer(Change) ->
After == Before + Change.
After == Before + Change.
47 changes: 47 additions & 0 deletions src/escalus_rpc.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
%% @doc This module should not exist as it has nothing to do with XMPP
%% nor is it needed by Escalus itself.
%%
%% The module contains RPC helpers which used to rely on Common Test,
%% but do not do so anymore.
-module(escalus_rpc).

%% Public
-export([call/6]).

%%
%% Public
%%

%% @doc Emulate `ct_rpc:call/6' but do not rely on Common Test.
%% `call/6' takes a `Cookie' as the last parameter,
%% so that nodes with different cookies can be easily called.
%% However, this function is not safe (and neither is the original `ct_rpc:call/6')
%% in a concurrent environment as it gets/sets the cookie
%% with `erlang:get_cookie/0' and `erlang:set_cookie/1'.
%% Interleaving these calls in concurrent processes is prone to race conditions.
call(Node, Module, Function, Args, TimeOut, Cookie) ->
call_with_cookie_match(Node, Module, Function, Args, TimeOut, Cookie).

%%
%% Internal
%%

%% Copied from ct_rpc and renamed.
call_with_cookie_match(Node, Module, Function, Args, TimeOut, Cookie) when is_atom(Node) ->
Cookie0 = set_the_cookie(Cookie),
Result = case rpc:call(Node, Module, Function, Args, TimeOut) of
{badrpc, Reason} ->
error({badrpc, Reason}, [Node, Module, Function, Args, TimeOut, Cookie]);
R ->
R
end,
_ = set_the_cookie(Cookie0),
Result.

%% Copied from ct_rpc.
set_the_cookie([]) ->
[];
set_the_cookie(Cookie) ->
Cookie0 = erlang:get_cookie(),
erlang:set_cookie(node(),Cookie),
Cookie0.

0 comments on commit 2e57435

Please sign in to comment.