-
Notifications
You must be signed in to change notification settings - Fork 10
/
token_bucket_limiter_test.go
55 lines (52 loc) · 1.04 KB
/
token_bucket_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 limiter
import (
"testing"
"time"
)
func TestNewTokenBucketLimiter(t *testing.T) {
type args struct {
capacity int
rate int
}
tests := []struct {
name string
args args
want *TokenBucketLimiter
}{
{
name: "60",
args: args{
capacity: 60,
rate: 10,
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := NewTokenBucketLimiter(tt.args.capacity, tt.args.rate)
time.Sleep(time.Second)
successCount := 0
for i := 0; i < tt.args.rate; i++ {
if l.TryAcquire() {
successCount++
}
}
if successCount != tt.args.rate {
t.Errorf("NewTokenBucketLimiter() got = %v, want %v", successCount, tt.args.rate)
return
}
successCount = 0
for i := 0; i < tt.args.capacity; i++ {
if l.TryAcquire() {
successCount++
}
time.Sleep(time.Second / 10)
}
if successCount != tt.args.capacity-tt.args.rate {
t.Errorf("NewTokenBucketLimiter() got = %v, want %v", successCount, tt.args.capacity-tt.args.rate)
return
}
})
}
}