-
Notifications
You must be signed in to change notification settings - Fork 49
/
limiters_test.go
250 lines (224 loc) · 6.93 KB
/
limiters_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package limiters_test
import (
"context"
"database/sql"
"fmt"
"hash/fnv"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/bradfitz/gomemcache/memcache"
"github.com/go-redsync/redsync/v4/redis/goredis/v9"
"github.com/google/uuid"
"github.com/hashicorp/consul/api"
"github.com/redis/go-redis/v9"
"github.com/samuel/go-zookeeper/zk"
"github.com/stretchr/testify/suite"
clientv3 "go.etcd.io/etcd/client/v3"
l "github.com/mennanov/limiters"
)
type fakeClock struct {
mu sync.Mutex
initial time.Time
t time.Time
}
func newFakeClock() *fakeClock {
now := time.Now()
return &fakeClock{t: now, initial: now}
}
func newFakeClockWithTime(t time.Time) *fakeClock {
return &fakeClock{t: t, initial: t}
}
func (c *fakeClock) Now() time.Time {
c.mu.Lock()
defer c.mu.Unlock()
return c.t
}
func (c *fakeClock) Sleep(d time.Duration) {
if d == 0 {
return
}
c.mu.Lock()
defer c.mu.Unlock()
c.t = c.t.Add(d)
}
func (c *fakeClock) reset() {
c.mu.Lock()
defer c.mu.Unlock()
c.t = c.initial
}
type LimitersTestSuite struct {
suite.Suite
etcdClient *clientv3.Client
redisClient *redis.Client
redisClusterClient *redis.ClusterClient
consulClient *api.Client
zkConn *zk.Conn
logger *l.StdLogger
dynamodbClient *dynamodb.Client
dynamoDBTableProps l.DynamoDBTableProperties
memcacheClient *memcache.Client
pgDb *sql.DB
}
func (s *LimitersTestSuite) SetupSuite() {
var err error
s.etcdClient, err = clientv3.New(clientv3.Config{
Endpoints: strings.Split(os.Getenv("ETCD_ENDPOINTS"), ","),
DialTimeout: time.Second,
})
s.Require().NoError(err)
s.redisClient = redis.NewClient(&redis.Options{
Addr: os.Getenv("REDIS_ADDR"),
})
s.redisClusterClient = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: strings.Split(os.Getenv("REDIS_NODES"), ","),
})
s.consulClient, err = api.NewClient(&api.Config{Address: os.Getenv("CONSUL_ADDR")})
s.Require().NoError(err)
s.zkConn, _, err = zk.Connect(strings.Split(os.Getenv("ZOOKEEPER_ENDPOINTS"), ","), time.Second)
s.Require().NoError(err)
s.logger = l.NewStdLogger()
awsCfg, err := config.LoadDefaultConfig(context.Background(),
config.WithRegion("us-east-1"),
config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
Value: aws.Credentials{
AccessKeyID: "dummy", SecretAccessKey: "dummy", SessionToken: "dummy",
Source: "Hard-coded credentials; values are irrelevant for local DynamoDB",
},
}),
)
s.Require().NoError(err)
endpoint := fmt.Sprintf("http://%s", os.Getenv("AWS_ADDR"))
s.dynamodbClient = dynamodb.NewFromConfig(awsCfg, func(options *dynamodb.Options) {
options.BaseEndpoint = &endpoint
})
_ = DeleteTestDynamoDBTable(context.Background(), s.dynamodbClient)
s.Require().NoError(CreateTestDynamoDBTable(context.Background(), s.dynamodbClient))
s.dynamoDBTableProps, err = l.LoadDynamoDBTableProperties(context.Background(), s.dynamodbClient, testDynamoDBTableName)
s.Require().NoError(err)
s.memcacheClient = memcache.New(strings.Split(os.Getenv("MEMCACHED_ADDR"), ",")...)
s.Require().NoError(s.memcacheClient.Ping())
s.pgDb, err = sql.Open("postgres", os.Getenv("POSTGRES_URL"))
s.Require().NoError(err)
s.Require().NoError(s.pgDb.Ping())
}
func (s *LimitersTestSuite) TearDownSuite() {
s.Assert().NoError(s.etcdClient.Close())
s.Assert().NoError(s.redisClient.Close())
s.Assert().NoError(s.redisClusterClient.Close())
s.Assert().NoError(DeleteTestDynamoDBTable(context.Background(), s.dynamodbClient))
s.Assert().NoError(s.memcacheClient.Close())
s.Assert().NoError(s.pgDb.Close())
}
func TestLimitersTestSuite(t *testing.T) {
suite.Run(t, new(LimitersTestSuite))
}
// lockers returns all possible lockers (including noop).
func (s *LimitersTestSuite) lockers(generateKeys bool) map[string]l.DistLocker {
lockers := s.distLockers(generateKeys)
lockers["LockNoop"] = l.NewLockNoop()
return lockers
}
func hash(s string) int64 {
h := fnv.New32a()
_, err := h.Write([]byte(s))
if err != nil {
panic(err)
}
return int64(h.Sum32())
}
// distLockers returns distributed lockers only.
func (s *LimitersTestSuite) distLockers(generateKeys bool) map[string]l.DistLocker {
randomKey := uuid.New().String()
consulKey := randomKey
etcdKey := randomKey
zkKey := "/" + randomKey
redisKey := randomKey
memcacheKey := randomKey
pgKey := randomKey
if !generateKeys {
consulKey = "dist_locker"
etcdKey = "dist_locker"
zkKey = "/dist_locker"
redisKey = "dist_locker"
memcacheKey = "dist_locker"
pgKey = "dist_locker"
}
consulLock, err := s.consulClient.LockKey(consulKey)
s.Require().NoError(err)
return map[string]l.DistLocker{
"LockEtcd": l.NewLockEtcd(s.etcdClient, etcdKey, s.logger),
"LockConsul": l.NewLockConsul(consulLock),
"LockZookeeper": l.NewLockZookeeper(zk.NewLock(s.zkConn, zkKey, zk.WorldACL(zk.PermAll))),
"LockRedis": l.NewLockRedis(goredis.NewPool(s.redisClient), redisKey),
"LockRedisCluster": l.NewLockRedis(goredis.NewPool(s.redisClusterClient), redisKey),
"LockMemcached": l.NewLockMemcached(s.memcacheClient, memcacheKey),
"LockPostgreSQL": l.NewLockPostgreSQL(s.pgDb, hash(pgKey)),
}
}
func (s *LimitersTestSuite) TestLimitContextCancelled() {
clock := newFakeClock()
capacity := int64(2)
rate := time.Second
limiters := make(map[string]interface{})
for n, b := range s.tokenBuckets(capacity, rate, clock) {
limiters[n] = b
}
for n, b := range s.leakyBuckets(capacity, rate, clock) {
limiters[n] = b
}
for n, w := range s.fixedWindows(capacity, rate, clock) {
limiters[n] = w
}
for n, w := range s.slidingWindows(capacity, rate, clock, 1e-9) {
limiters[n] = w
}
for n, b := range s.concurrentBuffers(capacity, rate, clock) {
limiters[n] = b
}
type rateLimiter interface {
Limit(context.Context) (time.Duration, error)
}
type concurrentLimiter interface {
Limit(context.Context, string) error
}
for name, limiter := range limiters {
s.Run(name, func() {
done1 := make(chan struct{})
go func(limiter interface{}) {
defer close(done1)
// The context is expired shortly after it is created.
ctx, cancel := context.WithCancel(context.Background())
cancel()
switch lim := limiter.(type) {
case rateLimiter:
_, err := lim.Limit(ctx)
s.Error(err, "%T", limiter)
case concurrentLimiter:
s.Error(lim.Limit(ctx, "key"), "%T", limiter)
}
}(limiter)
done2 := make(chan struct{})
go func(limiter interface{}) {
defer close(done2)
<-done1
ctx := context.Background()
switch lim := limiter.(type) {
case rateLimiter:
_, err := lim.Limit(ctx)
s.NoError(err, "%T", limiter)
case concurrentLimiter:
s.NoError(lim.Limit(ctx, "key"), "%T", limiter)
}
}(limiter)
// Verify that the second go routine succeeded calling the Limit() method.
<-done2
})
}
}