forked from vulcand/oxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket_test.go
385 lines (299 loc) · 10.1 KB
/
bucket_test.go
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
374
375
376
377
378
379
380
381
382
383
384
385
package ratelimit
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vulcand/oxy/internal/holsterv4/clock"
"github.com/vulcand/oxy/testutils"
)
func TestConsumeSingleToken(t *testing.T) {
done := testutils.FreezeTime()
defer done()
tb := newTokenBucket(&rate{period: clock.Second, average: 1, burst: 1})
// First request passes
delay, err := tb.consume(1)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
// Next request does not pass the same second
delay, err = tb.consume(1)
require.NoError(t, err)
assert.Equal(t, clock.Second, delay)
// Second later, the request passes
clock.Advance(clock.Second)
delay, err = tb.consume(1)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
// Five seconds later, still only one request is allowed
// because maxBurst is 1
clock.Advance(5 * clock.Second)
delay, err = tb.consume(1)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
// The next one is forbidden
delay, err = tb.consume(1)
require.NoError(t, err)
assert.Equal(t, clock.Second, delay)
}
func TestFastConsumption(t *testing.T) {
done := testutils.FreezeTime()
defer done()
tb := newTokenBucket(&rate{period: clock.Second, average: 1, burst: 1})
// First request passes
delay, err := tb.consume(1)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
// Try 200 ms later
clock.Advance(clock.Millisecond * 200)
delay, err = tb.consume(1)
require.NoError(t, err)
assert.Equal(t, clock.Second, delay)
// Try 700 ms later
clock.Advance(clock.Millisecond * 700)
delay, err = tb.consume(1)
require.NoError(t, err)
assert.Equal(t, clock.Second, delay)
// Try 100 ms later, success!
clock.Advance(clock.Millisecond * 100)
delay, err = tb.consume(1)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
}
func TestConsumeMultipleTokens(t *testing.T) {
done := testutils.FreezeTime()
defer done()
tb := newTokenBucket(&rate{period: clock.Second, average: 3, burst: 5})
delay, err := tb.consume(3)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
delay, err = tb.consume(2)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
delay, err = tb.consume(1)
require.NoError(t, err)
assert.NotEqual(t, time.Duration(0), delay)
}
func TestDelayIsCorrect(t *testing.T) {
done := testutils.FreezeTime()
defer done()
tb := newTokenBucket(&rate{period: clock.Second, average: 3, burst: 5})
// Exhaust initial capacity
delay, err := tb.consume(5)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
delay, err = tb.consume(3)
require.NoError(t, err)
assert.NotEqual(t, time.Duration(0), delay)
// Now wait provided delay and make sure we can consume now
clock.Advance(delay)
delay, err = tb.consume(3)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
}
// Make sure requests that exceed burst size are not allowed.
func TestExceedsBurst(t *testing.T) {
done := testutils.FreezeTime()
defer done()
tb := newTokenBucket(&rate{period: clock.Second, average: 1, burst: 10})
_, err := tb.consume(11)
require.Error(t, err)
}
func TestConsumeBurst(t *testing.T) {
done := testutils.FreezeTime()
defer done()
tb := newTokenBucket(&rate{period: clock.Second, average: 2, burst: 5})
// In two seconds we would have 5 tokens
clock.Advance(2 * clock.Second)
// Lets consume 5 at once
delay, err := tb.consume(5)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
}
func TestConsumeEstimate(t *testing.T) {
done := testutils.FreezeTime()
defer done()
tb := newTokenBucket(&rate{period: clock.Second, average: 2, burst: 4})
// Consume all burst at once
delay, err := tb.consume(4)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
// Now try to consume it and face delay
delay, err = tb.consume(4)
require.NoError(t, err)
assert.Equal(t, time.Duration(2)*clock.Second, delay)
}
// If a rate with different period is passed to the `update` method, then an
// error is returned but the state of the bucket remains valid and unchanged.
func TestUpdateInvalidPeriod(t *testing.T) {
done := testutils.FreezeTime()
defer done()
// Given
tb := newTokenBucket(&rate{period: clock.Second, average: 10, burst: 20})
_, err := tb.consume(15) // 5 tokens available
require.NoError(t, err)
// When
err = tb.update(&rate{period: clock.Second + 1, average: 30, burst: 40}) // still 5 tokens available
require.Error(t, err)
// Then
// ...check that rate did not change
clock.Advance(500 * clock.Millisecond)
delay, err := tb.consume(11)
require.NoError(t, err)
assert.Equal(t, 100*clock.Millisecond, delay)
delay, err = tb.consume(10)
require.NoError(t, err)
// 0 available
assert.Equal(t, time.Duration(0), delay)
// ...check that burst did not change
clock.Advance(40 * clock.Second)
_, err = tb.consume(21)
require.Error(t, err)
delay, err = tb.consume(20)
require.NoError(t, err)
// 0 available
assert.Equal(t, time.Duration(0), delay)
}
// If the capacity of the bucket is increased by the update then it takes some
// time to fill the bucket with tokens up to the new capacity.
func TestUpdateBurstIncreased(t *testing.T) {
done := testutils.FreezeTime()
defer done()
// Given
tb := newTokenBucket(&rate{period: clock.Second, average: 10, burst: 20})
_, err := tb.consume(15) // 5 tokens available
require.NoError(t, err)
// When
err = tb.update(&rate{period: clock.Second, average: 10, burst: 50}) // still 5 tokens available
require.NoError(t, err)
// Then
delay, err := tb.consume(50)
require.NoError(t, err)
assert.Equal(t, clock.Second/10*45, delay)
}
// If the capacity of the bucket is increased by the update then it takes some
// time to fill the bucket with tokens up to the new capacity.
func TestUpdateBurstDecreased(t *testing.T) {
done := testutils.FreezeTime()
defer done()
// Given
tb := newTokenBucket(&rate{period: clock.Second, average: 10, burst: 50})
_, err := tb.consume(15) // 35 tokens available
require.NoError(t, err)
// When
err = tb.update(&rate{period: clock.Second, average: 10, burst: 20}) // the number of available tokens reduced to 20.
require.NoError(t, err)
// Then
delay, err := tb.consume(21)
require.Error(t, err)
assert.Equal(t, time.Duration(-1), delay)
}
// If rate is updated then it affects the bucket refill speed.
func TestUpdateRateChanged(t *testing.T) {
done := testutils.FreezeTime()
defer done()
// Given
tb := newTokenBucket(&rate{period: clock.Second, average: 10, burst: 20})
_, err := tb.consume(15) // 5 tokens available
require.NoError(t, err)
// When
err = tb.update(&rate{period: clock.Second, average: 20, burst: 20}) // still 5 tokens available
require.NoError(t, err)
// Then
delay, err := tb.consume(20)
require.NoError(t, err)
assert.Equal(t, clock.Second/20*15, delay)
}
// Only the most recent consumption is reverted by `Rollback`.
func TestRollback(t *testing.T) {
done := testutils.FreezeTime()
defer done()
// Given
tb := newTokenBucket(&rate{period: clock.Second, average: 10, burst: 20})
_, err := tb.consume(8) // 12 tokens available
require.NoError(t, err)
_, err = tb.consume(7) // 5 tokens available
require.NoError(t, err)
// When
tb.rollback() // 12 tokens available
// Then
delay, err := tb.consume(12)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
delay, err = tb.consume(1)
require.NoError(t, err)
assert.Equal(t, 100*clock.Millisecond, delay)
}
// It is safe to call `Rollback` several times. The second and all subsequent
// calls just do nothing.
func TestRollbackSeveralTimes(t *testing.T) {
done := testutils.FreezeTime()
defer done()
// Given
tb := newTokenBucket(&rate{period: clock.Second, average: 10, burst: 20})
_, err := tb.consume(8) // 12 tokens available
require.NoError(t, err)
tb.rollback() // 20 tokens available
// When
tb.rollback() // still 20 tokens available
tb.rollback() // still 20 tokens available
tb.rollback() // still 20 tokens available
// Then: all 20 tokens can be consumed
delay, err := tb.consume(20)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
delay, err = tb.consume(1)
require.NoError(t, err)
assert.Equal(t, 100*clock.Millisecond, delay)
}
// If previous consumption returned a delay due to an attempt to consume more
// tokens then there are available, then `Rollback` has no effect.
func TestRollbackAfterAvailableExceeded(t *testing.T) {
done := testutils.FreezeTime()
defer done()
// Given
tb := newTokenBucket(&rate{period: clock.Second, average: 10, burst: 20})
_, err := tb.consume(8) // 12 tokens available
require.NoError(t, err)
delay, err := tb.consume(15) // still 12 tokens available
require.NoError(t, err)
assert.Equal(t, 300*clock.Millisecond, delay)
// When
tb.rollback() // Previous operation consumed 0 tokens, so rollback has no effect.
// Then
delay, err = tb.consume(12)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
delay, err = tb.consume(1)
require.NoError(t, err)
assert.Equal(t, 100*clock.Millisecond, delay)
}
// If previous consumption returned a error due to an attempt to consume more
// tokens then the bucket's burst size, then `Rollback` has no effect.
func TestRollbackAfterError(t *testing.T) {
done := testutils.FreezeTime()
defer done()
// Given
tb := newTokenBucket(&rate{period: clock.Second, average: 10, burst: 20})
_, err := tb.consume(8) // 12 tokens available
require.NoError(t, err)
delay, err := tb.consume(21) // still 12 tokens available
require.Error(t, err)
assert.Equal(t, time.Duration(-1), delay)
// When
tb.rollback() // Previous operation consumed 0 tokens, so rollback has no effect.
// Then
delay, err = tb.consume(12)
require.NoError(t, err)
assert.Equal(t, time.Duration(0), delay)
delay, err = tb.consume(1)
require.NoError(t, err)
assert.Equal(t, 100*clock.Millisecond, delay)
}
func TestDivisionByZeroOnPeriod(t *testing.T) {
var emptyPeriod int64
tb := newTokenBucket(&rate{period: time.Duration(emptyPeriod), average: 2, burst: 2})
_, err := tb.consume(1)
assert.NoError(t, err)
err = tb.update(&rate{period: clock.Nanosecond, average: 1, burst: 1})
assert.NoError(t, err)
}