-
Notifications
You must be signed in to change notification settings - Fork 49
/
leakybucket_test.go
199 lines (187 loc) · 6.87 KB
/
leakybucket_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
package limiters_test
import (
"context"
"sync"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
l "github.com/mennanov/limiters"
)
// leakyBuckets returns all the possible leakyBuckets combinations.
func (s *LimitersTestSuite) leakyBuckets(capacity int64, rate time.Duration, clock l.Clock) map[string]*l.LeakyBucket {
buckets := make(map[string]*l.LeakyBucket)
for lockerName, locker := range s.lockers(true) {
for backendName, backend := range s.leakyBucketBackends() {
buckets[lockerName+":"+backendName] = l.NewLeakyBucket(capacity, rate, locker, backend, clock, s.logger)
}
}
return buckets
}
func (s *LimitersTestSuite) leakyBucketBackends() map[string]l.LeakyBucketStateBackend {
return map[string]l.LeakyBucketStateBackend{
"LeakyBucketInMemory": l.NewLeakyBucketInMemory(),
"LeakyBucketEtcdNoRaceCheck": l.NewLeakyBucketEtcd(s.etcdClient, uuid.New().String(), time.Second, false),
"LeakyBucketEtcdWithRaceCheck": l.NewLeakyBucketEtcd(s.etcdClient, uuid.New().String(), time.Second, true),
"LeakyBucketRedisNoRaceCheck": l.NewLeakyBucketRedis(s.redisClient, uuid.New().String(), time.Second, false),
"LeakyBucketRedisWithRaceCheck": l.NewLeakyBucketRedis(s.redisClient, uuid.New().String(), time.Second, true),
"LeakyBucketRedisClusterNoRaceCheck": l.NewLeakyBucketRedis(s.redisClusterClient, uuid.New().String(), time.Second, false),
"LeakyBucketRedisClusterWithRaceCheck": l.NewLeakyBucketRedis(s.redisClusterClient, uuid.New().String(), time.Second, true),
"LeakyBucketMemcachedNoRaceCheck": l.NewLeakyBucketMemcached(s.memcacheClient, uuid.New().String(), time.Second, false),
"LeakyBucketMemcachedWithRaceCheck": l.NewLeakyBucketMemcached(s.memcacheClient, uuid.New().String(), time.Second, true),
"LeakyBucketDynamoDBNoRaceCheck": l.NewLeakyBucketDynamoDB(s.dynamodbClient, uuid.New().String(), s.dynamoDBTableProps, time.Second, false),
"LeakyBucketDynamoDBWithRaceCheck": l.NewLeakyBucketDynamoDB(s.dynamodbClient, uuid.New().String(), s.dynamoDBTableProps, time.Second, true),
}
}
func (s *LimitersTestSuite) TestLeakyBucketRealClock() {
capacity := int64(10)
rate := time.Millisecond * 10
clock := l.NewSystemClock()
for _, requestRate := range []time.Duration{rate / 2} {
for name, bucket := range s.leakyBuckets(capacity, rate, clock) {
s.Run(name, func() {
wg := sync.WaitGroup{}
mu := sync.Mutex{}
var totalWait time.Duration
for i := int64(0); i < capacity; i++ {
// No pause for the first request.
if i > 0 {
clock.Sleep(requestRate)
}
wg.Add(1)
go func(bucket *l.LeakyBucket) {
defer wg.Done()
wait, err := bucket.Limit(context.TODO())
s.Require().NoError(err)
if wait > 0 {
mu.Lock()
totalWait += wait
mu.Unlock()
clock.Sleep(wait)
}
}(bucket)
}
wg.Wait()
expectedWait := time.Duration(0)
if rate > requestRate {
expectedWait = time.Duration(float64(rate-requestRate) * float64(capacity-1) / 2 * float64(capacity))
}
// Allow 5ms lag for each request.
// TODO: figure out if this is enough for slow PCs and possibly avoid hard-coding it.
delta := float64(time.Duration(capacity) * time.Millisecond * 25)
s.InDelta(expectedWait, totalWait, delta, "request rate: %d, bucket: %v", requestRate, bucket)
})
}
}
}
func (s *LimitersTestSuite) TestLeakyBucketFakeClock() {
capacity := int64(10)
rate := time.Millisecond * 100
clock := newFakeClock()
for _, requestRate := range []time.Duration{rate * 2, rate, rate / 2, rate / 3, rate / 4, 0} {
for name, bucket := range s.leakyBuckets(capacity, rate, clock) {
s.Run(name, func() {
clock.reset()
start := clock.Now()
for i := int64(0); i < capacity; i++ {
// No pause for the first request.
if i > 0 {
clock.Sleep(requestRate)
}
wait, err := bucket.Limit(context.TODO())
s.Require().NoError(err)
clock.Sleep(wait)
}
interval := rate
if requestRate > rate {
interval = requestRate
}
s.Equal(interval*time.Duration(capacity-1), clock.Now().Sub(start), "request rate: %d, bucket: %v", requestRate, bucket)
})
}
}
}
func (s *LimitersTestSuite) TestLeakyBucketOverflow() {
rate := time.Second
capacity := int64(2)
clock := newFakeClock()
for name, bucket := range s.leakyBuckets(capacity, rate, clock) {
s.Run(name, func() {
clock.reset()
// The first call has no wait since there were no calls before.
wait, err := bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(time.Duration(0), wait)
// The second call increments the queue size by 1.
wait, err = bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(rate, wait)
// The third call overflows the bucket capacity.
wait, err = bucket.Limit(context.TODO())
s.Require().Equal(l.ErrLimitExhausted, err)
s.Equal(rate*2, wait)
// Move the Clock 1 position forward.
clock.Sleep(rate)
// Retry the last call. This time it should succeed.
wait, err = bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(rate, wait)
})
}
}
func (s *LimitersTestSuite) TestLeakyBucketReset() {
rate := time.Second
capacity := int64(2)
clock := newFakeClock()
for name, bucket := range s.leakyBuckets(capacity, rate, clock) {
s.Run(name, func() {
clock.reset()
// The first call has no wait since there were no calls before.
wait, err := bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(time.Duration(0), wait)
// The second call increments the queue size by 1.
wait, err = bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(rate, wait)
// The third call overflows the bucket capacity.
wait, err = bucket.Limit(context.TODO())
s.Require().Equal(l.ErrLimitExhausted, err)
s.Equal(rate*2, wait)
// Reset the bucket
err = bucket.Reset(context.TODO())
s.Require().NoError(err)
// Retry the last call. This time it should succeed.
wait, err = bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(time.Duration(0), wait)
})
}
}
func TestLeakyBucket_ZeroCapacity_ReturnsError(t *testing.T) {
capacity := int64(0)
rate := time.Hour
logger := l.NewStdLogger()
bucket := l.NewLeakyBucket(capacity, rate, l.NewLockNoop(), l.NewLeakyBucketInMemory(), newFakeClock(), logger)
wait, err := bucket.Limit(context.TODO())
require.Equal(t, l.ErrLimitExhausted, err)
require.Equal(t, time.Duration(0), wait)
}
func BenchmarkLeakyBuckets(b *testing.B) {
s := new(LimitersTestSuite)
s.SetT(&testing.T{})
s.SetupSuite()
capacity := int64(1)
rate := time.Second
clock := newFakeClock()
buckets := s.leakyBuckets(capacity, rate, clock)
for name, bucket := range buckets {
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := bucket.Limit(context.TODO())
s.Require().NoError(err)
}
})
}
s.TearDownSuite()
}