-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_pool_test.go
251 lines (219 loc) · 4.63 KB
/
worker_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
251
package workpool
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestWorkerPoolExitWhenNoWork(t *testing.T) {
numWorkers := 5
outputs := make(chan int, numWorkers)
worker := func(abort <-chan struct{}) bool {
outputs <- 1
return false
}
closer := func() {
close(outputs)
}
//pool := NewWithClose(numWorkers, worker, closer)
pool := &WorkPool{
Handler: worker,
Workers: numWorkers,
Close: closer,
}
start := time.Now()
pool.Run()
sum := 0
for result := range outputs {
sum += result
}
end := time.Now()
dur := end.Sub(start)
assert.Equal(t, numWorkers, sum)
assert.True(t, dur < 100*time.Millisecond)
}
func TestWorkerPoolWithWorkToDo(t *testing.T) {
numInputs := 100
numWorkers := 4
inputs := make(chan int, numInputs)
outputs := make(chan int, numInputs)
worker := func(abort <-chan struct{}) bool {
// This construct is the trickiest part
for i := range inputs {
outputs <- i
return true
}
return false
}
closer := func() {
close(outputs)
}
pool := WorkPool{
Handler: worker,
Workers: numWorkers,
Close: closer,
}
go func() {
for i := 0; i < 100; i++ {
inputs <- i + 1
}
close(inputs)
}()
start := time.Now()
pool.Run()
sum := 0
for result := range outputs {
sum += result
}
end := time.Now()
dur := end.Sub(start)
assert.Equal(t, 100*(100+1)/2, sum)
assert.True(t, dur < 100*time.Millisecond)
}
func TestConcurrency(t *testing.T) {
tests := []struct {
inputs int
workers int
sleep time.Duration
intervals int
}{
// inputs == workers, expect perfect concurrency for 1 interval
{
inputs: 5,
workers: 5,
sleep: 100 * time.Millisecond,
intervals: 1,
},
// inputs == workers + 1, expect 2 intervals as one worker processes two inputs
{
inputs: 6,
workers: 5,
sleep: 100 * time.Millisecond,
intervals: 2,
},
// a single worker processes inputs serially
{
inputs: 3,
workers: 1,
sleep: 100 * time.Millisecond,
intervals: 3,
},
// inputs == workers * 2, expect perfect concurrency for 2 intervals
{
inputs: 6,
workers: 3,
sleep: 100 * time.Millisecond,
intervals: 2,
},
}
for _, test := range tests {
numWorkers := test.workers
inputs := make(chan int, test.inputs)
outputs := make(chan int, test.inputs)
worker := func(abort <-chan struct{}) bool {
for i := range inputs {
// Simulate work time
time.Sleep(test.sleep)
outputs <- i
return true
}
return false
}
closer := func() {
close(outputs)
// not interested in outputs for this test
for len(outputs) > 0 {
<-outputs
}
}
// Configure pool
pool := &WorkPool{
Handler: worker,
Workers: numWorkers,
Close: closer,
}
// Initialize input channel.
for i := 0; i < test.inputs; i++ {
inputs <- 1
}
close(inputs)
// Process work
start := time.Now()
pool.Run()
assert.WithinDuration(t, start.Add(test.sleep*time.Duration(test.intervals)), time.Now(), 10*time.Millisecond)
}
}
// cleanup closes and drains the channel.
func cleanup(ch chan int) {
close(ch)
for len(ch) > 0 {
<-ch
}
}
// TestCancel ensures that the pool can be cancelled while processing work.
func TestCancel(t *testing.T) {
numWorkers := 1
numInputs := 100
inputs := make(chan int, numInputs)
outputs := make(chan int, numInputs)
worker := func(abort <-chan struct{}) bool {
for i := range inputs {
// Simulate work time
time.Sleep(100 * time.Microsecond)
outputs <- i
return true
}
return false
}
closer := func() {
close(outputs)
}
// Configure pool
pool := &WorkPool{
Handler: worker,
Workers: numWorkers,
Close: closer,
}
// Initialize input channel, don't close
for i := 0; i < numInputs; i++ {
inputs <- 1
}
// Cancel after enough time to process 10 inputs.
go func() {
time.Sleep(1 * time.Millisecond)
pool.Cancel()
}()
pool.Run()
cleanup(inputs)
processedCount := len(outputs)
for len(outputs) > 0 {
<-outputs
}
// Should not have been able to process everything
assert.Less(t, processedCount, numInputs)
}
// TestCancelWithOpenInputChannel ensures that the pool is gracefully stopped while workers are awaiting work.
func TestCancelWithOpenInputChannel(t *testing.T) {
numWorkers := 1
started := make(chan struct{})
worker := func(abort <-chan struct{}) bool {
close(started)
select {
case <-time.After(1 * time.Hour):
return true
case <-abort:
return false
}
return true
}
// Configure pool
pool := &WorkPool{
Workers: numWorkers,
Handler: worker,
}
// Wait for the worker to start, then cancel it.
go func() {
<-started
pool.Cancel()
}()
pool.Run()
}