forked from alitto/pond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pond_blackbox_test.go
412 lines (300 loc) · 8.15 KB
/
pond_blackbox_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
package pond_test
import (
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/alitto/pond"
)
func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
if expected != actual {
t.Helper()
t.Errorf("Expected %T(%v) but was %T(%v)", expected, expected, actual, actual)
}
}
func TestSubmitAndStopWaiting(t *testing.T) {
pool := pond.New(1, 5)
// Submit tasks
var doneCount int32
for i := 0; i < 17; i++ {
pool.Submit(func() {
time.Sleep(1 * time.Millisecond)
atomic.AddInt32(&doneCount, 1)
})
}
// Wait until all submitted tasks complete
pool.StopAndWait()
assertEqual(t, int32(17), atomic.LoadInt32(&doneCount))
}
func TestSubmitAndStopWaitingWithMoreWorkersThanTasks(t *testing.T) {
pool := pond.New(18, 5)
// Submit tasks
var doneCount int32
for i := 0; i < 17; i++ {
pool.Submit(func() {
time.Sleep(1 * time.Millisecond)
atomic.AddInt32(&doneCount, 1)
})
}
// Wait until all submitted tasks complete
pool.StopAndWait()
assertEqual(t, int32(17), atomic.LoadInt32(&doneCount))
}
func TestSubmitAndStopWithoutWaiting(t *testing.T) {
pool := pond.New(1, 5)
// Submit tasks
started := make(chan bool)
completed := make(chan bool)
var doneCount int32
for i := 0; i < 5; i++ {
pool.Submit(func() {
started <- true
time.Sleep(5 * time.Millisecond)
atomic.AddInt32(&doneCount, 1)
<-completed
})
}
// Make sure the first task started
<-started
// Stop without waiting for the rest of the tasks to start
pool.Stop()
// Let the first task complete now
completed <- true
// Only the first task should have been completed, the rest are discarded
assertEqual(t, int32(1), atomic.LoadInt32(&doneCount))
// Make sure the exit lines in the worker pool are executed and covered
time.Sleep(6 * time.Millisecond)
}
func TestSubmitWithNilTask(t *testing.T) {
pool := pond.New(2, 5)
// Submit nil task
pool.Submit(nil)
// Wait until all submitted tasks complete
pool.StopAndWait()
assertEqual(t, 0, pool.Running())
}
func TestSubmitAndWait(t *testing.T) {
pool := pond.New(1, 5)
defer pool.StopAndWait()
// Submit a task and wait for it to complete
var doneCount int32
pool.SubmitAndWait(func() {
time.Sleep(5 * time.Millisecond)
atomic.AddInt32(&doneCount, 1)
})
assertEqual(t, int32(1), atomic.LoadInt32(&doneCount))
}
func TestSubmitAndWaitWithNilTask(t *testing.T) {
pool := pond.New(2, 5)
// Submit nil task
pool.SubmitAndWait(nil)
// Wait until all submitted tasks complete
pool.StopAndWait()
assertEqual(t, 0, pool.Running())
}
func TestSubmitBefore(t *testing.T) {
pool := pond.New(1, 5)
// Submit a long-running task
var doneCount int32
pool.SubmitBefore(func() {
time.Sleep(5 * time.Millisecond)
atomic.AddInt32(&doneCount, 1)
}, 1*time.Millisecond)
// Submit a task that times out after 2ms
pool.SubmitBefore(func() {
time.Sleep(5 * time.Millisecond)
atomic.AddInt32(&doneCount, 1)
}, 2*time.Millisecond)
pool.StopAndWait()
// Only the first task must have executed
assertEqual(t, int32(1), atomic.LoadInt32(&doneCount))
}
func TestSubmitBeforeWithNilTask(t *testing.T) {
pool := pond.New(3, 5)
// Submit nil task
pool.SubmitBefore(nil, 1*time.Millisecond)
// Wait until all submitted tasks complete
pool.StopAndWait()
assertEqual(t, 0, pool.Running())
}
func TestTrySubmit(t *testing.T) {
pool := pond.New(1, 5)
// Submit a long-running task
var doneCount int32
pool.Submit(func() {
time.Sleep(5 * time.Millisecond)
atomic.AddInt32(&doneCount, 1)
})
// Attempt to submit a task without blocking
dispatched := pool.TrySubmit(func() {
time.Sleep(5 * time.Millisecond)
atomic.AddInt32(&doneCount, 1)
})
// Task was not dispatched because the pool was full
assertEqual(t, false, dispatched)
pool.StopAndWait()
// Only the first task must have executed
assertEqual(t, int32(1), atomic.LoadInt32(&doneCount))
}
func TestSubmitToIdle(t *testing.T) {
pool := pond.New(1, 5)
// Submit a task and wait for it to complete
pool.SubmitAndWait(func() {
time.Sleep(1 * time.Millisecond)
})
assertEqual(t, int(1), pool.Running())
assertEqual(t, int(1), pool.Idle())
// Submit another task (this one should go to the idle worker)
pool.SubmitAndWait(func() {
time.Sleep(1 * time.Millisecond)
})
pool.StopAndWait()
assertEqual(t, int(0), pool.Running())
assertEqual(t, int(0), pool.Idle())
}
func TestRunning(t *testing.T) {
workerCount := 5
taskCount := 10
pool := pond.New(workerCount, taskCount)
assertEqual(t, 0, pool.Running())
// Submit tasks
var started = make(chan struct{}, workerCount)
var completed = make(chan struct{}, workerCount)
for i := 0; i < taskCount; i++ {
pool.Submit(func() {
started <- struct{}{}
time.Sleep(1 * time.Millisecond)
<-completed
})
}
// Wait until half the tasks have started
for i := 0; i < taskCount/2; i++ {
<-started
}
assertEqual(t, workerCount, pool.Running())
time.Sleep(1 * time.Millisecond)
// Make sure half the tasks tasks complete
for i := 0; i < taskCount/2; i++ {
completed <- struct{}{}
}
// Wait until the rest of the tasks have started
for i := 0; i < taskCount/2; i++ {
<-started
}
// Make sure all tasks complete
for i := 0; i < taskCount/2; i++ {
completed <- struct{}{}
}
pool.StopAndWait()
assertEqual(t, 0, pool.Running())
}
func TestSubmitWithPanic(t *testing.T) {
pool := pond.New(1, 5)
assertEqual(t, 0, pool.Running())
// Submit a task that panics
var doneCount int32
pool.Submit(func() {
arr := make([]string, 0)
fmt.Printf("Out of range value: %s", arr[1])
atomic.AddInt32(&doneCount, 1)
})
// Submit a task that completes normally
pool.Submit(func() {
time.Sleep(2 * time.Millisecond)
atomic.AddInt32(&doneCount, 1)
})
pool.StopAndWait()
assertEqual(t, 0, pool.Running())
assertEqual(t, int32(1), atomic.LoadInt32(&doneCount))
}
func TestPoolWithCustomIdleTimeout(t *testing.T) {
pool := pond.New(1, 5, pond.IdleTimeout(1*time.Millisecond))
// Submit a task
started := make(chan bool)
completed := make(chan bool)
pool.Submit(func() {
<-started
time.Sleep(3 * time.Millisecond)
<-completed
})
// Make sure the first task has started
started <- true
// There should be 1 worker running
assertEqual(t, 1, pool.Running())
// Let the task complete
completed <- true
// Wait for some time
time.Sleep(10 * time.Millisecond)
// Worker should have been killed
assertEqual(t, 0, pool.Running())
pool.StopAndWait()
}
func TestPoolWithCustomPanicHandler(t *testing.T) {
var capturedPanic interface{} = nil
panicHandler := func(panic interface{}) {
capturedPanic = panic
}
pool := pond.New(1, 5, pond.PanicHandler(panicHandler))
// Submit a task that panics
pool.Submit(func() {
panic("panic now!")
})
pool.StopAndWait()
// Panic should have been captured
assertEqual(t, "panic now!", capturedPanic)
}
func TestPoolWithCustomMinWorkers(t *testing.T) {
pool := pond.New(10, 5, pond.MinWorkers(10))
// Submit a task that panics
started := make(chan struct{})
completed := make(chan struct{})
pool.Submit(func() {
<-started
completed <- struct{}{}
})
started <- struct{}{}
// 10 workers should have been started
assertEqual(t, 10, pool.Running())
<-completed
pool.StopAndWait()
assertEqual(t, 0, pool.Running())
}
func TestGroupSubmit(t *testing.T) {
pool := pond.New(5, 1000)
assertEqual(t, 0, pool.Running())
// Submit groups of tasks
var doneCount, taskCount int32
var groups []*pond.TaskGroup
for i := 0; i < 5; i++ {
group := pool.Group()
for j := 0; j < i+5; j++ {
group.Submit(func() {
time.Sleep(1 * time.Millisecond)
atomic.AddInt32(&doneCount, 1)
})
taskCount++
}
groups = append(groups, group)
}
// Wait for all groups to complete
for _, group := range groups {
group.Wait()
}
assertEqual(t, int32(taskCount), atomic.LoadInt32(&doneCount))
}
func TestPoolWithCustomStrategy(t *testing.T) {
pool := pond.New(3, 3, pond.Strategy(pond.RatedResizer(2)))
// Submit 3 tasks
group := pool.Group()
for i := 0; i < 3; i++ {
group.Submit(func() {
time.Sleep(10 * time.Millisecond)
})
}
// Wait for them to complete
group.Wait()
// 2 workers should have been started
assertEqual(t, 2, pool.Running())
pool.StopAndWait()
assertEqual(t, 0, pool.Running())
}