forked from mennanov/limiters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenbucket_test.go
164 lines (155 loc) · 3.75 KB
/
tokenbucket_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
package limiters_test
import (
"context"
"sync"
"time"
"github.com/google/uuid"
l "github.com/mennanov/limiters"
)
var tokenBucketUniformTestCases = []struct {
capacity int64
refillRate time.Duration
requestCount int64
requestRate time.Duration
missExpected int
delta float64
}{
{
5,
time.Millisecond * 100,
10,
time.Millisecond * 25,
3,
1,
},
{
10,
time.Millisecond * 100,
13,
time.Millisecond * 25,
0,
1,
},
{
10,
time.Millisecond * 100,
15,
time.Millisecond * 33,
2,
1,
},
{
10,
time.Millisecond * 100,
16,
time.Millisecond * 33,
2,
1,
},
{
10,
time.Millisecond * 10,
20,
time.Millisecond * 10,
0,
1,
},
{
1,
time.Millisecond * 200,
10,
time.Millisecond * 100,
5,
2,
},
}
// tokenBuckets returns all the possible TokenBucket combinations.
func (s *LimitersTestSuite) tokenBuckets(capacity int64, refillRate time.Duration, clock l.Clock) []*l.TokenBucket {
var buckets []*l.TokenBucket
for _, locker := range s.lockers(true) {
for _, backend := range s.tokenBucketBackends() {
buckets = append(buckets, l.NewTokenBucket(capacity, refillRate, locker, backend, clock, s.logger))
}
}
return buckets
}
func (s *LimitersTestSuite) tokenBucketBackends() []l.TokenBucketStateBackend {
return []l.TokenBucketStateBackend{
l.NewTokenBucketInMemory(),
l.NewTokenBucketEtcd(s.etcdClient, uuid.New().String(), time.Second, false),
l.NewTokenBucketEtcd(s.etcdClient, uuid.New().String(), time.Second, true),
l.NewTokenBucketRedis(s.redisClient, uuid.New().String(), time.Second, false),
l.NewTokenBucketRedis(s.redisClient, uuid.New().String(), time.Second, true),
}
}
func (s *LimitersTestSuite) TestTokenBucketRealClock() {
clock := l.NewSystemClock()
for _, testCase := range tokenBucketUniformTestCases {
for _, bucket := range s.tokenBuckets(testCase.capacity, testCase.refillRate, clock) {
wg := sync.WaitGroup{}
// mu guards the miss variable below.
var mu sync.Mutex
miss := 0
for i := int64(0); i < testCase.requestCount; i++ {
// No pause for the first request.
if i > 0 {
clock.Sleep(testCase.requestRate)
}
wg.Add(1)
go func(bucket *l.TokenBucket) {
defer wg.Done()
if _, err := bucket.Limit(context.TODO()); err != nil {
s.Equal(l.ErrLimitExhausted, err, "%T %v", bucket, bucket)
mu.Lock()
miss++
mu.Unlock()
}
}(bucket)
}
wg.Wait()
s.InDelta(testCase.missExpected, miss, testCase.delta, testCase)
}
}
}
func (s *LimitersTestSuite) TestTokenBucketFakeClock() {
for _, testCase := range tokenBucketUniformTestCases {
clock := newFakeClock()
for _, bucket := range s.tokenBuckets(testCase.capacity, testCase.refillRate, clock) {
clock.reset()
miss := 0
for i := int64(0); i < testCase.requestCount; i++ {
// No pause for the first request.
if i > 0 {
clock.Sleep(testCase.requestRate)
}
if _, err := bucket.Limit(context.TODO()); err != nil {
s.Equal(l.ErrLimitExhausted, err)
miss++
}
}
s.InDelta(testCase.missExpected, miss, testCase.delta, testCase)
}
}
}
func (s *LimitersTestSuite) TestTokenBucketOverflow() {
clock := newFakeClock()
rate := time.Second
for _, bucket := range s.tokenBuckets(2, rate, clock) {
clock.reset()
wait, err := bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(time.Duration(0), wait)
wait, err = bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(time.Duration(0), wait)
// The third call should fail.
wait, err = bucket.Limit(context.TODO())
s.Require().Equal(l.ErrLimitExhausted, err)
s.Equal(rate, wait)
clock.Sleep(wait)
// Retry the last call.
wait, err = bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(time.Duration(0), wait)
}
}