-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimit_test.go
82 lines (69 loc) · 1.61 KB
/
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
package rate
import (
"fmt"
"testing"
"time"
)
func TestNewLimit(t *testing.T) {
defer func() {
if err := recover(); err != nil {
t.Errorf("NewLimit paniked")
}
}()
NewLimit(time.Second)
}
func TestLimitWait(t *testing.T) {
l := NewLimit(10 * time.Millisecond)
defer l.Close()
start := time.Now()
l.Wait()
if time.Since(start) < 10*time.Millisecond {
t.Errorf("LimitWait was incorrect, should have run for 10ms")
}
}
func TestLimitTry(t *testing.T) {
l := NewLimit(10 * time.Millisecond)
defer l.Close()
var err error
<-time.After(11 * time.Millisecond)
err = l.Try()
if err != nil {
t.Errorf("LimitTry was incorrect, got: %v, want: %v", err, nil)
}
err = l.Try()
if err != errTooFast {
t.Errorf("LimitTry was incorrect, got: %v, want: %v", err, errTooFast)
}
}
func ExampleNewLimit() {
// initialize a new rate-limier
limiter := NewLimit(100 * time.Millisecond)
// remember to free resources
defer limiter.Close()
for i := 0; i < 10; i++ {
// block until the next free token becomes available
limiter.Wait()
// calling Try() immediately afterwards will always fail, because the
// above Wait() just consumed the token
err := limiter.Try()
fmt.Printf("%d, %s\n", i, err)
}
// if we wait for the regeneration of a new free token
<-time.After(time.Second)
// calling Try() now will not retrun an error, because there is a free
// token available in the bucket.
err := limiter.Try()
fmt.Println(err)
// Output:
// 0, too fast
// 1, too fast
// 2, too fast
// 3, too fast
// 4, too fast
// 5, too fast
// 6, too fast
// 7, too fast
// 8, too fast
// 9, too fast
// <nil>
}