Skip to content

Commit

Permalink
Simplify presence handling in mod_global_distrib_SUITE
Browse files Browse the repository at this point in the history
There was a function called 'wait_for_registration', that called
the non-existing old c2s module.

It was supposed to wait for the sm session, but actually the presence
is sent by the server after creating the session, not before as the
comment said.

Thus, it was enough to disable the filter predicate for Alice's
presences, and just handle them the same way as for other users.
  • Loading branch information
chrzaszcz committed Feb 9, 2023
1 parent e0f398f commit b7e6f3b
Showing 1 changed file with 13 additions and 43 deletions.
56 changes: 13 additions & 43 deletions big_tests/tests/mod_global_distrib_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,6 @@ test_pm_between_users_before_available_presence(Config) ->
escalus_client:stop(Config1, Eve).

test_two_way_pm(Alice, Eve) ->
%% Ensure that users are properly registered
%% Otherwise you can get "Unable to route global message... user not found in the routing table"
%% error, because "escalus_client:start" can return before SM registration is completed.
wait_for_registration(Alice, ct:get_config({hosts, mim, node})),
wait_for_registration(Eve, ct:get_config({hosts, reg, node})),

escalus_client:send(Alice, escalus_stanza:chat_to(Eve, <<"Hi to Eve from Europe1!">>)),
escalus_client:send(Eve, escalus_stanza:chat_to(Alice, <<"Hi to Alice from Asia!">>)),

Expand Down Expand Up @@ -443,10 +437,14 @@ test_muc_conversation_on_one_host(Config0) ->
RoomAddr = muc_helper:room_address(RoomJid),

escalus:send(Alice, muc_helper:stanza_muc_enter_room(RoomJid, AliceUsername)),
escalus:wait_for_stanza(Alice),
wait_for_muc_presence(Alice, RoomJid, AliceUsername),
wait_for_subject(Alice),

escalus:send(Eve, muc_helper:stanza_muc_enter_room(RoomJid, EveUsername)),
[_, _, _] = escalus:wait_for_stanzas(Eve, 3),
wait_for_muc_presence(Eve, RoomJid, AliceUsername),
wait_for_muc_presence(Eve, RoomJid, EveUsername),
wait_for_muc_presence(Alice, RoomJid, EveUsername),
wait_for_subject(Eve),

Msg= <<"Hi, Eve!">>,
escalus:send(Alice, escalus_stanza:groupchat_to(RoomAddr, Msg)),
Expand All @@ -473,7 +471,7 @@ test_muc_conversation_history(Config0) ->
RoomAddr = muc_helper:room_address(RoomJid),

escalus:send(Alice, muc_helper:stanza_muc_enter_room(RoomJid, AliceUsername)),
%% We don't care about presences from Alice, escalus would filter them out
wait_for_muc_presence(Alice, RoomJid, AliceUsername),
wait_for_subject(Alice),

send_n_muc_messages(Alice, RoomAddr, 3),
Expand All @@ -489,6 +487,7 @@ test_muc_conversation_history(Config0) ->

wait_for_muc_presence(Eve, RoomJid, AliceUsername),
wait_for_muc_presence(Eve, RoomJid, EveUsername),
wait_for_muc_presence(Alice, RoomJid, EveUsername),

%% XEP-0045: After sending the presence broadcast (and only after doing so),
%% the service MAY then send discussion history, the room subject,
Expand Down Expand Up @@ -659,8 +658,6 @@ test_pm_with_graceful_reconnection_to_different_server(Config) ->
escalus_client:send(Alice, chat_with_seqnum(Eve, <<"Hi from Europe1!">>)),

NewEve = connect_from_spec(EveSpec2, Config),
EveNode2 = ct:get_config({hosts, mim, node}),
wait_for_registration(NewEve, EveNode2),

ok = rpc(asia_node, sys, resume, [C2sPid]),

Expand Down Expand Up @@ -742,8 +739,6 @@ do_test_pm_with_ungraceful_reconnection_to_different_server(Config0, BeforeResum

%% Connect another one, we hope the message would be rerouted
NewEve = connect_from_spec(EveSpec2, Config),
EveNode2 = ct:get_config({hosts, mim, node}),
wait_for_registration(NewEve, EveNode2),

BeforeResume(),

Expand Down Expand Up @@ -1069,8 +1064,8 @@ hide_node(NodeName, Config) ->

connect_from_spec(UserSpec, Config) ->
{ok, User} = escalus_client:start(Config, UserSpec, <<"res1">>),
escalus_connection:set_filter_predicate(User, fun(S) -> not escalus_pred:is_presence(S) end),
escalus_story:send_initial_presence(User),
escalus:assert(is_presence, escalus_client:wait_for_stanza(User)),
User.

chat_with_seqnum(To, Text) ->
Expand Down Expand Up @@ -1252,23 +1247,11 @@ trigger_rebalance(NodeName, DestinationDomain) when is_binary(DestinationDomain)
%% -----------------------------------------------------------------------
%% Escalus-related helpers

%% Receive messages with Bodies in any order, skipping presences from stream resumption
user_receives(User, Bodies) ->
ExpectedLength = length(Bodies),
Messages = escalus_client:wait_for_stanzas(User, ExpectedLength),
SortedMessages = order_by_seqnum(Messages),
case length(Messages) of
ExpectedLength ->
Checks = [escalus_pred:is_chat_message(Body, Stanza) || {Body, Stanza} <- lists:zip(Bodies, SortedMessages)],
case lists:all(fun(Check) -> Check end, Checks) of
true ->
ok;
false ->
ct:fail({user_receives_failed, {wanted, Bodies}, {received, SortedMessages}, {check, Checks}})
end;
_ ->
ct:fail({user_receives_not_enough, {wanted, Bodies}, {received, SortedMessages}})
end.

Opts = #{pred => fun(Stanza) -> not escalus_pred:is_presence(Stanza) end},
Checks = [fun(Stanza) -> escalus_pred:is_chat_message(Body, Stanza) end || Body <- Bodies],
escalus:assert_many(Checks, [escalus_connection:receive_stanza(User, Opts) || _ <- Bodies]).

%% -----------------------------------------------------------------------
%% Refreshing helpers
Expand Down Expand Up @@ -1306,19 +1289,6 @@ wait_for_domain(Node, Domain) ->
end,
mongoose_helper:wait_until(F, true, #{name => {wait_for_domain, Node, Domain}}).

%% We receive presence BEFORE session is registered in ejabberd_sm.
%% So, to ensure that we processed do_open_session completely, let's send a "ping".
%% by calling the c2s process.
%% That call would only return, when all messages in erlang message queue
%% are processed.
wait_for_registration(Client, Node) ->
RPCSpec = #{node => Node},
mongoose_helper:wait_until(fun() -> is_pid(mongoose_helper:get_session_pid(Client, RPCSpec)) end, true,
#{name => wait_for_session}),
C2sPid = mongoose_helper:get_session_pid(Client, RPCSpec),
rpc:call(node(C2sPid), ejabberd_c2s, get_info, [C2sPid]),
ok.


%% -----------------------------------------------------------------------
%% Ensure, that endpoints are up
Expand Down

0 comments on commit b7e6f3b

Please sign in to comment.