-
Notifications
You must be signed in to change notification settings - Fork 3
/
codel_test.go
153 lines (122 loc) · 2.59 KB
/
codel_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package codel
import (
"context"
"math/rand"
"testing"
"time"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func Example() {
c := New(Options{
// The maximum number of pending acquires
MaxPending: 100,
// The maximum number of concurrent acquires
MaxOutstanding: 10,
// The target latency to wait for an acquire.
// Acquires that take longer than this can fail.
TargetLatency: 5 * time.Millisecond,
})
// Attempt to acquire the lock.
err := c.Acquire(context.Background())
// if err is not nil, acquisition failed.
if err != nil {
return
}
// If acquisition succeeded, we need to release it.
defer c.Release()
// Do some process with external resources
}
func TestLock(t *testing.T) {
c := New(Options{
MaxPending: 1,
MaxOutstanding: 1,
TargetLatency: 5 * time.Millisecond,
})
err := c.Acquire(context.Background())
if err != nil {
t.Error("Got an error:", err)
}
c.Release()
}
func TestAcquireFailsForCanceledContext(t *testing.T) {
c := New(Options{
MaxPending: 1,
MaxOutstanding: 0,
TargetLatency: 5 * time.Millisecond,
})
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := c.Acquire(ctx)
if err == nil {
t.Error("Expected an error:", err)
c.Release()
}
}
func TestLockCanHaveMultiple(t *testing.T) {
const concurrent = 4
c := New(Options{
MaxPending: 1,
MaxOutstanding: concurrent,
TargetLatency: 5 * time.Millisecond,
})
ctx := context.Background()
for i := 0; i < concurrent; i++ {
err := c.Acquire(ctx)
if err != nil {
t.Error("Got an error:", err)
return
}
}
for i := 0; i < concurrent; i++ {
c.Release()
}
}
func BenchmarkLockUnblocked(b *testing.B) {
c := New(Options{
MaxPending: 1,
MaxOutstanding: 1,
TargetLatency: 5 * time.Millisecond,
})
ctx := context.Background()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := c.Acquire(ctx)
if err != nil {
b.Log("Got an error:", err)
return
}
c.Release()
}
b.StopTimer()
}
func BenchmarkLockBlocked(b *testing.B) {
const concurrent = 4
c := New(Options{
MaxPending: 1,
MaxOutstanding: concurrent,
TargetLatency: 5 * time.Millisecond,
})
ctx := context.Background()
// Acquire maximum outstanding to avoid fast path
for i := 0; i < concurrent; i++ {
err := c.Acquire(ctx)
if err != nil {
b.Error("Got an error:", err)
return
}
}
b.ResetTimer()
// Race the release and the acquire in order to benchmark slow path
for i := 0; i < b.N; i++ {
go func() {
c.Release()
}()
err := c.Acquire(ctx)
if err != nil {
b.Log("Got an error:", err)
return
}
}
}