From cc1e0fba5f59da9b5915712d4da7c8e2b1647e8d Mon Sep 17 00:00:00 2001 From: Radek Szymczyszyn Date: Fri, 29 Dec 2017 11:12:16 +0100 Subject: [PATCH 1/4] Stub escalus_ejabberd's escalus_server impl --- src/escalus_ejabberd.erl | 17 +++++++++++++++-- src/escalus_mongooseim.erl | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/escalus_ejabberd.erl b/src/escalus_ejabberd.erl index d487abc0..ea8b9402 100644 --- a/src/escalus_ejabberd.erl +++ b/src/escalus_ejabberd.erl @@ -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, @@ -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 %%-------------------------------------------------------------------- diff --git a/src/escalus_mongooseim.erl b/src/escalus_mongooseim.erl index e2be413d..4478978c 100644 --- a/src/escalus_mongooseim.erl +++ b/src/escalus_mongooseim.erl @@ -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. \ No newline at end of file + After == Before + Change. From f44bd8918e939c3e5decf36aeb6597613c75b828 Mon Sep 17 00:00:00 2001 From: Radek Szymczyszyn Date: Tue, 27 Mar 2018 14:57:48 +0200 Subject: [PATCH 2/4] Don't rely on CT for rpc_call/6 --- src/escalus_ct.erl | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/escalus_ct.erl b/src/escalus_ct.erl index 6d81f78c..24c74cbc 100644 --- a/src/escalus_ct.erl +++ b/src/escalus_ct.erl @@ -69,23 +69,29 @@ interpret_config_file_path(RelPath) -> error({escalus_error, beam_not_loaded}) end. +%% TODO: This is CT-free! Move out of escalus_ct. 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. + call_with_cookie_match(Node, Module, Function, Args, TimeOut, Cookie). + +%% 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. -spec log_stanza(undefined | binary(), in | out, exml_stream:element()) -> ok. log_stanza(undefined, _, _) -> ok; From d6b36d5a58786e5c502e00f0f0732ef5afccad03 Mon Sep 17 00:00:00 2001 From: Radek Szymczyszyn Date: Thu, 10 May 2018 12:30:02 +0200 Subject: [PATCH 3/4] Move escalus_ct:rpc_call/6 to escalus_rpc:call/6 and document appropriately --- src/escalus_ct.erl | 27 ----------------------- src/escalus_ejabberd.erl | 2 +- src/escalus_rpc.erl | 47 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 28 deletions(-) create mode 100644 src/escalus_rpc.erl diff --git a/src/escalus_ct.erl b/src/escalus_ct.erl index 24c74cbc..b40cb71f 100644 --- a/src/escalus_ct.erl +++ b/src/escalus_ct.erl @@ -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. @@ -69,30 +66,6 @@ interpret_config_file_path(RelPath) -> error({escalus_error, beam_not_loaded}) end. -%% TODO: This is CT-free! Move out of escalus_ct. -rpc_call(Node, Module, Function, Args, TimeOut, Cookie) -> - call_with_cookie_match(Node, Module, Function, Args, TimeOut, Cookie). - -%% 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. - -spec log_stanza(undefined | binary(), in | out, exml_stream:element()) -> ok. log_stanza(undefined, _, _) -> ok; log_stanza(Jid, Direction, Stanza) -> diff --git a/src/escalus_ejabberd.erl b/src/escalus_ejabberd.erl index ea8b9402..099c37bd 100644 --- a/src/escalus_ejabberd.erl +++ b/src/escalus_ejabberd.erl @@ -58,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], diff --git a/src/escalus_rpc.erl b/src/escalus_rpc.erl new file mode 100644 index 00000000..02b4a271 --- /dev/null +++ b/src/escalus_rpc.erl @@ -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. From 29a600038330c08fa8c44ecacf0f501d9710dc35 Mon Sep 17 00:00:00 2001 From: Radek Szymczyszyn Date: Fri, 11 May 2018 16:52:10 +0200 Subject: [PATCH 4/4] [skip ci] Fix doc --- src/escalus_rpc.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/escalus_rpc.erl b/src/escalus_rpc.erl index 02b4a271..ab112c4d 100644 --- a/src/escalus_rpc.erl +++ b/src/escalus_rpc.erl @@ -15,7 +15,7 @@ %% @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') +%% 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.