Skip to content

Commit 7ea42e8

Browse files
authored
Merge pull request #1962 from esl/incremental-sleep-redundancy
Incremental sleep redundancy
2 parents 73f6c9a + 89457a4 commit 7ea42e8

12 files changed

+168
-238
lines changed

big_tests/tests/bosh_SUITE.erl

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ do_not_accept_0_hold_value(Config) ->
189189
send_specific_hold(Config, HoldValue) ->
190190
{Server, Path, Client} = get_fusco_connection(Config),
191191

192-
Rid = random:uniform(1000000),
192+
Rid = rand:uniform(1000000),
193193
Body0 = escalus_bosh:session_creation_body(2, <<"1.0">>, <<"en">>, Rid, Server, nil),
194194
#xmlel{attrs = Attrs0} = Body0,
195195
Attrs = lists:keyreplace(<<"hold">>, 1, Attrs0, {<<"hold">>, HoldValue}),
@@ -379,7 +379,7 @@ cant_send_invalid_rid(Config) ->
379379

380380
escalus:assert(is_stream_end, escalus:wait_for_stanza(Carol)),
381381
escalus_client:wait_for_close(Config, Carol, timer:seconds(1)),
382-
true = wait_for_session_close(Sid, 10)
382+
{ok, false} = wait_for_session_close(Sid)
383383

384384
end).
385385

@@ -464,7 +464,7 @@ disconnect_inactive(Config) ->
464464
timer:sleep(2 * timer:seconds(?INACTIVITY)),
465465

466466
%% Assert Carol has been disconnected due to inactivity.
467-
false = is_sesssion_alive(Sid),
467+
false = is_session_alive(Sid),
468468

469469
%% We don't need to close the session in escalus_bosh:stop/1
470470
escalus_client:kill_connection(Config, Carol)
@@ -486,7 +486,7 @@ connection_interrupted(Config) ->
486486

487487
%% Assert Carol has not been disconnected yet.
488488
timer:sleep(100),
489-
true = is_sesssion_alive(Sid),
489+
true = is_session_alive(Sid),
490490

491491
%% Wait for disconnection because of inactivity timeout.
492492
%% Keep in mind this only works due to the max_wait also being lowered.
@@ -495,7 +495,7 @@ connection_interrupted(Config) ->
495495
timer:sleep(timer:seconds(?INACTIVITY) + timer:seconds(?MAX_WAIT)),
496496

497497
%% Assert Carol has been disconnected due to inactivity.
498-
false = is_sesssion_alive(Sid)
498+
false = is_session_alive(Sid)
499499

500500
end).
501501

@@ -510,7 +510,7 @@ interrupt_long_poll_is_activity(ConfigIn) ->
510510
%% to Carol and one handler for Carol.
511511
Sid = get_bosh_sid(Carol),
512512
{_, _, CarolSessionPid} = get_bosh_session(Sid),
513-
1 = wait_for_handler(CarolSessionPid),
513+
{ok, 1} = wait_for_handler(CarolSessionPid),
514514

515515
%% Send a message. A new connection should be established, and
516516
%% the existing long-poll connection should be closed.
@@ -523,8 +523,8 @@ interrupt_long_poll_is_activity(ConfigIn) ->
523523

524524
%% No disconnection should have occurred.
525525
escalus_assert:has_no_stanzas(Carol),
526-
true = is_sesssion_alive(Sid),
527-
1 = wait_for_handler(CarolSessionPid)
526+
true = is_session_alive(Sid),
527+
{ok, 1} = wait_for_handler(CarolSessionPid)
528528

529529
end).
530530

@@ -536,13 +536,13 @@ reply_on_pause(Config) ->
536536
set_keepalive(Carol, false),
537537

538538
%% Sanity check - there should be one handler for Carol.
539-
1 = wait_for_handler(CarolSessionPid),
539+
{ok, 1} = wait_for_handler(CarolSessionPid),
540540

541541
pause(Carol, 10),
542542

543543
%% There should be no handlers for Carol,
544544
%% but the session should be alive.
545-
true = is_sesssion_alive(Sid),
545+
true = is_session_alive(Sid),
546546
0 = length(get_handlers(CarolSessionPid)),
547547
0 = get_bosh_requests(Carol)
548548

@@ -556,13 +556,13 @@ cant_pause_for_too_long(Config) ->
556556
set_keepalive(Carol, false),
557557

558558
%% Sanity check - there should be one handler for Carol.
559-
1 = wait_for_handler(CarolSessionPid),
559+
{ok, 1} = wait_for_handler(CarolSessionPid),
560560

