-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratelimiter_test.go
110 lines (88 loc) · 2.67 KB
/
ratelimiter_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
package qpool
import (
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestNewRateLimiter(t *testing.T) {
Convey("Given a new rate limiter", t, func() {
limiter := NewRateLimiter(100, time.Second)
Convey("It should be properly initialized", func() {
So(limiter, ShouldNotBeNil)
So(limiter.tokens, ShouldEqual, 100)
So(limiter.maxTokens, ShouldEqual, 100)
So(limiter.refillRate, ShouldEqual, time.Second)
})
})
}
func TestRateLimiterObserve(t *testing.T) {
Convey("Given a rate limiter", t, func() {
limiter := NewRateLimiter(100, time.Second)
metrics := &Metrics{}
Convey("When observing metrics", func() {
limiter.Observe(metrics)
So(limiter.metrics, ShouldEqual, metrics)
})
})
}
func TestRateLimiterLimit(t *testing.T) {
Convey("Given a rate limiter with 2 tokens", t, func() {
limiter := NewRateLimiter(2, time.Second)
Convey("When consuming tokens", func() {
// First two requests should succeed
So(limiter.Limit(), ShouldBeFalse)
So(limiter.Limit(), ShouldBeFalse)
// Third request should be limited
So(limiter.Limit(), ShouldBeTrue)
})
})
}
func TestRateLimiterBurst(t *testing.T) {
Convey("Given a rate limiter with burst capacity", t, func() {
limiter := NewRateLimiter(3, 100*time.Millisecond)
Convey("It should handle burst and refill", func() {
// Use all tokens in burst
So(limiter.Limit(), ShouldBeFalse)
So(limiter.Limit(), ShouldBeFalse)
So(limiter.Limit(), ShouldBeFalse)
So(limiter.Limit(), ShouldBeTrue)
// Wait for refill
time.Sleep(150 * time.Millisecond)
// Should have tokens again
So(limiter.Limit(), ShouldBeFalse)
})
})
}
func TestRateLimiterRefill(t *testing.T) {
Convey("Given a rate limiter", t, func() {
limiter := NewRateLimiter(5, 100*time.Millisecond)
Convey("It should refill tokens over time", func() {
// Use some tokens
So(limiter.Limit(), ShouldBeFalse)
So(limiter.Limit(), ShouldBeFalse)
So(limiter.tokens, ShouldEqual, 3)
// Wait for refill period
time.Sleep(150 * time.Millisecond)
// Force refill check
limiter.Renormalize()
// Should be refilled
So(limiter.tokens, ShouldEqual, 5)
})
})
}
func TestRateLimiterRenormalize(t *testing.T) {
Convey("Given a rate limiter", t, func() {
limiter := NewRateLimiter(2, 100*time.Millisecond)
Convey("When renormalizing", func() {
// Use all tokens
So(limiter.Limit(), ShouldBeFalse)
So(limiter.Limit(), ShouldBeFalse)
So(limiter.tokens, ShouldEqual, 0)
// Wait and renormalize
time.Sleep(150 * time.Millisecond)
limiter.Renormalize()
// Should have tokens again
So(limiter.tokens, ShouldEqual, 2)
})
})
}