-
Notifications
You must be signed in to change notification settings - Fork 22
/
rate_limiter_test.go
55 lines (43 loc) · 1003 Bytes
/
rate_limiter_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
package main_test
import (
. "github.com/cloudfoundry-samples/ratelimit-service"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("RateLimiter", func() {
var (
limit int
limiter *RateLimiter
)
Describe("ExceedsLimit", func() {
BeforeEach(func() {
limit = 5
limiter = NewRateLimiter(limit)
})
It("reports if rate exceeded", func() {
ip := "192.168.1.1"
for i := 0; i < limit; i++ {
Expect(limiter.ExceedsLimit(ip)).To(BeFalse())
}
Expect(limiter.ExceedsLimit(ip)).To(BeTrue())
})
})
Describe("Stats", func() {
BeforeEach(func() {
limit = 10
limiter = NewRateLimiter(limit)
})
It("reports stats ", func() {
for i := 5; i < limit; i++ {
ip := "192.168.1.100"
Expect(limiter.ExceedsLimit(ip)).To(BeFalse())
}
for i := 7; i < limit; i++ {
ip := "192.168.1.101"
Expect(limiter.ExceedsLimit(ip)).To(BeFalse())
}
stats := limiter.GetStats()
Expect(len(stats)).To(Equal(2))
})
})
})