-
Notifications
You must be signed in to change notification settings - Fork 2
/
bucket_test.go
137 lines (102 loc) · 3.37 KB
/
bucket_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
package bucket
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTokenBucket(t *testing.T) {
assert := assert.New(t)
t.Run("Should init the bucket with full available tokens", func(t *testing.T) {
var cap int64 = 100
b := New(time.Minute, cap)
defer b.Destory()
assert.Equal(cap, b.Capability())
assert.Equal(cap, b.Availible())
})
t.Run("Should panic when interval and cap is negative", func(t *testing.T) {
assert.Panics(func() { New(time.Minute, -1) })
assert.Panics(func() { New(-time.Minute, 1) })
assert.Panics(func() { New(-time.Minute, -1) })
})
t.Run("Should panic the count pass to checkCount is negative", func(t *testing.T) {
b := New(time.Minute, 1)
defer b.Destory()
assert.Panics(func() { b.checkCount(-1) })
})
t.Run("Should panic the count pass to checkCount is greater than cap", func(t *testing.T) {
b := New(time.Minute, 1)
defer b.Destory()
assert.Panics(func() { b.checkCount(2) })
})
t.Run("Should return true when tryTake is succsessful", func(t *testing.T) {
b := New(time.Minute, 5)
defer b.Destory()
assert.True(b.TryTake(1))
assert.Equal(int64(4), b.avail)
})
t.Run("Should return false when tryTake is failed", func(t *testing.T) {
b := New(time.Minute, 1)
defer b.Destory()
assert.True(b.TryTake(1))
assert.False(b.TryTake(1))
assert.Equal(int64(0), b.avail)
})
t.Run("Should take until count tokens availible", func(t *testing.T) {
start := time.Now()
b := New(time.Second*2, 1)
defer b.Destory()
assert.True(b.TryTake(1))
assert.False(b.TryTake(1))
b.Take(1)
assert.True(time.Now().Sub(start) > time.Second)
})
t.Run("Should return false when take reach max duration", func(t *testing.T) {
b := New(time.Second*10, 1)
defer b.Destory()
assert.True(b.TryTake(1))
assert.False(b.TakeMaxDuration(1, time.Second*2))
})
t.Run("Should return true when get availible tokens before max duration", func(t *testing.T) {
start := time.Now()
b := New(time.Second*2, 1)
defer b.Destory()
assert.True(b.TryTake(1))
assert.True(b.TakeMaxDuration(1, time.Second*3))
assert.True(time.Now().Sub(start) < time.Second*3)
assert.True(time.Now().Sub(start) > time.Second*2)
assert.Equal(int64(0), b.avail)
})
t.Run("Should return immediately when have count tokens available using waitAndTake", func(t *testing.T) {
start := time.Now()
b := New(time.Second*2, 1)
defer b.Destory()
b.waitAndTake(1, 1)
assert.True(time.Now().Sub(start) < time.Second)
assert.Equal(int64(0), b.avail)
})
t.Run("Should return immediately when have count tokens available using waitAndTakeMaxDuration", func(t *testing.T) {
start := time.Now()
b := New(time.Second*2, 1)
defer b.Destory()
b.waitAndTakeMaxDuration(1, 0, time.Second*3)
assert.True(time.Now().Sub(start) < time.Second)
assert.Equal(int64(1), b.avail)
})
t.Run("Should wait until count tokens available", func(t *testing.T) {
start := time.Now()
b := New(time.Second*2, 1)
defer b.Destory()
assert.True(b.TryTake(1))
assert.False(b.TryTake(1))
b.Wait(1)
assert.True(time.Now().Sub(start) > time.Second)
assert.Equal(int64(1), b.avail)
})
t.Run("Should return false when wait reach max duration", func(t *testing.T) {
b := New(time.Second*10, 1)
defer b.Destory()
assert.True(b.TryTake(1))
assert.False(b.WaitMaxDuration(1, time.Second*2))
assert.Equal(int64(0), b.avail)
})
}