-
Notifications
You must be signed in to change notification settings - Fork 245
/
ratelimiter_test.go
86 lines (71 loc) · 2.29 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
package boomer
import (
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("test ratelimiter", func() {
It("test stable ratelimiter", func() {
rateLimiter := NewStableRateLimiter(1, 10*time.Millisecond)
rateLimiter.Start()
defer rateLimiter.Stop()
Expect(rateLimiter.Acquire()).NotTo(BeTrue())
Expect(rateLimiter.Acquire()).To(BeTrue())
})
It("test stable ratelimiter start and stop many times", func() {
Expect(func() {
ratelimiter := NewStableRateLimiter(100, 5*time.Millisecond)
for i := 0; i < 500; i++ {
ratelimiter.Start()
time.Sleep(5 * time.Millisecond)
ratelimiter.Stop()
}
}).Should(Not(Panic()))
})
It("test rampup ratelimiter", func() {
rateLimiter, _ := NewRampUpRateLimiter(100, "10/200ms", 100*time.Millisecond)
rateLimiter.Start()
defer rateLimiter.Stop()
for i := 0; i < 10; i++ {
Expect(rateLimiter.Acquire()).NotTo(BeTrue())
}
Expect(rateLimiter.Acquire()).To(BeTrue())
time.Sleep(210 * time.Millisecond)
// now, the threshold is 20
for i := 0; i < 20; i++ {
Expect(rateLimiter.Acquire()).NotTo(BeTrue())
}
Expect(rateLimiter.Acquire()).To(BeTrue())
})
It("test rampup ratelimiter start and stop many times", func() {
Expect(func() {
ratelimiter, _ := NewRampUpRateLimiter(100, "10/200ms", 100*time.Millisecond)
for i := 0; i < 500; i++ {
ratelimiter.Start()
time.Sleep(5 * time.Millisecond)
ratelimiter.Stop()
}
}).Should(Not(Panic()))
})
It("test parse rampup rate", func() {
rateLimiter := &RampUpRateLimiter{}
rampUpStep, rampUpPeriod, _ := rateLimiter.parseRampUpRate("100")
Expect(rampUpStep).To(BeEquivalentTo(100))
Expect(rampUpPeriod).To(BeEquivalentTo(time.Second))
rampUpStep, rampUpPeriod, _ = rateLimiter.parseRampUpRate("200/10s")
Expect(rampUpStep).To(BeEquivalentTo(200))
Expect(rampUpPeriod).To(BeEquivalentTo(10 * time.Second))
})
DescribeTable("test parse invalid rampup rate", func(rampupRate string) {
rateLimiter := &RampUpRateLimiter{}
_, _, err := rateLimiter.parseRampUpRate(rampupRate)
Expect(err).Should(HaveOccurred())
Expect(err).Should(BeIdenticalTo(ErrParsingRampUpRate))
},
Entry("Invalid", "A/1m"),
Entry("Invalid", "A"),
Entry("Invalid", "200/1s/"),
Entry("Invalid", "200/1"),
Entry("Invalid", "200/1"),
)
})