Skip to content

Commit

Permalink
[ct] fix rate limit test on (slow) GH runners
Browse files Browse the repository at this point in the history
  • Loading branch information
RoadRunnr committed May 13, 2024
1 parent 8017659 commit bffe076
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/ergw_aaa_diameter_srv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ inc_stats(no_tokens, #peer{stats = Stats} = Peer) ->
inc_stats(no_capacity, #peer{stats = Stats} = Peer) ->
Peer#peer{stats = inc_element(#peer_stats.no_capacity, Stats)}.

start_request_h(SvcName, {PeerRef, #diameter_caps{origin_host = {_, OH}} = Caps}, RPid, Peers0) ->
start_request_h(SvcName, {PeerRef, #diameter_caps{origin_host = {_, OH}} = Caps}, RPid, Peers0) ->
Peer = get_peer(OH, Peers0),
?LOG(debug, "start_request_h: ~p", [PeerRef]),
#peer{outstanding = Cnt,
Expand Down
39 changes: 30 additions & 9 deletions test/diameter_Gy_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -611,26 +611,47 @@ rate_limit(_Config) ->
%% make sure the token bucket is completely filled, previous test might have drained it
ct:sleep(2000),

ct:pal("Peers-#0: ~p", [ergw_aaa_diameter_srv:get_peers_info()]),
StartT = erlang:monotonic_time(),

%% with 4 peers, a rate limit at 10 req/s and 2 retries (= 3 attempts max) we should
%% be able to get 40 requests through
SRefs = [begin Ref = make_ref(), spawn(?MODULE, async_session, [Self, Ref]), Ref end
|| _ <- lists:seq(1, 60)],

CCRi = CollectFun('CCR-Initial', SRefs, #{}),
InitT = erlang:monotonic_time(),
ct:pal("Peers-#1: ~p", [ergw_aaa_diameter_srv:get_peers_info()]),
CCRt = CollectFun('CCR-Terminate', SRefs, #{}),
TermT = erlang:monotonic_time(),
ct:pal("Peers-#2: ~p", [ergw_aaa_diameter_srv:get_peers_info()]),

%% make sure to refill the token buckets, so that the following tests don't fail
ct:sleep(1100),

?match(#{ok := OkayI, {error,rate_limit} := LimitI}
when OkayI >= 40 andalso
LimitI /= 0 andalso
OkayI + LimitI =:= 60, CCRi),
?match(#{ok := OkayT, {error,rate_limit} := LimitT}
when OkayT >= 40 andalso
LimitT /= 0 andalso
OkayT + LimitT =:= 60, CCRt),
ok.
Ms = erlang:convert_time_unit(1, millisecond, native),
ct:pal("Test duration~nCCT-I: ~.4f ms ~nCCT-T: ~.4f ms",
[(InitT - StartT) / Ms, (TermT - InitT) / Ms]),

InitMaxT = erlang:convert_time_unit(200, millisecond, native),
TermMaxT = erlang:convert_time_unit(1300, millisecond, native),

if InitT - StartT > InitMaxT orelse
TermT - InitT > TermMaxT ->
%% test took too long
{skip, "Test took too long, runner is too slow"};
true ->
?match(#{ok := OkayI, {error,rate_limit} := LimitI}
when OkayI >= 40 andalso
LimitI /= 0 andalso
OkayI + LimitI =:= 60, CCRi),
?match(#{ok := OkayT, {error,rate_limit} := LimitT}
when OkayT >= 40 andalso
LimitT /= 0 andalso
OkayT + LimitT =:= 60, CCRt),
ok
end.

ccr_t_rate_limit() ->
[{doc, "older version of the rate limit test, does not work, keep for reference"}].
ccr_t_rate_limit(Config) ->
Expand Down
35 changes: 26 additions & 9 deletions test/diameter_Rf_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -726,26 +726,43 @@ rate_limit(_Config) ->
%% make sure the token bucket is completely filled, previous test might have drained it
ct:sleep(2000),

StartT = erlang:monotonic_time(),

%% with 1 peers, a rate limit at 20 req/s and 2 retries (= 3 attempts max) we should
%% be able to get 20 requests through
SRefs = [begin Ref = make_ref(), spawn(?MODULE, async_session, [Self, Ref]), Ref end
|| _ <- lists:seq(1, 60)],

CCRi = CollectFun('Initial', SRefs, #{}),
InitT = erlang:monotonic_time(),
CCRt = CollectFun('Terminate', SRefs, #{}),
TermT = erlang:monotonic_time(),

%% make sure to refill the token buckets, so that the following tests don't fail
ct:sleep(1100),

?match(#{ok := OkayI, {error,rate_limit} := LimitI}
when OkayI >= 20 andalso
LimitI /= 0 andalso
OkayI + LimitI =:= 60, CCRi),
?match(#{ok := OkayT, {error,rate_limit} := LimitT}
when OkayT >= 20 andalso
LimitT /= 0 andalso
OkayT + LimitT =:= 60, CCRt),
ok.
Ms = erlang:convert_time_unit(1, millisecond, native),
ct:pal("Test duration~nCCT-I: ~.4f ms ~nCCT-T: ~.4f ms",
[(InitT - StartT) / Ms, (TermT - InitT) / Ms]),

InitMaxT = erlang:convert_time_unit(200, millisecond, native),
TermMaxT = erlang:convert_time_unit(1300, millisecond, native),

if InitT - StartT > InitMaxT orelse
TermT - InitT > TermMaxT ->
%% test took too long
{skip, "Test took too long, runner is too slow"};
true ->
?match(#{ok := OkayI, {error,rate_limit} := LimitI}
when OkayI >= 20 andalso
LimitI /= 0 andalso
OkayI + LimitI =:= 60, CCRi),
?match(#{ok := OkayT, {error,rate_limit} := LimitT}
when OkayT >= 20 andalso
LimitT /= 0 andalso
OkayT + LimitT =:= 60, CCRt),
ok
end.

encode_error() ->
[{doc, "Check that a message encode error does not leave the "
Expand Down

0 comments on commit bffe076

Please sign in to comment.