-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliding_window_test.go
80 lines (70 loc) · 2.04 KB
/
sliding_window_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
package ratelimiter_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"ratelimiter"
"time"
)
var _ = Describe("Sliding window", func() {
opts := ratelimiter.LimiterOptions{
Limit: 5,
Interval: time.Millisecond * 5,
Tick: time.Millisecond,
}
Describe("Basic tests, sync, one instance", func() {
var limiter ratelimiter.RateLimiter
BeforeEach(func() {
limiter = ratelimiter.NewSlidingWindow(opts)
})
It("Get some, no tick advance", func() {
Expect(limiter.GetTokens(2)).To(Equal(int32(2)))
Expect(limiter.GetTokens(2)).To(Equal(int32(2)))
Expect(limiter.GetTokens(2)).To(Equal(int32(1)))
Expect(limiter.GetTokens(2)).To(Equal(int32(0)))
})
It("Get some, with tick advance", func() {
for i := 0; i < 5; i++ {
limiter.GetTokens(2)
limiter.NextTick()
}
Expect(limiter.GetTokens(10)).To(Equal(int32(1)))
limiter.NextTick()
Expect(limiter.GetTokens(10)).To(Equal(int32(1)))
limiter.NextTick()
Expect(limiter.GetTokens(10)).To(Equal(int32(1)))
limiter.NextTick()
Expect(limiter.GetTokens(10)).To(Equal(int32(1)))
limiter.NextTick()
Expect(limiter.GetTokens(10)).To(Equal(int32(1)))
})
It("Get some from end of prev, then from start of next", func() {
limiter.NextTick()
limiter.NextTick()
limiter.NextTick()
limiter.NextTick()
limiter.GetTokens(10)
limiter.NextTick()
Expect(limiter.GetTokens(10)).To(Equal(int32(1)))
limiter.NextTick()
Expect(limiter.GetTokens(10)).To(Equal(int32(1)))
limiter.NextTick()
Expect(limiter.GetTokens(10)).To(Equal(int32(1)))
limiter.NextTick()
Expect(limiter.GetTokens(10)).To(Equal(int32(1)))
limiter.NextTick()
Expect(limiter.GetTokens(10)).To(Equal(int32(1)))
})
})
Describe("Sync test, for race condition checking", func() {
XIt("Race condition test, run with --race flag", func() {
limiter := ratelimiter.NewSlidingWindow(opts)
for i := 0; i < 100000; i++ {
go func() {
limiter.GetTokens(10)
limiter.IsTokenExists()
limiter.NextTick()
}()
}
})
})
})