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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
// Copyright 2024 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package dispatcher
import (
"context"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestPoolDefault(t *testing.T) {
count := 3
pool := makePool(count)
mgr := NewPool[*testInstance](
count,
func(idx int) (*testInstance, error) {
pool[idx].reset()
return &pool[idx], nil
},
func(ctx context.Context, inst *testInstance, _ UpdateInfo) {
pool[inst.Index()].run(ctx)
},
)
ctx, cancel := context.WithCancel(context.Background())
done := make(chan bool)
go func() {
mgr.Loop(ctx)
close(done)
}()
// Eventually all instances are up and busy.
for i := 0; i < count; i++ {
pool[i].waitRun()
}
// The pool restarts failed jobs.
for i := 0; i < 10; i++ {
pool[0].stopRun()
pool[2].stopRun()
pool[0].waitRun()
pool[2].waitRun()
}
cancel()
<-done
}
func TestPoolSplit(t *testing.T) {
count := 3
pool := makePool(count)
var defaultCount atomic.Int64
mgr := NewPool[*testInstance](
count,
func(idx int) (*testInstance, error) {
pool[idx].reset()
return &pool[idx], nil
},
func(ctx context.Context, inst *testInstance, _ UpdateInfo) {
defaultCount.Add(1)
pool[inst.Index()].run(ctx)
defaultCount.Add(-1)
},
)
done := make(chan bool)
ctx, cancel := context.WithCancel(context.Background())
go func() {
mgr.Loop(ctx)
close(done)
}()
startedRuns := make(chan bool)
stopRuns := make(chan bool)
job := func(ctx context.Context, _ *testInstance, _ UpdateInfo) {
startedRuns <- true
select {
case <-ctx.Done():
case <-stopRuns:
}
}
go mgr.Run(ctx, job)
// So far, there are no reserved instances.
for i := 0; i < count; i++ {
pool[i].waitRun()
}
// Dedicate one instance to the pool.
mgr.ReserveForRun(1)
// The first job must start.
<-startedRuns
// Two default jobs are running.
assert.EqualValues(t, 2, defaultCount.Load())
stopRuns <- true
// Take away the pool instance.
mgr.ReserveForRun(0)
// All instances must be busy with the default jobs.
for i := 0; i < count; i++ {
pool[i].waitRun()
}
assert.EqualValues(t, 3, defaultCount.Load())
// Now let's create and finish more jobs.
for i := 0; i < 10; i++ {
go mgr.Run(ctx, job)
}
mgr.ReserveForRun(2)
for i := 0; i < 10; i++ {
<-startedRuns
stopRuns <- true
}
cancel()
<-done
}
func TestPoolStress(t *testing.T) {
// The test to aid the race detector.
mgr := NewPool[*nilInstance](
10,
func(idx int) (*nilInstance, error) {
return &nilInstance{}, nil
},
func(ctx context.Context, _ *nilInstance, _ UpdateInfo) {
<-ctx.Done()
},
)
done := make(chan bool)
ctx, cancel := context.WithCancel(context.Background())
go func() {
mgr.Loop(ctx)
close(done)
}()
go func() {
for i := 0; i < 128; i++ {
mgr.TogglePause(i%2 == 0)
runtime.Gosched()
}
}()
for i := 0; i < 128; i++ {
go mgr.Run(ctx, func(ctx context.Context, _ *nilInstance, _ UpdateInfo) {})
mgr.ReserveForRun(5 + i%5)
}
cancel()
<-done
}
func TestPoolNewDefault(t *testing.T) {
var originalCount atomic.Int64
// The test to aid the race detector.
mgr := NewPool[*nilInstance](
10,
func(idx int) (*nilInstance, error) {
return &nilInstance{}, nil
},
func(ctx context.Context, _ *nilInstance, _ UpdateInfo) {
originalCount.Add(1)
<-ctx.Done()
originalCount.Add(-1)
},
)
done := make(chan bool)
ctx, cancel := context.WithCancel(context.Background())
go func() {
mgr.Loop(ctx)
close(done)
}()
for originalCount.Load() != 10 {
time.Sleep(time.Second / 10)
}
var newCount atomic.Int64
mgr.SetDefault(func(ctx context.Context, _ *nilInstance, _ UpdateInfo) {
newCount.Add(1)
<-ctx.Done()
newCount.Add(-1)
})
for newCount.Load() != 10 {
time.Sleep(time.Second / 10)
}
assert.Equal(t, int64(0), originalCount.Load())
cancel()
<-done
}
func TestPoolPause(t *testing.T) {
mgr := NewPool[*nilInstance](
10,
func(idx int) (*nilInstance, error) {
return &nilInstance{}, nil
},
func(ctx context.Context, _ *nilInstance, _ UpdateInfo) {
t.Fatal("must not be called")
},
)
mgr.ReserveForRun(10)
mgr.TogglePause(true)
done := make(chan bool)
ctx, cancel := context.WithCancel(context.Background())
go func() {
mgr.Loop(ctx)
close(done)
}()
run := make(chan bool, 1)
go mgr.Run(ctx, func(ctx context.Context, _ *nilInstance, _ UpdateInfo) {
run <- true
})
time.Sleep(10 * time.Millisecond)
if len(run) != 0 {
t.Fatalf("job run while paused")
}
mgr.TogglePause(false)
<-run
mgr.Run(ctx, func(ctx context.Context, _ *nilInstance, _ UpdateInfo) {})
cancel()
<-done
}
func TestPoolCancelRun(t *testing.T) {
// The test to aid the race detector.
mgr := NewPool[*nilInstance](
10,
func(idx int) (*nilInstance, error) {
return &nilInstance{}, nil
},
func(ctx context.Context, _ *nilInstance, _ UpdateInfo) {
<-ctx.Done()
},
)
var wg sync.WaitGroup
wg.Add(1)
ctx, cancel := context.WithCancel(context.Background())
go func() {
mgr.Loop(ctx)
wg.Done()
}()
mgr.ReserveForRun(2)
started := make(chan struct{})
// Schedule more jobs than could be processed simultaneously.
for i := 0; i < 15; i++ {
wg.Add(1)
go func() {
defer wg.Done()
mgr.Run(ctx, func(ctx context.Context, _ *nilInstance, _ UpdateInfo) {
select {
case <-ctx.Done():
return
case started <- struct{}{}:
}
<-ctx.Done()
})
}()
}
// Two can be started.
<-started
<-started
// Now stop the loop and the jbos.
cancel()
// Everything must really stop.
wg.Wait()
}
func makePool(count int) []testInstance {
var ret []testInstance
for i := 0; i < count; i++ {
ret = append(ret, testInstance{index: i})
}
return ret
}
type testInstance struct {
index int
hasRun atomic.Bool
stop chan bool
}
func (ti *testInstance) reset() {
ti.stop = make(chan bool)
ti.hasRun.Store(false)
}
func (ti *testInstance) run(ctx context.Context) {
ti.hasRun.Store(true)
select {
case <-ti.stop:
case <-ctx.Done():
}
}
func (ti *testInstance) waitRun() {
for !ti.hasRun.Load() {
time.Sleep(10 * time.Millisecond)
}
}
func (ti *testInstance) stopRun() {
close(ti.stop)
ti.hasRun.Store(false) // make subsequent waitRun() actually wait for the next command.
}
func (ti *testInstance) Index() int {
return ti.index
}
func (ti *testInstance) Close() error {
return nil
}
type nilInstance struct {
}
func (ni *nilInstance) Close() error {
return nil
}
|