forked from dgiot/dgiot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemqx_limiter_SUITE.erl
78 lines (60 loc) · 3.15 KB
/
emqx_limiter_SUITE.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
%%--------------------------------------------------------------------
%% Copyright (c) 2019-2021 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(emqx_limiter_SUITE).
-compile(export_all).
-compile(nowarn_export_all).
-include_lib("eunit/include/eunit.hrl").
%%--------------------------------------------------------------------
%% Setups
%%--------------------------------------------------------------------
all() -> emqx_ct:all(?MODULE).
init_per_testcase(_, Cfg) ->
_ = esockd_limiter:start_link(),
Cfg.
end_per_testcase(_, _) ->
esockd_limiter:stop().
%%--------------------------------------------------------------------
%% Cases
%%--------------------------------------------------------------------
t_init(_) ->
Cap1 = 1000, Intv1 = 10,
Cap2 = 2000, Intv2 = 15,
undefined = emqx_limiter:init(external, undefined, undefined, []),
#{conn_bytes_in := #{capacity := Cap2, interval := Intv2, tokens := Cap2},
conn_messages_in := #{capacity := Cap1, interval := Intv1, tokens := Cap1 }} =
emqx_limiter:info(
emqx_limiter:init(external, {Cap1, Intv1}, {Cap2, Intv2}, [])),
#{conn_bytes_in := #{capacity := Cap2, interval := Intv2, tokens := Cap2 }} =
emqx_limiter:info(
emqx_limiter:init(external, undefined, {Cap1, Intv1}, [{conn_bytes_in, {Cap2, Intv2}}])).
t_check_conn(_) ->
Limiter = emqx_limiter:init(external, [{conn_bytes_in, {100, 1}}]),
{ok, Limiter2} = emqx_limiter:check(#{cnt => 0, oct => 1}, Limiter),
#{conn_bytes_in := #{tokens := 99}} = emqx_limiter:info(Limiter2),
{pause, 10, Limiter3} = emqx_limiter:check(#{cnt => 0, oct => 100}, Limiter),
#{conn_bytes_in := #{tokens := 0}} = emqx_limiter:info(Limiter3),
{pause, 100000, Limiter4} = emqx_limiter:check(#{cnt => 0, oct => 10000}, Limiter3),
#{conn_bytes_in := #{tokens := 0}} = emqx_limiter:info(Limiter4).
t_check_overall(_) ->
Limiter = emqx_limiter:init(external, [{overall_messages_routing, {100, 1}}]),
{ok, Limiter2} = emqx_limiter:check(#{cnt => 1, oct => 0}, Limiter),
#{overall_messages_routing := #{tokens := 99}} = emqx_limiter:info(Limiter2),
%% XXX: P = 1/r = 1/100 * 1000 = 10ms ?
{pause, _, Limiter3} = emqx_limiter:check(#{cnt => 100, oct => 0}, Limiter),
#{overall_messages_routing := #{tokens := 0}} = emqx_limiter:info(Limiter2),
%% XXX: P = 10000/r = 10000/100 * 1000 = 100s ?
{pause, _, Limiter4} = emqx_limiter:check(#{cnt => 10000, oct => 0}, Limiter3),
#{overall_messages_routing := #{tokens := 0}} = emqx_limiter:info(Limiter4).