-
Notifications
You must be signed in to change notification settings - Fork 2
/
token_test.go
45 lines (37 loc) · 922 Bytes
/
token_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
package limiter
import (
"errors"
"testing"
)
func TestTokenChanLimiter(t *testing.T) {
l := NewTokenChanLimiter(1)
token := l.AcquireToken()
l.ReleaseToken(token)
}
func TestTokenChanLimiter_Invoke(t *testing.T) {
l := NewTokenChanLimiter(1)
if err := l.Invoke(func() error { return errors.New("error") }); err == nil {
t.Error("Expected error, got nil")
}
if err := l.Invoke(func() error { return nil }); err != nil {
t.Errorf("Unexpected error, got: %s", err.Error())
}
}
func BenchmarkTokenChanLimiterSingle(b *testing.B) {
l := NewTokenChanLimiter(1)
for i := 0; i < b.N; i++ {
token := l.AcquireToken()
l.ReleaseToken(token)
}
}
func BenchmarkTokenChanLimiterMulti(b *testing.B) {
n := b.N * 5
l := NewTokenChanLimiter(uint(n))
tokens := make([]*[16]byte, n)
for j := 0; j < n; j += 1 {
tokens[j] = l.AcquireToken()
}
for j := 0; j < n; j += 1 {
l.ReleaseToken(tokens[j])
}
}