561561
pause(Carol, 10000),
562562

563563
escalus:assert(is_stream_end, escalus:wait_for_stanza(Carol)),
564564
escalus_client:wait_for_close(Config, Carol, timer:seconds(1)),
565-
false = is_sesssion_alive(Sid)
565+
false = is_session_alive(Sid)
566566

567567
end).
568568

@@ -575,7 +575,7 @@ pause_request_is_activity(Config) ->
575575
set_keepalive(Carol, false),
576576

577577
%% Sanity check - there should be one handler for Carol.
578-
1 = wait_for_handler(CarolSessionPid),
578+
{ok, 1} = wait_for_handler(CarolSessionPid),
579579

580580
%% Wait most of the allowed inactivity interval.
581581
timer:sleep(timer:seconds(?INACTIVITY - 1)),
@@ -588,7 +588,7 @@ pause_request_is_activity(Config) ->
588588

589589
%% No disconnection should've occured.
590590
escalus_assert:has_no_stanzas(Carol),
591-
true = is_sesssion_alive(Sid)
591+
true = is_session_alive(Sid)
592592

593593
end).
594594

@@ -618,7 +618,7 @@ reply_in_time(ConfigIn) ->
618618
Empty = escalus_bosh:empty_body(Rid, Sid),
619619
bosh_send_raw(Carol, Empty),
620620
timer:sleep(100),
621-
1 = wait_for_handler(CarolSessionPid),
621+
{ok, 1} = wait_for_handler(CarolSessionPid),
622622

623623
timer:sleep(timer:seconds(Wait) + 100),
624624

@@ -870,34 +870,24 @@ server_acks_opt() ->
870870
fun(V) -> rpc(mim(), mod_bosh, set_server_acks, [V]) end,
871871
true}.
872872

873-
is_sesssion_alive(Sid) ->
873+
is_session_alive(Sid) ->
874874
BoshSessions = get_bosh_sessions(),
875875
lists:keymember(Sid, 2, BoshSessions).
876876

877-
wait_for_session_close(Sid, 0) ->
878-
false == is_sesssion_alive(Sid);
879-
wait_for_session_close(Sid, N) ->
880-
case is_sesssion_alive(Sid) of
881-
false ->
882-
true;
883-
_ ->
884-
timer:sleep(100),
885-
wait_for_session_close(Sid, N-1)
886-
end.
877+
wait_for_session_close(Sid) ->
878+
mongoose_helper:wait_until(fun() -> is_session_alive(Sid) end, false,
879+
#{
880+
time_left => timer:seconds(10),
881+
time_sleep => timer:seconds(1),
882+
name => is_session_alive
883+
}).
887884

888885
wait_for_handler(Pid) ->
889-
wait_for_handler(Pid, 10).
890-
891-
wait_for_handler(Pid, 0) ->
892-
length(get_handlers(Pid));
893-
wait_for_handler(Pid, N) ->
894-
case get_handlers(Pid) of
895-
[] ->
896-
timer:sleep(50),
897-
wait_for_handler(Pid, N-1);
898-
L ->
899-
length(L)
900-
end.
886+
mongoose_helper:wait_until(fun() -> length(get_handlers(Pid)) end, 1, #{
887+
time_left => timer:seconds(10),
888+
time_sleep => timer:seconds(1),
889+
name => get_handlers
890+
}).
901891

902892
domain() ->
903893
ct:get_config({hosts, mim, domain}).

