-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathsubpool_test.go
336 lines (256 loc) · 7.22 KB
/
subpool_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
package pond
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/alitto/pond/v2/internal/assert"
)
func TestSubpool(t *testing.T) {
taskDuration := 10 * time.Millisecond
taskCount := 10
maxConcurrency := 5
pool := NewPool(10)
subpool := pool.NewSubpool(maxConcurrency)
subsubpool := subpool.NewSubpool(1)
completed := make(chan struct{})
var subpoolElapsedTime time.Duration
var subsubpoolElapsedTime time.Duration
go func() {
start := time.Now()
wg := sync.WaitGroup{}
wg.Add(taskCount)
for i := 0; i < taskCount; i++ {
subpool.Submit(func() {
time.Sleep(taskDuration)
wg.Done()
})
}
wg.Wait()
subpoolElapsedTime = time.Since(start)
completed <- struct{}{}
}()
go func() {
start := time.Now()
wg := sync.WaitGroup{}
wg.Add(taskCount)
for i := 0; i < taskCount; i++ {
subsubpool.Submit(func() {
time.Sleep(taskDuration)
wg.Done()
})
}
wg.Wait()
subsubpool.Stop().Wait()
subsubpoolElapsedTime = time.Since(start)
completed <- struct{}{}
}()
<-completed
<-completed
assert.True(t, subpoolElapsedTime >= time.Duration(taskCount/maxConcurrency)*taskDuration)
assert.True(t, subsubpoolElapsedTime >= time.Duration(taskCount/1)*taskDuration)
}
func TestSubpoolStopAndWait(t *testing.T) {
taskDuration := 1 * time.Microsecond
taskCount := 100000
maxConcurrency := 100
subpool := NewPool(1000).NewSubpool(maxConcurrency)
var executedCount atomic.Int64
for i := 0; i < taskCount; i++ {
subpool.Submit(func() {
time.Sleep(taskDuration)
executedCount.Add(1)
})
}
subpool.StopAndWait()
assert.Equal(t, int64(taskCount), executedCount.Load())
}
func TestSubpoolMetrics(t *testing.T) {
pool := NewPool(10)
subpool := pool.NewSubpool(5)
sampleErr := errors.New("sample error")
for i := 0; i < 20; i++ {
i := i
if i%2 == 0 {
subpool.SubmitErr(func() error {
if i%4 == 0 {
return sampleErr
}
return nil
})
} else {
pool.SubmitErr(func() error {
if i%3 == 0 {
return sampleErr
}
return nil
})
}
}
subpool.StopAndWait()
pool.StopAndWait()
assert.Equal(t, uint64(20), pool.SubmittedTasks())
assert.Equal(t, uint64(20), pool.CompletedTasks())
assert.Equal(t, uint64(8), pool.FailedTasks())
assert.Equal(t, uint64(12), pool.SuccessfulTasks())
assert.Equal(t, uint64(0), pool.WaitingTasks())
assert.Equal(t, uint64(10), subpool.SubmittedTasks())
assert.Equal(t, uint64(10), subpool.CompletedTasks())
assert.Equal(t, uint64(5), subpool.FailedTasks())
assert.Equal(t, uint64(5), subpool.SuccessfulTasks())
assert.Equal(t, uint64(0), subpool.WaitingTasks())
}
func TestSubpoolStop(t *testing.T) {
pool := NewPool(10)
subpool := pool.NewSubpool(5)
var executedCount atomic.Int64
subpool.Submit(func() {
executedCount.Add(1)
})
subpool.StopAndWait()
pool.Submit(func() {
executedCount.Add(1)
}).Wait()
assert.Equal(t, int64(2), executedCount.Load())
err := subpool.Submit(func() {}).Wait()
assert.Equal(t, ErrPoolStopped, err)
pool.StopAndWait()
err = pool.Submit(func() {}).Wait()
assert.Equal(t, ErrPoolStopped, err)
}
func TestSubpoolMaxConcurrency(t *testing.T) {
pool := NewPool(10)
assert.PanicsWithError(t, "maxConcurrency must be greater than 0", func() {
pool.NewSubpool(-1)
})
assert.PanicsWithError(t, "maxConcurrency cannot be greater than the parent pool's maxConcurrency (10)", func() {
pool.NewSubpool(11)
})
subpool := pool.NewSubpool(0)
assert.Equal(t, 10, subpool.MaxConcurrency())
}
func TestSubpoolStoppedAfterCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
pool := NewPool(10, WithContext(ctx))
subpool := pool.NewSubpool(5)
cancel()
time.Sleep(1 * time.Millisecond)
err := pool.Submit(func() {}).Wait()
assert.Equal(t, ErrPoolStopped, err)
err = subpool.Submit(func() {}).Wait()
assert.Equal(t, ErrPoolStopped, err)
}
func TestSubpoolWithDifferentLimits(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
pool := NewPool(7, WithContext(ctx))
subpool1 := pool.NewSubpool(1)
subpool2 := pool.NewSubpool(2)
subpool3 := pool.NewSubpool(3)
taskStarted := make(chan struct{}, 10)
taskWait := make(chan struct{})
var task = func() func() {
return func() {
taskStarted <- struct{}{}
<-taskWait
}
}
// Submit tasks to subpool1 and wait for 1 task to start
for i := 0; i < 10; i++ {
subpool1.Submit(task())
}
<-taskStarted
// Submit tasks to subpool2 and wait for 2 tasks to start
for i := 0; i < 10; i++ {
subpool2.Submit(task())
}
<-taskStarted
<-taskStarted
// Submit tasks to subpool3 and wait for 3 tasks to start
for i := 0; i < 10; i++ {
subpool3.Submit(task())
}
<-taskStarted
<-taskStarted
<-taskStarted
// Submit tasks to the main pool and wait for 1 to start
for i := 0; i < 10; i++ {
pool.Submit(task())
}
<-taskStarted
// Verify concurrency of each pool
assert.Equal(t, int64(1), subpool1.RunningWorkers())
assert.Equal(t, int64(2), subpool2.RunningWorkers())
assert.Equal(t, int64(3), subpool3.RunningWorkers())
assert.Equal(t, int64(7), pool.RunningWorkers())
assert.Equal(t, uint64(0), subpool1.CompletedTasks())
assert.Equal(t, uint64(0), subpool2.CompletedTasks())
assert.Equal(t, uint64(0), subpool3.CompletedTasks())
assert.Equal(t, uint64(0), pool.CompletedTasks())
// Cancel the context to abort pending tasks
cancel()
// Unblock all running tasks
close(taskWait)
subpool1.StopAndWait()
subpool2.StopAndWait()
subpool3.StopAndWait()
pool.StopAndWait()
assert.Equal(t, uint64(1), subpool1.CompletedTasks())
assert.Equal(t, uint64(2), subpool2.CompletedTasks())
assert.Equal(t, uint64(3), subpool3.CompletedTasks())
assert.Equal(t, uint64(7), pool.CompletedTasks())
}
func TestSubpoolWithOverlappingConcurrency(t *testing.T) {
taskDuration := 1 * time.Millisecond
pool := NewPool(1)
subpool := pool.NewSubpool(1)
var executedCount atomic.Int64
subpool.Submit(func() {
time.Sleep(taskDuration)
executedCount.Add(1)
assert.Equal(t, int64(1), pool.RunningWorkers())
assert.Equal(t, int64(1), subpool.RunningWorkers())
})
pool.Submit(func() {
time.Sleep(taskDuration)
executedCount.Add(1)
assert.Equal(t, int64(1), pool.RunningWorkers())
assert.Equal(t, int64(1), subpool.RunningWorkers())
})
subpool.Submit(func() {
time.Sleep(taskDuration)
executedCount.Add(1)
assert.Equal(t, int64(1), pool.RunningWorkers())
assert.Equal(t, int64(1), subpool.RunningWorkers())
})
subpool.StopAndWait()
pool.StopAndWait()
assert.Equal(t, int64(3), executedCount.Load())
}
func TestSubpoolWithQueueSizeOverride(t *testing.T) {
pool := NewPool(10, WithQueueSize(10))
subpool := pool.NewSubpool(1, WithQueueSize(2), WithNonBlocking(true))
taskStarted := make(chan struct{}, 10)
taskWait := make(chan struct{})
var task = func() func() {
return func() {
taskStarted <- struct{}{}
<-taskWait
}
}
// Submit tasks to subpool and wait for it to start
subpool.Submit(task())
<-taskStarted
// Submit more tasks to fill up the queue
for i := 0; i < 10; i++ {
subpool.Submit(task())
}
// 7 tasks should have been discarded
assert.Equal(t, int64(1), subpool.RunningWorkers())
assert.Equal(t, uint64(3), subpool.SubmittedTasks())
// Unblock all running tasks
close(taskWait)
subpool.StopAndWait()
pool.StopAndWait()
}