-
Notifications
You must be signed in to change notification settings - Fork 0
/
waiter_test.go
232 lines (202 loc) · 5.02 KB
/
waiter_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package concurrent_test
import (
"context"
"sync"
"testing"
"time"
"github.com/knzm/go-concurrent"
)
type timeoutError struct{}
func (e timeoutError) Error() string { return "timeout" }
func (e timeoutError) IsTimeout() bool { return true }
func isTimeout(err error) bool {
terr, ok := err.(timeoutError)
return ok && terr.IsTimeout()
}
func acquireWithTimeout(ctx context.Context, waiter concurrent.Waiter, timeout time.Duration) error {
ch := make(chan error)
defer close(ch)
go func() {
defer func() {
if err := recover(); err != nil {
// ignore error on closed channel
}
}()
ch <- waiter.Acquire(ctx)
}()
select {
case err := <-ch:
return err
case <-time.After(timeout):
return timeoutError{}
}
}
func releaseWithTimeout(ctx context.Context, waiter concurrent.Waiter, timeout time.Duration) error {
ch := make(chan struct{})
defer close(ch)
go func() {
defer func() {
if err := recover(); err != nil {
// ignore error on closed channel
}
}()
waiter.Release()
ch <- struct{}{}
}()
select {
case <-ch:
return nil
case <-time.After(timeout):
return timeoutError{}
}
}
func TestWaiter(t *testing.T) {
const timeThresholdForBlocking = time.Millisecond
t.Run("non blocking", func(t *testing.T) {
ctx := context.Background()
waiter := concurrent.NewWaiter(3)
// waiter.Acquire() should not be blocked
err := acquireWithTimeout(ctx, waiter, timeThresholdForBlocking)
if err != nil {
if isTimeout(err) {
t.Error("Blocking Acquire()")
} else {
t.Errorf("Expected no error, got %v", err)
}
}
})
t.Run("blocking", func(t *testing.T) {
ctx := context.Background()
waiter := concurrent.NewWaiter(3)
for i := 0; i < 3; i++ {
err := acquireWithTimeout(ctx, waiter, timeThresholdForBlocking)
if err != nil {
if isTimeout(err) {
t.Errorf("[%d] Blocking Acquire()", i)
} else {
t.Errorf("[%d] Expected no error, got %v", i, err)
}
return
}
}
// waiter.Acquire() should be blocked
err := acquireWithTimeout(ctx, waiter, timeThresholdForBlocking)
if err == nil {
t.Error("Non-blocking Acquire()")
} else if !isTimeout(err) {
t.Errorf("[%d] Expected no error, got %v", 3, err)
}
})
t.Run("timeout", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
waiter := concurrent.NewWaiter(3)
for i := 0; i < 3; i++ {
err := acquireWithTimeout(ctx, waiter, timeThresholdForBlocking)
if err != nil {
if isTimeout(err) {
t.Errorf("[%d] Blocking Acquire()", i)
} else {
t.Errorf("[%d] Expected no error, got %v", i, err)
}
return
}
}
// waiter.Acquire() should be timed out after 100ms
err := acquireWithTimeout(ctx, waiter, time.Second)
if err == nil {
t.Error("Non-blocking Acquire()")
} else if isTimeout(err) {
// timeout without context
t.Error("Expected timeout")
} else if err.Error() != context.DeadlineExceeded.Error() {
t.Errorf("[%d] Expected no error, got %v", 3, err)
}
})
t.Run("acquire and release", func(t *testing.T) {
ctx := context.Background()
waiter := concurrent.NewWaiter(3)
for i := 0; i < 5; i++ {
// waiter.Acquire() should not be blocked
err := acquireWithTimeout(ctx, waiter, timeThresholdForBlocking)
if err != nil {
if isTimeout(err) {
t.Errorf("[%d] blocking Acquire()", i)
} else {
t.Errorf("[%d] Expected no error, got %v", i, err)
}
return
}
// waiter.Release() should not be blocked
err = releaseWithTimeout(ctx, waiter, timeThresholdForBlocking)
if err != nil {
if isTimeout(err) {
t.Errorf("[%d] blocking Release()", i)
} else {
t.Errorf("[%d] Expected no error, got %v", i, err)
}
return
}
}
})
t.Run("slow worker", func(t *testing.T) {
var mu sync.Mutex
var alive, maxAlive int
workerStarted := func() {
mu.Lock()
defer mu.Unlock()
alive++
if alive > maxAlive {
maxAlive = alive
}
}
workerFinished := func() {
mu.Lock()
defer mu.Unlock()
alive--
}
worker := func(ctx context.Context, waiter concurrent.Waiter) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
err := waiter.Acquire(ctx)
if err != nil {
return err
}
ch := make(chan struct{})
go func() {
defer waiter.Release()
workerStarted()
// do the heavy task
time.Sleep(100 * time.Millisecond)
workerFinished()
ch <- struct{}{}
}()
select {
case <-ctx.Done():
return ctx.Err()
case <-ch:
return nil
}
}
const numThreads = 10
const numJobs = 100
waiter := concurrent.NewWaiter(numThreads)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ch := make(chan error)
for i := 0; i < numJobs; i++ {
go func() {
ch <- worker(ctx, waiter)
}()
}
for i := 0; i < numJobs; i++ {
err := <-ch
if err != nil {
t.Errorf("[%d] Expected no error, got %v", i, err)
}
}
if maxAlive > numThreads {
t.Errorf("maxAlive is greater than expected: %d", maxAlive)
}
})
}