-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner_pool_test.go
250 lines (189 loc) · 5.79 KB
/
runner_pool_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package runnerpool
import (
"context"
"errors"
"testing"
"time"
"github.com/fortytw2/leaktest"
"github.com/stretchr/testify/assert"
)
func TestWorkerPool(t *testing.T) {
t.Run("performing parallel tasks", func(t *testing.T) {
defer leaktest.Check(t)()
cfg := Config{Workers: 2}
pool := New(cfg, goRunner)
err := pool.Start()
assert.NoError(t, err)
defer pool.Stop(context.Background())
ch1, ch2 := make(chan struct{}), make(chan struct{})
func() {
worker, err := pool.Worker(context.Background())
assert.NoError(t, err)
defer worker.Release()
worker.Run(func(_ context.Context) {
close(ch1)
<-ch2
})
}()
func() {
worker, err := pool.Worker(context.Background())
assert.NoError(t, err)
defer worker.Release()
worker.Run(func(_ context.Context) {
close(ch2)
<-ch1
})
}()
select {
case <-ch1:
case <-time.After(time.Second):
t.Errorf("ch1 should be closed")
}
select {
case <-ch2:
case <-time.After(time.Second):
t.Errorf("ch2 should be closed")
}
})
t.Run("worker's context is canceled when stopping", func(t *testing.T) {
defer leaktest.Check(t)()
cfg := Config{Workers: 1}
pool := New(cfg, goRunner)
err := pool.Start()
assert.NoError(t, err)
worker, err := pool.Worker(context.Background())
assert.NoError(t, err)
defer worker.Release()
workerStopped := make(chan struct{})
worker.Run(func(ctx context.Context) {
<-ctx.Done()
close(workerStopped)
})
err = pool.Stop(context.Background())
assert.NoError(t, err)
select {
case <-workerStopped:
case <-time.After(time.Second):
t.Errorf("Worker didn't stop when pool.Stop was called")
}
})
t.Run("stop returns a wrapped ctx.Err() when worker won't stop", func(t *testing.T) {
defer leaktest.Check(t)()
cfg := Config{Workers: 1}
pool := New(cfg, goRunner)
err := pool.Start()
assert.NoError(t, err)
worker, err := pool.Worker(context.Background())
assert.NoError(t, err)
defer worker.Release()
block := make(chan struct{})
defer close(block) // don't leak this after test has ended
worker.Run(func(_ context.Context) {
<-block
})
// this can be flaky letting broken code pass, but can't give false positives
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
err = pool.Stop(ctx)
assert.Error(t, err)
assert.True(t, errors.Is(err, context.DeadlineExceeded), "Error should wrap context.DeadlineExceeded, but it is: %s", err)
})
t.Run("rejecting when no workers are available", func(t *testing.T) {
defer leaktest.Check(t)()
cfg := Config{Workers: 1}
pool := New(cfg, goRunner)
err := pool.Start()
assert.NoError(t, err)
defer pool.Stop(context.Background())
unblock, unblocked := runBlockingWorker(t, pool)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
_, err = pool.Worker(ctx)
assert.NotNil(t, err)
assert.IsType(t, ErrCantAcquireWorker{}, err)
assert.True(t, errors.Is(err, context.DeadlineExceeded), "Error doesn't wrap cause correctly")
close(unblock)
<-unblocked
})
t.Run("releasing a worker without running anything", func(t *testing.T) {
defer leaktest.Check(t)()
cfg := Config{Workers: 1}
pool := New(cfg, goRunner)
err := pool.Start()
assert.NoError(t, err)
func() {
worker, err := pool.Worker(context.Background())
assert.NoError(t, err)
worker.Release()
}()
// just try to stop, if can't or there're leaked goroutines, then we failed releasing
pool.Stop(context.Background())
})
t.Run("stats", func(t *testing.T) {
defer leaktest.Check(t)()
cfg := Config{Workers: 10}
pool := New(cfg, goRunner)
err := pool.Start()
assert.NoError(t, err)
defer pool.Stop(context.Background())
unblockFirst, unblockedFirst := runBlockingWorker(t, pool)
unblockSecond, unblockedSecond := runBlockingWorker(t, pool)
acquiredWorker, err := pool.Worker(context.Background())
assert.NoError(t, err)
defer acquiredWorker.Release()
close(unblockFirst)
<-unblockedFirst
// let the numbers update, we can't know when counter is decreased after first worker has been freed
time.Sleep(100 * time.Millisecond)
assert.Equal(t, Stats{
MaxWorkers: int32(cfg.Workers),
Workers: 10, // all the workers are created
Acquired: 2, // second is running, and another one is just acquired
Running: 1, // only second is running now
}, pool.Stats())
close(unblockSecond)
<-unblockedSecond
})
t.Run("stats are okay even if we panic", func(t *testing.T) {
defer leaktest.Check(t)()
cfg := Config{Workers: 10}
pool := New(cfg, recoverFromPanics)
err := pool.Start()
assert.NoError(t, err)
defer pool.Stop(context.Background())
worker, err := pool.Worker(context.Background())
assert.NoError(t, err)
worker.Run(func(_ context.Context) { panic("roto, todo roto") })
worker.Release()
// let the numbers update, we can't know when counter is decreased after first worker has been freed
time.Sleep(100 * time.Millisecond)
assert.Equal(t, Stats{
MaxWorkers: int32(cfg.Workers),
Workers: int32(cfg.Workers), // panicked worker has been created again
Acquired: 0,
Running: 0,
}, pool.Stats())
})
}
func runBlockingWorker(t *testing.T, pool Pool) (unblock, unblocked chan struct{}) {
unblock = make(chan struct{})
unblocked = make(chan struct{})
func() {
worker, err := pool.Worker(context.Background())
assert.NoError(t, err)
defer worker.Release()
worker.Run(func(_ context.Context) {
<-unblock
close(unblocked)
})
}()
return unblock, unblocked
}
func goRunner(f func()) { go f() }
func recoverFromPanics(f func()) {
go func() {
// deferring recover() directly causes a warning in linters, IDEs, etc, so we defer an anonoymous function calling recover()
defer func() { recover() }()
f()
}()
}