1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 func TestLimit(t *testing.T) {
21 t.Errorf("Limit(10) == Inf should be false")
25 func closeEnough(a, b Limit) bool {
26 return (math.Abs(float64(a)/float64(b)) - 1.0) < 1e-9
29 func TestEvery(t *testing.T) {
31 interval time.Duration
36 {1 * time.Nanosecond, Limit(1e9)},
37 {1 * time.Microsecond, Limit(1e6)},
38 {1 * time.Millisecond, Limit(1e3)},
39 {10 * time.Millisecond, Limit(100)},
40 {100 * time.Millisecond, Limit(10)},
41 {1 * time.Second, Limit(1)},
42 {2 * time.Second, Limit(0.5)},
43 {time.Duration(2.5 * float64(time.Second)), Limit(0.4)},
44 {4 * time.Second, Limit(0.25)},
45 {10 * time.Second, Limit(0.1)},
46 {time.Duration(math.MaxInt64), Limit(1e9 / float64(math.MaxInt64))},
48 for _, tc := range cases {
49 lim := Every(tc.interval)
50 if !closeEnough(lim, tc.lim) {
51 t.Errorf("Every(%v) = %v want %v", tc.interval, lim, tc.lim)
57 d = 100 * time.Millisecond
62 t1 = t0.Add(time.Duration(1) * d)
63 t2 = t0.Add(time.Duration(2) * d)
64 t3 = t0.Add(time.Duration(3) * d)
65 t4 = t0.Add(time.Duration(4) * d)
66 t5 = t0.Add(time.Duration(5) * d)
67 t9 = t0.Add(time.Duration(9) * d)
76 func run(t *testing.T, lim *Limiter, allows []allow) {
77 for i, allow := range allows {
78 ok := lim.AllowN(allow.t, allow.n)
80 t.Errorf("step %d: lim.AllowN(%v, %v) = %v want %v",
81 i, allow.t, allow.n, ok, allow.ok)
86 func TestLimiterBurst1(t *testing.T) {
87 run(t, NewLimiter(10, 1), []allow{
94 {t2, 2, false}, // burst size is 1, so n=2 always fails
100 func TestLimiterBurst3(t *testing.T) {
101 run(t, NewLimiter(10, 3), []allow{
118 func TestLimiterJumpBackwards(t *testing.T) {
119 run(t, NewLimiter(10, 3), []allow{
120 {t1, 1, true}, // start at t1
121 {t0, 1, true}, // jump back to t0, two tokens remain
125 {t1, 1, true}, // got a token
128 {t2, 1, true}, // got another token
134 func TestSimultaneousRequests(t *testing.T) {
145 // Very slow replenishing bucket.
146 lim := NewLimiter(limit, burst)
148 // Tries to take a token, atomically updates the counter and decreases the wait
152 if ok := lim.Allow(); ok {
153 atomic.AddUint32(&numOK, 1)
158 for i := 0; i < numRequests; i++ {
163 t.Errorf("numOK = %d, want %d", numOK, burst)
167 func TestLongRunningQPS(t *testing.T) {
169 t.Skip("skipping in short mode")
171 if runtime.GOOS == "openbsd" {
172 t.Skip("low resolution time.Sleep invalidates test (golang.org/issue/14183)")
176 // The test runs for a few seconds executing many requests and then checks
177 // that overall number of requests is reasonable.
184 lim := NewLimiter(limit, burst)
186 var wg sync.WaitGroup
188 if ok := lim.Allow(); ok {
189 atomic.AddInt32(&numOK, 1)
195 end := start.Add(5 * time.Second)
196 for time.Now().Before(end) {
200 // This will still offer ~500 requests per second, but won't consume
201 // outrageous amount of CPU.
202 time.Sleep(2 * time.Millisecond)
205 elapsed := time.Since(start)
206 ideal := burst + (limit * float64(elapsed) / float64(time.Second))
208 // We should never get more requests than allowed.
209 if want := int32(ideal + 1); numOK > want {
210 t.Errorf("numOK = %d, want %d (ideal %f)", numOK, want, ideal)
212 // We should get very close to the number of requests allowed.
213 if want := int32(0.999 * ideal); numOK < want {
214 t.Errorf("numOK = %d, want %d (ideal %f)", numOK, want, ideal)
218 type request struct {
225 // dFromDuration converts a duration to a multiple of the global constant d
226 func dFromDuration(dur time.Duration) int {
227 // Adding a millisecond to be swallowed by the integer division
228 // because we don't care about small inaccuracies
229 return int((dur + time.Millisecond) / d)
232 // dSince returns multiples of d since t0
233 func dSince(t time.Time) int {
234 return dFromDuration(t.Sub(t0))
237 func runReserve(t *testing.T, lim *Limiter, req request) *Reservation {
238 return runReserveMax(t, lim, req, InfDuration)
241 func runReserveMax(t *testing.T, lim *Limiter, req request, maxReserve time.Duration) *Reservation {
242 r := lim.reserveN(req.t, req.n, maxReserve)
243 if r.ok && (dSince(r.timeToAct) != dSince(req.act)) || r.ok != req.ok {
244 t.Errorf("lim.reserveN(t%d, %v, %v) = (t%d, %v) want (t%d, %v)",
245 dSince(req.t), req.n, maxReserve, dSince(r.timeToAct), r.ok, dSince(req.act), req.ok)
250 func TestSimpleReserve(t *testing.T) {
251 lim := NewLimiter(10, 2)
253 runReserve(t, lim, request{t0, 2, t0, true})
254 runReserve(t, lim, request{t0, 2, t2, true})
255 runReserve(t, lim, request{t3, 2, t4, true})
258 func TestMix(t *testing.T) {
259 lim := NewLimiter(10, 2)
261 runReserve(t, lim, request{t0, 3, t1, false}) // should return false because n > Burst
262 runReserve(t, lim, request{t0, 2, t0, true})
263 run(t, lim, []allow{{t1, 2, false}}) // not enought tokens - don't allow
264 runReserve(t, lim, request{t1, 2, t2, true})
265 run(t, lim, []allow{{t1, 1, false}}) // negative tokens - don't allow
266 run(t, lim, []allow{{t3, 1, true}})
269 func TestCancelInvalid(t *testing.T) {
270 lim := NewLimiter(10, 2)
272 runReserve(t, lim, request{t0, 2, t0, true})
273 r := runReserve(t, lim, request{t0, 3, t3, false})
274 r.CancelAt(t0) // should have no effect
275 runReserve(t, lim, request{t0, 2, t2, true}) // did not get extra tokens
278 func TestCancelLast(t *testing.T) {
279 lim := NewLimiter(10, 2)
281 runReserve(t, lim, request{t0, 2, t0, true})
282 r := runReserve(t, lim, request{t0, 2, t2, true})
283 r.CancelAt(t1) // got 2 tokens back
284 runReserve(t, lim, request{t1, 2, t2, true})
287 func TestCancelTooLate(t *testing.T) {
288 lim := NewLimiter(10, 2)
290 runReserve(t, lim, request{t0, 2, t0, true})
291 r := runReserve(t, lim, request{t0, 2, t2, true})
292 r.CancelAt(t3) // too late to cancel - should have no effect
293 runReserve(t, lim, request{t3, 2, t4, true})
296 func TestCancel0Tokens(t *testing.T) {
297 lim := NewLimiter(10, 2)
299 runReserve(t, lim, request{t0, 2, t0, true})
300 r := runReserve(t, lim, request{t0, 1, t1, true})
301 runReserve(t, lim, request{t0, 1, t2, true})
302 r.CancelAt(t0) // got 0 tokens back
303 runReserve(t, lim, request{t0, 1, t3, true})
306 func TestCancel1Token(t *testing.T) {
307 lim := NewLimiter(10, 2)
309 runReserve(t, lim, request{t0, 2, t0, true})
310 r := runReserve(t, lim, request{t0, 2, t2, true})
311 runReserve(t, lim, request{t0, 1, t3, true})
312 r.CancelAt(t2) // got 1 token back
313 runReserve(t, lim, request{t2, 2, t4, true})
316 func TestCancelMulti(t *testing.T) {
317 lim := NewLimiter(10, 4)
319 runReserve(t, lim, request{t0, 4, t0, true})
320 rA := runReserve(t, lim, request{t0, 3, t3, true})
321 runReserve(t, lim, request{t0, 1, t4, true})
322 rC := runReserve(t, lim, request{t0, 1, t5, true})
323 rC.CancelAt(t1) // get 1 token back
324 rA.CancelAt(t1) // get 2 tokens back, as if C was never reserved
325 runReserve(t, lim, request{t1, 3, t5, true})
328 func TestReserveJumpBack(t *testing.T) {
329 lim := NewLimiter(10, 2)
331 runReserve(t, lim, request{t1, 2, t1, true}) // start at t1
332 runReserve(t, lim, request{t0, 1, t1, true}) // should violate Limit,Burst
333 runReserve(t, lim, request{t2, 2, t3, true})
336 func TestReserveJumpBackCancel(t *testing.T) {
337 lim := NewLimiter(10, 2)
339 runReserve(t, lim, request{t1, 2, t1, true}) // start at t1
340 r := runReserve(t, lim, request{t1, 2, t3, true})
341 runReserve(t, lim, request{t1, 1, t4, true})
342 r.CancelAt(t0) // cancel at t0, get 1 token back
343 runReserve(t, lim, request{t1, 2, t4, true}) // should violate Limit,Burst
346 func TestReserveSetLimit(t *testing.T) {
347 lim := NewLimiter(5, 2)
349 runReserve(t, lim, request{t0, 2, t0, true})
350 runReserve(t, lim, request{t0, 2, t4, true})
351 lim.SetLimitAt(t2, 10)
352 runReserve(t, lim, request{t2, 1, t4, true}) // violates Limit and Burst
355 func TestReserveSetLimitCancel(t *testing.T) {
356 lim := NewLimiter(5, 2)
358 runReserve(t, lim, request{t0, 2, t0, true})
359 r := runReserve(t, lim, request{t0, 2, t4, true})
360 lim.SetLimitAt(t2, 10)
361 r.CancelAt(t2) // 2 tokens back
362 runReserve(t, lim, request{t2, 2, t3, true})
365 func TestReserveMax(t *testing.T) {
366 lim := NewLimiter(10, 2)
369 runReserveMax(t, lim, request{t0, 2, t0, true}, maxT)
370 runReserveMax(t, lim, request{t0, 1, t1, true}, maxT) // reserve for close future
371 runReserveMax(t, lim, request{t0, 1, t2, false}, maxT) // time to act too far in the future
378 delay int // in multiples of d
382 func runWait(t *testing.T, lim *Limiter, w wait) {
384 err := lim.WaitN(w.ctx, w.n)
385 delay := time.Now().Sub(start)
386 if (w.nilErr && err != nil) || (!w.nilErr && err == nil) || w.delay != dFromDuration(delay) {
389 errString = "<non-nil error>"
391 t.Errorf("lim.WaitN(%v, lim, %v) = %v with delay %v ; want %v with delay %v",
392 w.name, w.n, err, delay, errString, d*time.Duration(w.delay))
396 func TestWaitSimple(t *testing.T) {
397 lim := NewLimiter(10, 3)
399 ctx, cancel := context.WithCancel(context.Background())
401 runWait(t, lim, wait{"already-cancelled", ctx, 1, 0, false})
403 runWait(t, lim, wait{"exceed-burst-error", context.Background(), 4, 0, false})
405 runWait(t, lim, wait{"act-now", context.Background(), 2, 0, true})
406 runWait(t, lim, wait{"act-later", context.Background(), 3, 2, true})
409 func TestWaitCancel(t *testing.T) {
410 lim := NewLimiter(10, 3)
412 ctx, cancel := context.WithCancel(context.Background())
413 runWait(t, lim, wait{"act-now", ctx, 2, 0, true}) // after this lim.tokens = 1
418 runWait(t, lim, wait{"will-cancel", ctx, 3, 1, false})
419 // should get 3 tokens back, and have lim.tokens = 2
420 t.Logf("tokens:%v last:%v lastEvent:%v", lim.tokens, lim.last, lim.lastEvent)
421 runWait(t, lim, wait{"act-now-after-cancel", context.Background(), 2, 0, true})
424 func TestWaitTimeout(t *testing.T) {
425 lim := NewLimiter(10, 3)
427 ctx, cancel := context.WithTimeout(context.Background(), d)
429 runWait(t, lim, wait{"act-now", ctx, 2, 0, true})
430 runWait(t, lim, wait{"w-timeout-err", ctx, 3, 0, false})
433 func TestWaitInf(t *testing.T) {
434 lim := NewLimiter(Inf, 0)
436 runWait(t, lim, wait{"exceed-burst-no-error", context.Background(), 3, 0, true})
439 func BenchmarkAllowN(b *testing.B) {
440 lim := NewLimiter(Every(1*time.Second), 1)
444 b.RunParallel(func(pb *testing.PB) {