@@ -17,19 +17,9 @@ defmodule BorutaWeb.Plugs.RateLimitTest do
17
17
test "request is throttled" , % { conn: conn } do
18
18
:timer . sleep ( 1000 )
19
19
b_conn = % { conn | remote_ip: { 127 , 0 , 0 , 2 } }
20
- options = [ time_unit: :second , count: 5 ]
20
+ options = [ time_unit: :second , count: 5 , penality: 500 ]
21
21
assert RateLimit . call ( conn , options ) == conn
22
22
assert RateLimit . call ( b_conn , options ) == b_conn
23
- assert RateLimit . call ( conn , options ) == conn
24
- assert RateLimit . call ( b_conn , options ) == b_conn
25
- assert RateLimit . call ( conn , options ) == conn
26
- assert RateLimit . call ( b_conn , options ) == b_conn
27
- assert RateLimit . call ( conn , options ) == conn
28
- assert RateLimit . call ( b_conn , options ) == b_conn
29
- assert RateLimit . call ( conn , options ) == conn
30
- assert RateLimit . call ( b_conn , options ) == b_conn
31
- assert RateLimit . call ( conn , options ) . status == 429
32
- assert RateLimit . call ( b_conn , options ) . status == 429
33
23
end
34
24
end
35
25
@@ -57,32 +47,88 @@ defmodule BorutaWeb.Plugs.RateLimitTest do
57
47
end
58
48
end
59
49
50
+ describe "Counter.throttling_timeout" do
51
+ test "gives the timeout within the time unit range" do
52
+ :timer . sleep ( 1000 )
53
+ ip = :ip
54
+ time_unit = :second
55
+ penality = 100
56
+ count = 1
57
+
58
+ Agent . update ( RateLimit.Counter , fn _counter -> % { } end )
59
+ assert RateLimit.Counter . throttling_timeout ( ip , count , time_unit , penality ) == 0
60
+
61
+ Agent . update ( RateLimit.Counter , fn _counter -> % { ip => [ :os . system_time ( :millisecond ) ] } end )
62
+ assert RateLimit.Counter . throttling_timeout ( ip , count , time_unit , penality ) == 0
63
+
64
+ Agent . update ( RateLimit.Counter , fn _counter ->
65
+ % {
66
+ ip => [
67
+ :os . system_time ( :millisecond ) ,
68
+ :os . system_time ( :millisecond ) ,
69
+ :os . system_time ( :millisecond ) ,
70
+ :os . system_time ( :millisecond ) ,
71
+ :os . system_time ( :millisecond ) ,
72
+ :os . system_time ( :millisecond ) ,
73
+ :os . system_time ( :millisecond )
74
+ ]
75
+ }
76
+ end )
77
+
78
+ assert RateLimit.Counter . throttling_timeout ( ip , count , time_unit , penality ) == 700
79
+
80
+ Agent . update ( RateLimit.Counter , fn _counter ->
81
+ % {
82
+ ip => [
83
+ :os . system_time ( :millisecond ) - 1000 ,
84
+ :os . system_time ( :millisecond ) - 800 ,
85
+ :os . system_time ( :millisecond )
86
+ ]
87
+ }
88
+ end )
89
+
90
+ assert RateLimit.Counter . throttling_timeout ( ip , count , time_unit , penality ) == 200
91
+
92
+ Agent . update ( RateLimit.Counter , fn _counter ->
93
+ % { ip => [ :os . system_time ( :millisecond ) , :os . system_time ( :millisecond ) - 1000 ] }
94
+ end )
95
+
96
+ assert RateLimit.Counter . throttling_timeout ( ip , count , time_unit , penality ) == 0
97
+ end
98
+ end
99
+
60
100
describe "Counter.increment" do
61
101
test "updates the counter" do
62
102
:timer . sleep ( 1000 )
63
103
ip = :ip
64
104
time_unit = :second
65
105
66
106
assert Agent . get ( RateLimit.Counter , fn counter ->
67
- Map . get ( counter , ip , [ ] ) |> Enum . count ( )
107
+ Map . get ( counter , ip , [ ] )
108
+ |> Enum . count ( fn timestamp -> timestamp > :os . system_time ( :millisecond ) - 1000 end )
68
109
end ) == 0
69
110
70
111
RateLimit.Counter . increment ( ip , time_unit )
71
112
72
113
assert Agent . get ( RateLimit.Counter , fn % { ^ ip => timestamps } ->
73
- Enum . count ( timestamps )
114
+ timestamps
115
+ |> Enum . count ( fn timestamp -> timestamp > :os . system_time ( :millisecond ) - 1000 end )
74
116
end ) == 1
75
117
76
118
RateLimit.Counter . increment ( ip , time_unit )
77
119
78
120
assert Agent . get ( RateLimit.Counter , fn % { ^ ip => timestamps } ->
79
- Enum . count ( timestamps )
121
+ timestamps
122
+ |> Enum . count ( fn timestamp -> timestamp > :os . system_time ( :millisecond ) - 1000 end )
80
123
end ) == 2
81
124
82
125
:timer . sleep ( 1000 )
83
126
RateLimit.Counter . increment ( ip , time_unit )
84
127
85
- assert Agent . get ( RateLimit.Counter , fn % { ^ ip => timestamps } -> Enum . count ( timestamps ) end ) ==
128
+ assert Agent . get ( RateLimit.Counter , fn % { ^ ip => timestamps } ->
129
+ timestamps
130
+ |> Enum . count ( fn timestamp -> timestamp > :os . system_time ( :millisecond ) - 1000 end )
131
+ end ) ==
86
132
1
87
133
end
88
134
end
0 commit comments