/
ratelimit_test.go
143 lines (117 loc) · 2.59 KB
/
ratelimit_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package ratelimit_test
import (
"fmt"
"sync"
"testing"
"time"
"go.uber.org/ratelimit"
"go.uber.org/ratelimit/internal/clock"
"github.com/stretchr/testify/assert"
"github.com/uber-go/atomic"
)
func ExampleRatelimit() {
rl := ratelimit.New(100) // per second
prev := time.Now()
for i := 0; i < 10; i++ {
now := rl.Take()
if i > 0 {
fmt.Println(i, now.Sub(prev))
}
prev = now
}
// Output:
// 1 10ms
// 2 10ms
// 3 10ms
// 4 10ms
// 5 10ms
// 6 10ms
// 7 10ms
// 8 10ms
// 9 10ms
}
func TestUnlimited(t *testing.T) {
now := time.Now()
rl := ratelimit.NewUnlimited()
for i := 0; i < 1000; i++ {
rl.Take()
}
assert.Condition(t, func() bool { return time.Now().Sub(now) < 1*time.Millisecond }, "no artificial delay")
}
func TestRateLimiter(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
clock := clock.NewMock()
rl := ratelimit.New(100, ratelimit.WithClock(clock), ratelimit.WithoutSlack)
count := atomic.NewInt32(0)
// Until we're done...
done := make(chan struct{})
defer close(done)
// Create copious counts concurrently.
go job(rl, count, done)
go job(rl, count, done)
go job(rl, count, done)
go job(rl, count, done)
clock.AfterFunc(1*time.Second, func() {
assert.InDelta(t, 100, count.Load(), 10, "count within rate limit")
})
clock.AfterFunc(2*time.Second, func() {
assert.InDelta(t, 200, count.Load(), 10, "count within rate limit")
})
clock.AfterFunc(3*time.Second, func() {
assert.InDelta(t, 300, count.Load(), 10, "count within rate limit")
wg.Done()
})
clock.Add(4 * time.Second)
clock.Add(5 * time.Second)
}
func TestDelayedRateLimiter(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
defer wg.Wait()
clock := clock.NewMock()
slow := ratelimit.New(10, ratelimit.WithClock(clock))
fast := ratelimit.New(100, ratelimit.WithClock(clock))
count := atomic.NewInt32(0)
// Until we're done...
done := make(chan struct{})
defer close(done)
// Run a slow job
go func() {
for {
slow.Take()
fast.Take()
count.Inc()
select {
case <-done:
return
default:
}
}
}()
// Accumulate slack for 10 seconds,
clock.AfterFunc(20*time.Second, func() {
// Then start working.
go job(fast, count, done)
go job(fast, count, done)
go job(fast, count, done)
go job(fast, count, done)
})
clock.AfterFunc(30*time.Second, func() {
assert.InDelta(t, 1200, count.Load(), 10, "count within rate limit")
wg.Done()
})
clock.Add(40 * time.Second)
}
func job(rl ratelimit.Limiter, count *atomic.Int32, done <-chan struct{}) {
for {
rl.Take()
count.Inc()
select {
case <-done:
return
default:
}
}
}