This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
/
ratelimite_test.go
121 lines (106 loc) · 2.62 KB
/
ratelimite_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
package ratelimiter_test
import (
"testing"
"time"
"github.com/garyburd/redigo/redis"
limiter "github.com/lysu/go-rate-limiter"
"github.com/stretchr/testify/assert"
)
var redisPool = &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
func clearUpRedis() {
conn := redisPool.Get()
defer conn.Close()
conn.Do("DEL", "rl-key1")
conn.Do("DEL", "rl-key2")
}
func size(key string) int {
conn := redisPool.Get()
defer conn.Close()
r, err := conn.Do("ZCARD", key)
if err != nil {
panic("fetch size failure")
}
ri, _ := redis.Int(r, err)
return ri
}
func assertRateLimiter(r func(key string) bool, t *testing.T) {
assert := assert.New(t)
key1 := "key1"
allowCount := 0
for i := 0; i < 100; i++ {
if r(key1) {
allowCount++
}
}
assert.Equal(10, allowCount, "key1 1st round not limit")
key2 := "key2"
allowCount = 0
for i := 0; i < 100; i++ {
if r(key2) {
allowCount++
}
}
assert.Equal(10, allowCount, "key2 1st round not limit")
time.Sleep(1050 * time.Millisecond)
allowCount = 0
for i := 0; i < 100; i++ {
if r(key1) {
allowCount++
}
}
assert.Equal(10, allowCount, "key1 2nd round not limit")
allowCount = 0
for i := 0; i < 100; i++ {
if r(key2) {
allowCount++
}
}
assert.Equal(10, allowCount, "key2 2nd round not limit")
}
func TestMemoryLimiter(t *testing.T) {
rf := limiter.RateLimiter(limiter.UseMemory(), limiter.BucketLimit(1000*time.Millisecond, 10))
mr := rf()
assertRateLimiter(mr, t)
}
func TestRedisLimiter(t *testing.T) {
clearUpRedis()
rf := limiter.RateLimiter(limiter.UseRedis(redisPool), limiter.BucketLimit(1000*time.Millisecond, 10))
rr := rf()
assertRateLimiter(rr, t)
assert.Equal(t, 100, size("rl-key1"))
}
func TestFloodThreshold(t *testing.T) {
clearUpRedis()
rf := limiter.RateLimiter(limiter.UseRedis(redisPool), limiter.BucketLimit(1000*time.Millisecond, 10), limiter.FloodThreshold(5))
rr := rf()
assertRateLimiter(rr, t)
assert.Equal(t, 51, size("rl-key1"))
time.Sleep(1500 * time.Millisecond)
assertRateLimiter(rr, t)
}
func TestMinPeriod(t *testing.T) {
clearUpRedis()
rf := limiter.RateLimiter(limiter.UseRedis(redisPool), limiter.MinPeriod(10*time.Millisecond))
allow := rf()
assert.True(t, allow("key1"))
assert.False(t, allow("key1"))
assert.True(t, allow("key2"))
time.Sleep(15 * time.Millisecond)
assert.True(t, allow("key1"))
assert.False(t, allow("key1"))
assert.True(t, allow("key2"))
}