forked from gammazero/workerpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkerpool_test.go
561 lines (474 loc) · 10.8 KB
/
workerpool_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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package workerpool
import (
"sync"
"testing"
"time"
)
const max = 20
func TestExample(t *testing.T) {
wp := New(2)
requests := []string{"alpha", "beta", "gamma", "delta", "epsilon"}
rspChan := make(chan string, len(requests))
for _, r := range requests {
r := r
wp.Submit(func() {
rspChan <- r
})
}
wp.StopWait()
close(rspChan)
rspSet := map[string]struct{}{}
for rsp := range rspChan {
rspSet[rsp] = struct{}{}
}
if len(rspSet) < len(requests) {
t.Fatal("Did not handle all requests")
}
for _, req := range requests {
if _, ok := rspSet[req]; !ok {
t.Fatal("Missing expected values:", req)
}
}
}
func TestMaxWorkers(t *testing.T) {
t.Parallel()
wp := New(0)
if wp.maxWorkers != 1 {
t.Fatal("should have created one worker")
}
wp = New(max)
defer wp.Stop()
started := make(chan struct{}, max)
sync := make(chan struct{})
// Start workers, and have them all wait on a channel before completing.
for i := 0; i < max; i++ {
wp.Submit(func() {
started <- struct{}{}
<-sync
})
}
// Wait for all queued tasks to be dispatched to workers.
timeout := time.After(5 * time.Second)
if wp.waitingQueue.Len() != wp.WaitingQueueSize() {
t.Fatal("Working Queue size returned should not be 0")
panic("WRONG")
}
for startCount := 0; startCount < max; {
select {
case <-started:
startCount++
case <-timeout:
t.Fatal("timed out waiting for workers to start")
}
}
// Release workers.
close(sync)
}
func TestReuseWorkers(t *testing.T) {
t.Parallel()
wp := New(5)
defer wp.Stop()
sync := make(chan struct{})
// Cause worker to be created, and available for reuse before next task.
for i := 0; i < 10; i++ {
wp.Submit(func() { <-sync })
sync <- struct{}{}
time.Sleep(100 * time.Millisecond)
}
// If the same worker was always reused, then only one worker would have
// been created and there should only be one ready.
if countReady(wp) > 1 {
t.Fatal("Worker not reused")
}
}
func TestWorkerTimeout(t *testing.T) {
t.Parallel()
wp := New(max)
defer wp.Stop()
sync := make(chan struct{})
started := make(chan struct{}, max)
// Cause workers to be created. Workers wait on channel, keeping them busy
// and causing the worker pool to create more.
for i := 0; i < max; i++ {
wp.Submit(func() {
started <- struct{}{}
<-sync
})
}
// Wait for tasks to start.
for i := 0; i < max; i++ {
<-started
}
if anyReady(wp) {
t.Fatal("number of ready workers should be zero")
}
// Release workers.
close(sync)
if countReady(wp) != max {
t.Fatal("Expected", max, "ready workers")
}
// Check that a worker timed out.
time.Sleep((idleTimeoutSec + 1) * time.Second)
if countReady(wp) != max-1 {
t.Fatal("First worker did not timeout")
}
// Check that another worker timed out.
time.Sleep((idleTimeoutSec + 1) * time.Second)
if countReady(wp) != max-2 {
t.Fatal("Second worker did not timeout")
}
}
func TestStop(t *testing.T) {
t.Parallel()
wp := New(max)
defer wp.Stop()
started := make(chan struct{}, max)
sync := make(chan struct{})
// Start workers, and have them all wait on a channel before completing.
for i := 0; i < max; i++ {
wp.Submit(func() {
started <- struct{}{}
<-sync
})
}
// Wait for all queued tasks to be dispatched to workers.
timeout := time.After(5 * time.Second)
for startCount := 0; startCount < max; {
select {
case <-started:
startCount++
case <-timeout:
t.Fatal("timed out waiting for workers to start")
}
}
// Release workers.
close(sync)
if wp.Stopped() {
t.Error("pool should not be stopped")
}
wp.Stop()
if anyReady(wp) {
t.Error("should have zero workers after stop")
}
if !wp.Stopped() {
t.Error("pool should be stopped")
}
// Start workers, and have them all wait on a channel before completing.
wp = New(5)
sync = make(chan struct{})
finished := make(chan struct{}, max)
for i := 0; i < max; i++ {
wp.Submit(func() {
<-sync
finished <- struct{}{}
})
}
// Call Stop() and see that only the already running tasks were completed.
go func() {
time.Sleep(10000 * time.Millisecond)
close(sync)
}()
wp.Stop()
var count int
Count:
for count < max {
select {
case <-finished:
count++
default:
break Count
}
}
if count > 5 {
t.Error("Should not have completed any queued tasks, did", count)
}
// Check that calling Stop() againg is OK.
wp.Stop()
}
func TestStopWait(t *testing.T) {
t.Parallel()
// Start workers, and have them all wait on a channel before completing.
wp := New(5)
sync := make(chan struct{})
finished := make(chan struct{}, max)
for i := 0; i < max; i++ {
wp.Submit(func() {
<-sync
finished <- struct{}{}
})
}
// Call StopWait() and see that all tasks were completed.
go func() {
time.Sleep(10 * time.Millisecond)
close(sync)
}()
wp.StopWait()
for count := 0; count < max; count++ {
select {
case <-finished:
default:
t.Error("Should have completed all queued tasks")
}
}
if anyReady(wp) {
t.Error("should have zero workers after stopwait")
}
if !wp.Stopped() {
t.Error("pool should be stopped")
}
// Make sure that calling StopWait() with no queued tasks is OK.
wp = New(5)
wp.StopWait()
if anyReady(wp) {
t.Error("should have zero workers after stopwait")
}
// Check that calling StopWait() againg is OK.
wp.StopWait()
}
func TestSubmitWait(t *testing.T) {
wp := New(1)
defer wp.Stop()
// Check that these are noop.
wp.Submit(nil)
wp.SubmitWait(nil)
done1 := make(chan struct{})
wp.Submit(func() {
time.Sleep(100 * time.Millisecond)
close(done1)
})
select {
case <-done1:
t.Fatal("Submit did not return immediately")
default:
}
done2 := make(chan struct{})
wp.SubmitWait(func() {
time.Sleep(100 * time.Millisecond)
close(done2)
})
select {
case <-done2:
default:
t.Fatal("SubmitWait did not wait for function to execute")
}
}
func TestOverflow(t *testing.T) {
wp := New(2)
releaseChan := make(chan struct{})
// Start workers, and have them all wait on a channel before completing.
for i := 0; i < 64; i++ {
wp.Submit(func() { <-releaseChan })
}
// Start a goroutine to free the workers after calling stop. This way
// the dispatcher can exit, then when this goroutine runs, the workerpool
// can exit.
go func() {
<-time.After(time.Millisecond)
close(releaseChan)
}()
wp.Stop()
// Now that the worker pool has exited, it is safe to inspect its waiting
// queue without causing a race.
qlen := wp.waitingQueue.Len()
if qlen != 62 {
t.Fatal("Expected 62 tasks in waiting queue, have", qlen)
}
}
func TestStopRace(t *testing.T) {
wp := New(20)
releaseChan := make(chan struct{})
workRelChan := make(chan struct{})
// Start workers, and have them all wait on a channel before completing.
for i := 0; i < 20; i++ {
wp.Submit(func() { <-workRelChan })
}
time.Sleep(5 * time.Second)
for i := 0; i < 64; i++ {
go func() {
<-releaseChan
wp.Stop()
}()
}
close(workRelChan)
close(releaseChan)
}
// Run this test with race detector to test that using WaitingQueueSize has no
// race condition
func TestWaitingQueueSizeRace(t *testing.T) {
const (
goroutines = 10
tasks = 20
workers = 5
)
wp := New(workers)
maxChan := make(chan int)
for g := 0; g < goroutines; g++ {
go func() {
max := 0
// Submit 100 tasks, checking waiting queue size each time. Report
// the maximum queue size seen.
for i := 0; i < tasks; i++ {
wp.Submit(func() {
time.Sleep(time.Microsecond)
})
waiting := wp.WaitingQueueSize()
if waiting > max {
max = waiting
}
}
maxChan <- max
}()
}
// Find maximum queuesize seen by any thread.
maxMax := 0
for g := 0; g < goroutines; g++ {
max := <-maxChan
if max > maxMax {
maxMax = max
}
}
if maxMax == 0 {
t.Error("expected to see waiting queue size > 0")
}
if maxMax >= goroutines*tasks {
t.Error("should not have seen all tasks on waiting queue")
}
}
func anyReady(w *WorkerPool) bool {
select {
case wkCh := <-w.readyWorkers:
w.readyWorkers <- wkCh
return true
default:
}
return false
}
func countReady(w *WorkerPool) int {
// Try to pull max workers off of ready queue.
timeout := time.After(5 * time.Second)
readyTmp := make(chan chan func(), max)
var readyCount int
for i := 0; i < max; i++ {
select {
case wkCh := <-w.readyWorkers:
readyTmp <- wkCh
readyCount++
case <-timeout:
readyCount = i
i = max
}
}
// Restore ready workers.
close(readyTmp)
go func() {
for r := range readyTmp {
w.readyWorkers <- r
}
}()
return readyCount
}
/*
Run benchmarking with: go test -bench '.'
*/
func BenchmarkEnqueue(b *testing.B) {
wp := New(1)
defer wp.Stop()
releaseChan := make(chan struct{})
b.ResetTimer()
// Start workers, and have them all wait on a channel before completing.
for i := 0; i < b.N; i++ {
wp.Submit(func() { <-releaseChan })
}
close(releaseChan)
}
func BenchmarkEnqueue2(b *testing.B) {
wp := New(2)
defer wp.Stop()
releaseChan := make(chan struct{})
b.ResetTimer()
// Start workers, and have them all wait on a channel before completing.
for i := 0; i < b.N; i++ {
for i := 0; i < 64; i++ {
wp.Submit(func() { <-releaseChan })
}
for i := 0; i < 64; i++ {
releaseChan <- struct{}{}
}
}
close(releaseChan)
}
func BenchmarkExecute1Worker(b *testing.B) {
wp := New(1)
defer wp.Stop()
var allDone sync.WaitGroup
allDone.Add(b.N)
b.ResetTimer()
// Start workers, and have them all wait on a channel before completing.
for i := 0; i < b.N; i++ {
wp.Submit(func() {
time.Sleep(time.Millisecond)
allDone.Done()
})
}
allDone.Wait()
}
func BenchmarkExecute2Worker(b *testing.B) {
wp := New(2)
defer wp.Stop()
var allDone sync.WaitGroup
allDone.Add(b.N)
b.ResetTimer()
// Start workers, and have them all wait on a channel before completing.
for i := 0; i < b.N; i++ {
wp.Submit(func() {
time.Sleep(time.Millisecond)
allDone.Done()
})
}
allDone.Wait()
}
func BenchmarkExecute4Workers(b *testing.B) {
wp := New(4)
defer wp.Stop()
var allDone sync.WaitGroup
allDone.Add(b.N)
b.ResetTimer()
// Start workers, and have them all wait on a channel before completing.
for i := 0; i < b.N; i++ {
wp.Submit(func() {
time.Sleep(time.Millisecond)
allDone.Done()
})
}
allDone.Wait()
}
func BenchmarkExecute16Workers(b *testing.B) {
wp := New(16)
defer wp.Stop()
var allDone sync.WaitGroup
allDone.Add(b.N)
b.ResetTimer()
// Start workers, and have them all wait on a channel before completing.
for i := 0; i < b.N; i++ {
wp.Submit(func() {
time.Sleep(time.Millisecond)
allDone.Done()
})
}
allDone.Wait()
}
func BenchmarkExecute64Workers(b *testing.B) {
wp := New(64)
defer wp.Stop()
var allDone sync.WaitGroup
allDone.Add(b.N)
b.ResetTimer()
// Start workers, and have them all wait on a channel before completing.
for i := 0; i < b.N; i++ {
wp.Submit(func() {
time.Sleep(time.Millisecond)
allDone.Done()
})
}
allDone.Wait()
}