big_tests/tests/component_helper.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ disconnect_component(Component, Addr) ->
4444
disconnect_components(Components, Addr) ->
4545
%% TODO replace 'kill' with 'stop' when server supports stream closing
4646
[escalus_connection:kill(Component) || Component <- Components],
47-
mongoose_helper:wait_until(fun() -> rpc(ejabberd_router, lookup_component, [Addr]) =:= [] end, 5, 200).
47+
mongoose_helper:wait_until(fun() -> rpc(ejabberd_router, lookup_component, [Addr]) =:= [] end, true,
48+
#{name => rpc}).
4849

4950
rpc(M, F, A) ->
5051
Node = ct:get_config({hosts, mim, node}),

big_tests/tests/mam_helper.erl

Lines changed: 44 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
-compile(export_all).
1919

20-
-export([wait_for_complete_archive_response/3]).
21-
2220
-include("mam_helper.hrl").
2321
-include_lib("escalus/include/escalus.hrl").
2422
-include_lib("escalus/include/escalus_xmlns.hrl").
@@ -764,8 +762,8 @@ clean_archives(Config) ->
764762
%% It is not the best place to delete these messages.
765763
[ok = delete_offline_messages(S, U) || {S, U} <- SUs],
766764
[ok = delete_archive(S, U) || {S, U} <- SUs],
767-
%% Retry 10 times if not empty
768-
[assert_empty_archive(S, U, 10) || {S, U} <- SUs],
765+
%% Wait for archive to be empty
766+
[wait_for_archive_size(S, U, 0) || {S, U} <- SUs],
769767
Config.
770768

771769
destroy_room(Config) ->
@@ -776,7 +774,7 @@ clean_room_archive(Config) ->
776774
Room = ?config(room, Config),
777775
delete_room_archive(muc_host(), Room),
778776
%% Retry 10 times if not empty
779-
assert_empty_room_archive(muc_host(), Room, 10),
777+
wait_for_room_archive_size(muc_host(), Room, 0),
780778
Config.
781779

782780
serv_users(Config) ->
@@ -787,63 +785,48 @@ serv_user(Config, UserSpec) ->
787785
[Username, Server, _Pass] = escalus_users:get_usp(Config, UserSpec),
788786
{Server, Username}.
789787

790-
%% @doc Check, that the archive is empty.
791-
assert_empty_archive(Server, Username, RetryTimes) when is_integer(RetryTimes) ->
792-
%% Wait for zero messages in archive
793-
case wait_for_archive_size(Server, Username, RetryTimes, 0) of
794-
0 -> ok;
795-
X -> ct:fail({not_empty, Server, Username, {actual_size, X}})
796-
end.
797-
798788
wait_for_archive_size(User, ExpectedSize) ->
799-
Server = escalus_utils:get_server(User),
800-
Username = escalus_utils:jid_to_lower(escalus_utils:get_username(User)),
801-
case wait_for_archive_size(Server, Username, 100, ExpectedSize) of
802-
ExpectedSize ->
803-
ok;
804-
ArchiveSize ->
805-
ct:fail({expected_archive_size, Username, Server, ExpectedSize,
806-
{actual_size, ArchiveSize}})
807-
end.
808-
809-
wait_for_archive_size(Server, Username, _RetryTimes=0, _ExpectedSize) ->
810-
archive_size(Server, Username);
811-
wait_for_archive_size(Server, Username, RetryTimes, ExpectedSize) when RetryTimes > 0 ->
812-
case archive_size(Server, Username) of
813-
ExpectedSize ->
814-
ExpectedSize;
815-
_ActualSize ->
816-
%% Wait and retry
817-
timer:sleep(100),
818-
wait_for_archive_size(Server, Username, RetryTimes-1, ExpectedSize)
819-
end.
820-
821-
wait_for_archive_size_or_warning(Server, Username, RetryTimes, ExpectedSize) ->
822-
case wait_for_archive_size(Server, Username, RetryTimes, ExpectedSize) of
823-
ExpectedSize -> ok;
824-
ActualSize ->
825-
ct:pal("issue=wait_for_archive_size_or_warning, expected_size=~p, actual_size=~p",
826-
[ExpectedSize, ActualSize])
827-
end.
828-
829-
%% @doc Check, that the archive is empty.
830-
assert_empty_room_archive(Server, Username, RetryTimes) ->
831-
%% Wait for zero messages in archive
832-
case wait_for_room_archive_size(Server, Username, RetryTimes, 0) of
833-
0 -> ok;
834-
X -> ct:fail({room_not_empty, Server, Username, {actual_size, X}})
789+
wait_for_archive_size(
790+
escalus_utils:get_server(User),
791+
escalus_utils:jid_to_lower(escalus_utils:get_username(User)),
792+
ExpectedSize).
793+
794+
wait_for_archive_size(Server, Username, ExpectedSize) ->
795+
mongoose_helper:wait_until(fun() -> archive_size(Server, Username) end,
796+
ExpectedSize,
797+
#{
798+
time_left => timer:seconds(20),
799+
name => archive_size
800+
}).
801+
802+
wait_for_archive_size_or_warning(Server, Username, ExpectedSize) ->
803+
try mongoose_helper:wait_until(fun() -> archive_size(Server, Username) end,
804+
ExpectedSize,
805+
#{
806+
time_left => timer:seconds(20),
807+
name => archive_size
808+
}) of
809+
{ok, ExpectedSize} ->
810+
ok
811+
catch
812+
_Error:Reason ->
813+
ct:pal("issue=wait_for_archive_size_or_warning, expected_size=~p, log=~p",
814+
[ExpectedSize, Reason])
835815
end.
836816

