-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlimiter_test.go
More file actions
373 lines (296 loc) · 7.29 KB
/
Copy pathlimiter_test.go
File metadata and controls
373 lines (296 loc) · 7.29 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package ratelimit
import (
"testing"
"time"
)
func TestNewLimiter(t *testing.T) {
const limit = 5 * time.Second
const burst = 4
l := NewLimiter(limit, burst)
if l == nil {
t.Fatal("NewLimiter() should not return nil")
}
if l.limit != limit {
t.Errorf("limit should be %v but was %v", limit, l.limit)
}
if l.burst != burst {
t.Errorf("burst should be %d but was %d", burst, l.burst)
}
if l.tokens != burst {
t.Errorf("tokens should be %d but was %d", burst, l.tokens)
}
}
func TestReserveN(t *testing.T) {
const limit = 100 * time.Millisecond
const burst = 3
ts := &testTimeSource{}
l := NewLimiterWithTimeSource(ts.Now, limit, burst)
ok, res := l.ReserveN(0)
if !ok || (res != Reservation{}) {
t.Errorf(
"ReserveN(0) should return (true, nil) but returned (%t, %+v)",
ok, res)
}
ok, res = l.ReserveN(2)
if !ok {
t.Error("returned false but should return true")
}
if res.Burst != burst {
t.Errorf("res.Burst should be %d but was %d",
burst, res.Burst)
}
if res.Remaining != 1 {
t.Errorf("res.Remaining should be %d but was %d",
1, res.Remaining)
}
ts.Advance(3 * limit)
if l.Tokens() != burst {
t.Errorf("recovered amount of tokens should be %d but was %d",
burst, l.Tokens())
}
// -----------------------------------------
ts = &testTimeSource{}
l = NewLimiterWithTimeSource(ts.Now, limit, burst)
for i := 0; i < 3; i++ {
ok, _ = l.ReserveN(1)
if !ok {
t.Fatalf("Reservation was not successful")
}
}
ok, _ = l.ReserveN(1)
if ok {
t.Fatalf("Reservation was successful even though it should not")
}
ts.Advance(limit)
ok, _ = l.ReserveN(1)
if !ok {
t.Fatalf("Reservation was not successful")
}
ok, _ = l.ReserveN(1)
if ok {
t.Fatalf("Reservation was successful even though it should not")
}
ts.Advance(2 * limit)
ok, _ = l.ReserveN(3)
if ok {
t.Fatalf("Reservation was successful even though it should not")
}
ok, _ = l.ReserveN(2)
if !ok {
t.Fatalf("Reservation was not successful")
}
ok, _ = l.ReserveN(1)
if ok {
t.Fatalf("Reservation was successful even though it should not")
}
}
func TestReserve(t *testing.T) {
const limit = 100 * time.Millisecond
const burst = 3
ts := &testTimeSource{}
l := NewLimiterWithTimeSource(ts.Now, limit, burst)
ok, res := l.Reserve()
if !ok {
t.Error("returned false but should return true")
}
if res.Burst != burst {
t.Errorf("res.Burst should be %d but was %d",
burst, res.Burst)
}
if res.Remaining != 2 {
t.Errorf("res.Remaining should be %d but was %d",
2, res.Remaining)
}
ts.Advance(3 * limit)
if l.Tokens() != burst {
t.Errorf("recovered amount of tokens should be %d but was %d",
burst, l.Tokens())
}
// -----------------------------------------
ts = &testTimeSource{}
l = NewLimiterWithTimeSource(ts.Now, limit, burst)
for i := 0; i < 3; i++ {
ok, _ = l.Reserve()
if !ok {
t.Fatalf("Reservation was not successful")
}
}
ok, _ = l.Reserve()
if ok {
t.Fatalf("Reservation was successful even though it should not")
}
ts.Advance(limit)
ok, _ = l.Reserve()
if !ok {
t.Fatalf("Reservation was not successful")
}
ok, _ = l.Reserve()
if ok {
t.Fatalf("Reservation was successful even though it should not")
}
}
func TestAllowN(t *testing.T) {
const limit = 100 * time.Millisecond
const burst = 3
ts := &testTimeSource{}
l := NewLimiterWithTimeSource(ts.Now, limit, burst)
ok := l.AllowN(1)
if !ok {
t.Error("returned false but should return true")
}
ts.Advance(3 * limit)
if l.Tokens() != burst {
t.Errorf("recovered amount of tokens should be %d but was %d",
burst, l.Tokens())
}
// -----------------------------------------
ts = &testTimeSource{}
l = NewLimiterWithTimeSource(ts.Now, limit, burst)
for i := 0; i < 3; i++ {
ok = l.AllowN(1)
if !ok {
t.Fatalf("Reservation was not successful")
}
}
// 0 tokens remaining
ok = l.AllowN(1)
if ok {
t.Fatalf("Reservation was successful even though it should not")
}
ts.Advance(limit)
// 1 tokens remaining
ok = l.AllowN(1)
if !ok {
t.Fatalf("Reservation was not successful")
}
// 0 tokens remaining
ok = l.AllowN(1)
if ok {
t.Fatalf("Reservation was successful even though it should not")
}
ts.Advance(2 * limit)
// 2 tokens remaining
ok = l.AllowN(3)
if ok {
t.Fatalf("Reservation was successful even though it should not")
}
// 2 tokens remaining
ok = l.AllowN(2)
if !ok {
t.Fatalf("Reservation was not successful")
}
// 0 tokens remaining
ok = l.AllowN(1)
if ok {
t.Fatalf("Reservation was successful even though it should not")
}
}
func TestAllow(t *testing.T) {
const limit = 100 * time.Millisecond
const burst = 3
ts := &testTimeSource{}
l := NewLimiterWithTimeSource(ts.Now, limit, burst)
ok := l.Allow()
if !ok {
t.Error("returned false but should return true")
}
ts.Advance(3 * limit)
if l.Tokens() != burst {
t.Errorf("recovered amount of tokens should be %d but was %d",
burst, l.Tokens())
}
// -----------------------------------------
ts = &testTimeSource{}
l = NewLimiterWithTimeSource(ts.Now, limit, burst)
for i := 0; i < 3; i++ {
ok = l.Allow()
if !ok {
t.Fatalf("Reservation was not successful")
}
}
ok = l.Allow()
if ok {
t.Fatalf("Reservation was successful even though it should not")
}
ts.Advance(limit)
ok = l.Allow()
if !ok {
t.Fatalf("Reservation was not successful")
}
ok = l.Allow()
if ok {
t.Fatalf("Reservation was successful even though it should not")
}
}
func TestLimit(t *testing.T) {
const limit = 100 * time.Millisecond
const burst = 3
l := NewLimiter(limit, burst)
if m := l.Limit(); m != limit {
t.Errorf("l.Burst() should be %s but was %s", limit, m)
}
}
func TestBurst(t *testing.T) {
const limit = 100 * time.Millisecond
const burst = 3
l := NewLimiter(limit, burst)
if b := l.Burst(); b != burst {
t.Errorf("l.Burst() should be %d but was %d", burst, b)
}
}
func TestTokens(t *testing.T) {
const limit = 100 * time.Millisecond
const burst = 3
l := NewLimiter(limit, burst)
if tg := l.Tokens(); tg != burst {
t.Errorf("l.Tokens() should be %d but was %d", burst, tg)
}
l.Reserve()
if tg := l.Tokens(); tg != burst-1 {
t.Errorf("l.Tokens() should be %d but was %d", burst-1, tg)
}
}
func TestReserve_zero(t *testing.T) {
l := NewLimiter(0, 10)
ok, res := l.ReserveN(1)
if ok || (res != Reservation{}) {
t.Error("ReserveN should return false when l is 0")
}
l = NewLimiter(100*time.Millisecond, 0)
ok, res = l.ReserveN(1)
if ok || (res != Reservation{}) {
t.Error("ReserveN should return false when b is 0")
}
l = NewLimiter(-1, 10)
ok, res = l.ReserveN(1)
if ok || (res != Reservation{}) {
t.Error("ReserveN should return false when b is -1")
}
l = NewLimiter(100*time.Millisecond, -1)
ok, res = l.ReserveN(1)
if ok || (res != Reservation{}) {
t.Error("ReserveN should return false when b is -1")
}
}
// Test for issue #1
// https://github.com/zekroTJA/ratelimit/issues/1
func TestReserve_reset(t *testing.T) {
const limit = 100 * time.Millisecond
const burst = 1
ts := &testTimeSource{}
l := NewLimiterWithTimeSource(ts.Now, limit, burst)
ok, _ := l.Reserve()
if !ok {
t.Fatal("First Reserve should return true")
}
ts.Advance(limit / 2)
ok, _ = l.Reserve()
if ok {
t.Fatal("Second Reserve should return false")
}
ts.Advance(limit / 2)
ok, _ = l.Reserve()
if !ok {
t.Fatal("Third Reserve should return true")
}
}