-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon_test.go
89 lines (85 loc) · 1.71 KB
/
daemon_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
package tokenbucket
import (
"testing"
"time"
"gotest.tools/v3/assert"
)
func Test_flag_has(t *testing.T) {
type args struct {
flag flag
}
tests := []struct {
name string
a flag
args args
want bool
}{
{
name: "Test single flag",
a: Retryable,
args: args{
flag: Retryable,
},
want: true,
},
{
name: "Test multiple flags",
a: Retryable | Forgiving,
args: args{
flag: Retryable,
},
want: true,
},
{
name: "Test flag not found",
a: Retryable,
args: args{
flag: Forgiving,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.a.has(tt.args.flag); got != tt.want {
t.Errorf("has() = %v, want %v", got, tt.want)
}
})
}
}
func TestDaemon_Hit(t *testing.T) {
t.Run("Test happy day successful", func(t *testing.T) {
bucket := NewBucket("Test Bucket", 2)
dm := NewDaemon(bucket, NA)
dm.Start()
// drain the bucket
assert.Assert(t, dm.Hit())
assert.Assert(t, dm.Hit())
// wait for it to fill
time.Sleep(time.Duration(1) * time.Second)
// drain the bucket again
assert.Assert(t, dm.Hit())
assert.Assert(t, dm.Hit())
dm.Stop()
})
t.Run("Test drain bucket and fail to get token", func(t *testing.T) {
bucket := NewBucket("Test Bucket", 2)
dm := NewDaemon(bucket, NA)
dm.Start()
// drain the bucket
assert.Assert(t, dm.Hit())
assert.Assert(t, dm.Hit())
assert.Assert(t, !dm.Hit())
dm.Stop()
})
t.Run("Test forgiving", func(t *testing.T) {
bucket := NewBucket("Test Bucket", 2)
dm := NewDaemon(bucket, Forgiving)
dm.Start()
// drain the bucket
assert.Assert(t, dm.Hit())
assert.Assert(t, dm.Hit())
assert.Assert(t, dm.Hit())
dm.Stop()
})
}