837-
wait_for_room_archive_size(Server, Username, _RetryTimes=0, _ExpectedSize) ->
838-
room_archive_size(Server, Username);
839-
wait_for_room_archive_size(Server, Username, RetryTimes, ExpectedSize) when RetryTimes > 0 ->
840-
case room_archive_size(Server, Username) of
841-
ExpectedSize ->
842-
ExpectedSize;
843-
_ActualSize ->
844-
%% Wait and retry
845-
timer:sleep(100),
846-
wait_for_room_archive_size(Server, Username, RetryTimes-1, ExpectedSize)
817+
wait_for_room_archive_size(Server, Username, ExpectedSize) ->
818+
try mongoose_helper:wait_until(fun() -> room_archive_size(Server, Username) end,
819+
ExpectedSize,
820+
#{
821+
time_left => timer:seconds(20),
822+
name => room_archive_size
823+
}) of
824+
{ok, ExpectedSize} ->
825+
ExpectedSize
826+
catch
827+
_Error:Reason ->
828+
ct:pal("issue=wait_for_room_archive_size, expected_size=~p, log=~p",
829+
[ExpectedSize, Reason])
847830
end.
848831

849832

@@ -943,7 +926,7 @@ bootstrap_archive(Config) ->
943926
%% Wait for messages to be written
944927
wait_for_msgs(Msgs, Users) ->
945928
UsersCnt = [{S, U, count_msgs(Msgs, S, U)} || {S, U} <- Users],
946-
[wait_for_archive_size_or_warning(S, U, 20, C) || {S, U, C} <- UsersCnt],
929+
[wait_for_archive_size_or_warning(S, U, C) || {S, U, C} <- UsersCnt],
947930
ok.
948931

949932
count_msgs(Msgs, S, U) ->
@@ -1039,7 +1022,7 @@ muc_bootstrap_archive(Config) ->
10391022

10401023
maybe_wait_for_archive(Config),
10411024
?assert_equal(length(Msgs),
1042-
wait_for_room_archive_size(Domain, Room, 10, length(Msgs))),
1025+
wait_for_room_archive_size(Domain, Room, length(Msgs))),
10431026

10441027
[{pre_generated_muc_msgs, sort_msgs(Msgs)} | Config].
10451028

big_tests/tests/metrics_register_SUITE.erl

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
-import(metrics_helper, [assert_counter/2,
3131
get_counter_value/1]).
3232

33-
-import(mongoose_helper, [factor_backoff/3]).
3433

3534
%%--------------------------------------------------------------------
3635
%% Suite configuration
@@ -91,32 +90,29 @@ end_per_testcase(_CaseName, Config) ->
9190
%%--------------------------------------------------------------------
9291

9392
register(Config) ->
94-
{value, Registarations} = get_counter_value(modRegisterCount),
93+
{value, Registrations} = get_counter_value(modRegisterCount),
9594

9695
Alice = escalus_users:get_user_by_name(alice),
9796
escalus_users:create_user(Config, Alice),
98-
99-
factor_backoff(fun() -> assert_counter(Registarations + 1, modRegisterCount) end,
100-
{value, Registarations + 1},
101-
#{
102-
attempts => 10,
103-
min_time => 30,
104-
max_time => 150
105-
}
106-
).
107-
97+
wait_for_registrations(Registrations + 1).
10898

10999
unregister(Config) ->
110-
{value, Deregistarations} = get_counter_value(modUnregisterCount),
100+
{value, Deregistrations} = get_counter_value(modUnregisterCount),
111101

112102
Alice = escalus_users:get_user_by_name(alice),
113103
escalus_users:delete_user(Config, Alice),
104+
wait_for_deregistrations(Deregistrations + 1).
105+
106+
%%--------------------------------------------------------------------
107+
%% Helpers
108+
%%--------------------------------------------------------------------
109+
110+
wait_for_registrations(Count) ->
111+
mongoose_helper:wait_until(fun() -> assert_counter(Count, modRegisterCount) end,
112+
{value, Count},
113+
#{name => assert_counter}).
114114

115-
factor_backoff(fun() -> assert_counter(Deregistarations + 1, modUnregisterCount) end,
116-
{value, Deregistarations + 1},
117-
#{
118-
attempts => 10,
119-
min_time => 30,
120-
max_time => 150
121-
}
122-
).
115+
wait_for_deregistrations(Count) ->
116+
mongoose_helper:wait_until(fun() -> assert_counter(Count, modUnregisterCount) end,
117+
{value, Count},
118+
#{name => assert_counter}).

0 commit comments

Comments
 (0)