/
fixedwindow_test.go
104 lines (96 loc) · 2.97 KB
/
fixedwindow_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
package limiters_test
import (
"context"
"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),
}
}
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)
}
if _, err := bucket.Limit(context.TODO()); 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)
})
}
}