-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimiter_test.go
89 lines (76 loc) · 2.1 KB
/
limiter_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
package limiter
import (
"testing"
"time"
"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
)
var pool *redis.Pool
func newPool(addr string, password string) *redis.Pool {
return &redis.Pool{
Wait: true,
MaxIdle: 3,
MaxActive: 10000,
IdleTimeout: 240 * time.Second,
// Dial or DialContext must be set. When both are set, DialContext takes precedence over Dial.
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", addr,
redis.DialConnectTimeout(5*time.Second),
redis.DialReadTimeout(5*time.Second),
redis.DialWriteTimeout(5*time.Second))
if err != nil {
return nil, err
}
if len(password) != 0 {
if _, err := c.Do("AUTH", password); err != nil {
c.Close()
return nil, err
}
}
if _, err := c.Do("SELECT", 0); err != nil {
c.Close()
return nil, err
}
return c, nil
},
}
}
func rateLimiter() *Limiter {
pool = newPool("127.0.0.1:6379", "")
return NewLimiter(pool)
}
func TestAllow(t *testing.T) {
var l = rateLimiter()
limit := PerSecond(10)
res, err := l.Allow("test_id", limit)
assert.Nil(t, err)
assert.True(t, res.Allowed)
assert.Equal(t, res.Remaining, 9)
assert.Equal(t, res.RetryAfter, time.Duration(-1))
assert.InDelta(t, res.ResetAfter, 100*time.Millisecond, float64(10*time.Millisecond))
res, err = l.AllowN("test_id", limit, 2)
assert.Nil(t, err)
assert.True(t, res.Allowed)
assert.Equal(t, res.Remaining, 7)
assert.Equal(t, res.RetryAfter, time.Duration(-1))
assert.InDelta(t, res.ResetAfter, 300*time.Millisecond, float64(50*time.Millisecond))
res, err = l.AllowN("test_id", limit, 1000)
assert.Nil(t, err)
assert.False(t, res.Allowed)
assert.Equal(t, res.Remaining, 0)
assert.InDelta(t, res.RetryAfter, 99*time.Second, float64(time.Second))
assert.InDelta(t, res.ResetAfter, 300*time.Millisecond, float64(50*time.Millisecond))
}
func BenchmarkAllow(b *testing.B) {
var l = rateLimiter()
limit := PerSecond(100)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := l.Allow("foo", limit)
if err != nil {
b.Fatal(err)
}
}
})
}