-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrate_limit_test.go
94 lines (84 loc) · 1.66 KB
/
rate_limit_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
package limiter
import (
"sync"
"sync/atomic"
"testing"
"time"
)
// run: go test -v -run Test_RateLimit_Run
func Test_RateLimit_Run(t *testing.T) {
limter := NewRateLimiter(100, 1*time.Second) // QPS = 100
total := 500
i := 1
handler := func(expectSucc, expectFail int64) {
var wg sync.WaitGroup
var succ, fail int64
// so many request coming at the same time
for j := 0; j < total; j++ {
go func() {
wg.Add(1)
err := limter.Run(func() {
time.Sleep(10 * time.Millisecond)
})
if err != nil {
atomic.AddInt64(&fail, 1)
} else {
atomic.AddInt64(&succ, 1)
}
wg.Done()
}()
}
wg.Wait()
if succ != expectSucc || fail != expectFail {
t.Errorf("i:%d expect succ:%d fail:%d, actual succ:%d fail:%d\n", i, expectSucc, expectFail, succ, fail)
}
}
go handler(99, 401)
dur := time.Tick(time.Second)
for {
select {
case <-dur:
i++
if i == 3 {
goto HERE
}
handler(100, 400)
}
}
HERE:
time.Sleep(2 * time.Second)
go handler(100, 400)
time.Sleep(time.Second)
}
// run: go test -v -run Test_RateLimit_Run2
func Test_RateLimit_Run2(t *testing.T) {
var succ, fail int64
var wg sync.WaitGroup
limter := NewRateLimiter(10, 1*time.Second) // QPS = 10
ms10 := time.Tick(time.Millisecond * 10)
s10 := time.Tick(time.Second * 10)
LOOP:
for {
select {
case <-ms10:
go func() {
wg.Add(1)
err := limter.Run(func() {
time.Sleep(10 * time.Millisecond)
})
if err != nil {
atomic.AddInt64(&fail, 1)
} else {
atomic.AddInt64(&succ, 1)
}
wg.Done()
}()
case <-s10:
break LOOP
}
}
wg.Wait()
if succ != 99 {
t.Errorf("succ:%d fail:%d\n", succ, fail)
}
}