-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathfixedwindow_test.go
More file actions
153 lines (132 loc) · 4.25 KB
/
fixedwindow_test.go
File metadata and controls
153 lines (132 loc) · 4.25 KB
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
package limiters_test
import (
"context"
"testing"
"time"
"github.com/google/uuid"
l "github.com/mennanov/limiters"
)
// fixedWindows returns all the possible FixedWindow combinations.
func (s *LimitersTestSuite) fixedWindows(capacity int64, rate time.Duration, clock l.Clock) map[string]*l.FixedWindow {
windows := make(map[string]*l.FixedWindow)
for name, inc := range s.fixedWindowIncrementers() {
windows[name] = l.NewFixedWindow(capacity, rate, inc, clock)
}
return windows
}
func (s *LimitersTestSuite) fixedWindowIncrementers() map[string]l.FixedWindowIncrementer {
return map[string]l.FixedWindowIncrementer{
"FixedWindowInMemory": l.NewFixedWindowInMemory(),
"FixedWindowRedis": l.NewFixedWindowRedis(s.redisClient, uuid.New().String()),
"FixedWindowRedisCluster": l.NewFixedWindowRedis(s.redisClusterClient, uuid.New().String()),
"FixedWindowMemcached": l.NewFixedWindowMemcached(s.memcacheClient, uuid.New().String()),
"FixedWindowDynamoDB": l.NewFixedWindowDynamoDB(s.dynamodbClient, uuid.New().String(), s.dynamoDBTableProps),
"FixedWindowCosmosDB": l.NewFixedWindowCosmosDB(s.cosmosContainerClient, uuid.New().String()),
}
}
var fixedWindowTestCases = []struct {
capacity int64
rate time.Duration
requestCount int
requestRate time.Duration
missExpected int
}{
{
capacity: 2,
rate: time.Millisecond * 100,
requestCount: 20,
requestRate: time.Millisecond * 25,
missExpected: 10,
},
{
capacity: 4,
rate: time.Millisecond * 100,
requestCount: 20,
requestRate: time.Millisecond * 25,
missExpected: 0,
},
{
capacity: 2,
rate: time.Millisecond * 100,
requestCount: 15,
requestRate: time.Millisecond * 33,
missExpected: 5,
},
}
func (s *LimitersTestSuite) TestFixedWindowFakeClock() {
clock := newFakeClockWithTime(time.Date(2019, 8, 30, 0, 0, 0, 0, time.UTC))
for _, testCase := range fixedWindowTestCases {
for name, bucket := range s.fixedWindows(testCase.capacity, testCase.rate, clock) {
s.Run(name, func() {
clock.reset()
miss := 0
for i := 0; i < testCase.requestCount; i++ {
// No pause for the first request.
if i > 0 {
clock.Sleep(testCase.requestRate)
}
_, err := bucket.Limit(context.TODO())
if err != nil {
s.Equal(l.ErrLimitExhausted, err)
miss++
}
}
s.Equal(testCase.missExpected, miss, testCase)
})
}
}
}
func (s *LimitersTestSuite) TestFixedWindowOverflow() {
clock := newFakeClockWithTime(time.Date(2019, 8, 30, 0, 0, 0, 0, time.UTC))
for name, bucket := range s.fixedWindows(2, time.Second, clock) {
s.Run(name, func() {
clock.reset()
w, err := bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(time.Duration(0), w)
w, err = bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(time.Duration(0), w)
w, err = bucket.Limit(context.TODO())
s.Require().Equal(l.ErrLimitExhausted, err)
s.Equal(time.Second, w)
clock.Sleep(time.Second)
w, err = bucket.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(time.Duration(0), w)
})
}
}
func (s *LimitersTestSuite) TestFixedWindowDynamoDBPartitionKey() {
clock := newFakeClockWithTime(time.Date(2019, 8, 30, 0, 0, 0, 0, time.UTC))
incrementor := l.NewFixedWindowDynamoDB(s.dynamodbClient, "partitionKey1", s.dynamoDBTableProps)
window := l.NewFixedWindow(2, time.Millisecond*100, incrementor, clock)
w, err := window.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(time.Duration(0), w)
w, err = window.Limit(context.TODO())
s.Require().NoError(err)
s.Equal(time.Duration(0), w)
// The third call should fail for the "partitionKey1", but succeed for "partitionKey2".
w, err = window.Limit(l.NewFixedWindowDynamoDBContext(context.Background(), "partitionKey2"))
s.Require().NoError(err)
s.Equal(time.Duration(0), w)
}
func BenchmarkFixedWindows(b *testing.B) {
s := new(LimitersTestSuite)
s.SetT(&testing.T{})
s.SetupSuite()
capacity := int64(1)
rate := time.Second
clock := newFakeClock()
windows := s.fixedWindows(capacity, rate, clock)
for name, window := range windows {
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := window.Limit(context.TODO())
s.Require().NoError(err)
}
})
}
s.TearDownSuite()
}