/
leakybucket_test.go
142 lines (133 loc) · 5.25 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
package limiters_test
import (
"context"
"sync"
"time"
"github.com/google/uuid"
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. It does not increment the queue size.
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 increments the queue size by 1.
wait, err = bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(rate*2, wait)
// The third call overflows the bucket capacity.
wait, err = bucket.Limit(context.TODO())
s.Require().Equal(l.ErrLimitExhausted, err)
s.Equal(rate*3, 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*2, wait)
})
}
}