-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_test.go
314 lines (291 loc) · 9.21 KB
/
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
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
package workerpool
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestPool_GivenNumberWorkers_WhenNewFixedSize_ShouldInitFieldsCorrectly(t *testing.T) {
numWorkers := 3
pool := NewFixedSize(numWorkers)
assert.Equal(t, FixedSize, pool.option.Mode)
assert.Equal(t, numWorkers, pool.option.NumberWorkers)
assert.Equal(t, numWorkers*defaultCapacityRatio, pool.option.Capacity)
assert.Equal(t, numWorkers*defaultCapacityRatio, pool.Capacity())
assert.NotNil(t, pool.option.LogFunc)
assert.Equal(t, pool.option.Capacity, cap(pool.jobQueue))
assert.Len(t, pool.Workers(), 0)
assert.Equal(t, 0, pool.SubmittedJobs())
assert.Equal(t, 0, pool.AssignedJobs())
}
func TestPool_GivenAnOption_WhenNew_ShouldInitFieldsCorrectly(t *testing.T) {
opt := Option{
Mode: FlexibleSize,
Capacity: 1000,
NumberWorkers: 4,
LogFunc: func(msgFormat string, args ...interface{}) {},
}
pool := New(&opt)
assert.Equal(t, opt.Mode, pool.option.Mode)
assert.Equal(t, opt.NumberWorkers, pool.option.NumberWorkers)
assert.Equal(t, opt.Capacity, pool.option.Capacity)
assert.Equal(t, opt.Capacity, pool.Capacity())
assert.NotNil(t, pool.option.LogFunc)
assert.Equal(t, pool.option.Capacity, cap(pool.jobQueue))
assert.Len(t, pool.Workers(), 0)
assert.Equal(t, 0, pool.SubmittedJobs())
assert.Equal(t, 0, pool.AssignedJobs())
}
func TestPool_GivenNegativeOption_WhenNew_ShouldInitWithFallbackValues(t *testing.T) {
opt := Option{
Mode: FlexibleSize,
Capacity: -100,
NumberWorkers: -4,
LogFunc: func(msgFormat string, args ...interface{}) {},
}
pool := New(&opt)
assert.Equal(t, opt.Mode, pool.option.Mode)
assert.Equal(t, defaultNumberWorkers, pool.option.NumberWorkers)
assert.Equal(t, defaultNumberWorkers*defaultCapacityRatio, pool.option.Capacity)
assert.Equal(t, defaultNumberWorkers*defaultCapacityRatio, pool.Capacity())
assert.NotNil(t, pool.option.LogFunc)
assert.Equal(t, pool.option.Capacity, cap(pool.jobQueue))
assert.Len(t, pool.Workers(), 0)
assert.Equal(t, 0, pool.SubmittedJobs())
assert.Equal(t, 0, pool.AssignedJobs())
}
func startDummyPoolFixedSize(t *testing.T, numberWorkers int, capacity int) *Pool {
pool := NewFixedSize(numberWorkers, WithCapacity(capacity))
pool.Start()
assert.Equal(t, FixedSize, pool.option.Mode)
assert.Equal(t, numberWorkers, pool.option.NumberWorkers)
assert.Equal(t, capacity, pool.option.Capacity)
assert.Equal(t, capacity, pool.Capacity())
assert.NotNil(t, pool.option.LogFunc)
assert.Len(t, pool.Workers(), numberWorkers)
return pool
}
func TestPool_GivenAPoolFixedSize_WhenSubmitAFuncJob_ShouldRunCorrectly(t *testing.T) {
pool := startDummyPoolFixedSize(t, 2, 2)
run := false
go pool.SubmitFunc(func() {
run = true
pool.option.LogFunc("Func job is finished")
})
time.Sleep(20 * time.Millisecond)
assert.True(t, run)
assert.Equal(t, 1, pool.SubmittedJobs())
assert.Equal(t, 1, pool.AssignedJobs())
assert.Len(t, pool.jobQueue, 0)
}
func TestPool_GivenAPoolFixedSize_WhenSubmitJob_ShouldRunCorrectly(t *testing.T) {
pool := startDummyPoolFixedSize(t, 2, 2)
job1 := 0
go pool.Submit(NewCustomIdentifierJob("1", func() {
job1 = 1
pool.option.LogFunc("Job 1 is finished")
}))
time.Sleep(20 * time.Millisecond)
assert.Equal(t, 1, job1)
assert.Equal(t, 1, pool.SubmittedJobs())
assert.Equal(t, 1, pool.AssignedJobs())
assert.Len(t, pool.jobQueue, 0)
// Job 2, 3 will be assigned to workers (1, 2)
job2 := 0
pool.Submit(NewCustomIdentifierJob("2", func() {
job2++
time.Sleep(200 * time.Millisecond)
pool.option.LogFunc("Job 2 is finished")
job2++
}))
job3 := 0
pool.Submit(NewCustomIdentifierJob("3", func() {
job3++
time.Sleep(200 * time.Millisecond)
pool.option.LogFunc("Job 3 is finished")
job3++
}))
// Job 4, 5 will be added to job queue
// When all worker are busy, job 4 or 5 will be wait in Idle holding point (See Pool.Start function)
job4 := 0
pool.Submit(NewCustomIdentifierJob("4", func() {
job4++
time.Sleep(200 * time.Millisecond)
pool.option.LogFunc("Job 4 is finished")
job4++
}))
job5 := 0
pool.Submit(NewCustomIdentifierJob("5", func() {
job5++
time.Sleep(200 * time.Millisecond)
pool.option.LogFunc("Job 5 is finished")
job5++
}))
// Job 6 will be added to the queue even though capacity=2 due by Idle holding point (See Pool.Start function)
job6 := 0
pool.Submit(NewCustomIdentifierJob("6", func() {
job6++
time.Sleep(200 * time.Millisecond)
pool.option.LogFunc("Job 6 is finished")
job6++
}))
// Job 7 will be hang due by job queue's capacity is full now
job7 := 0
job7InQueue := false
go func() {
pool.Submit(NewCustomIdentifierJob("7", func() {
job7++
time.Sleep(200 * time.Millisecond)
pool.option.LogFunc("Job 7 is finished")
job7++
}))
job7InQueue = true
}()
pool.option.LogFunc("Submitted all jobs")
time.Sleep(50 * time.Millisecond)
// Job 2, 3 is processing
// Job 4 in Idle holding point, job 5, 6 in queue
// Job 7 is hanged
assert.Equal(t, 1, job2)
assert.Equal(t, 1, job3)
assert.Equal(t, 0, job4)
assert.Equal(t, 0, job5)
assert.Equal(t, 0, job6)
assert.Equal(t, 0, job7)
assert.False(t, job7InQueue)
assert.Len(t, pool.jobQueue, 2)
assert.Equal(t, 7, pool.SubmittedJobs())
assert.Equal(t, 3, pool.AssignedJobs())
time.Sleep(200 * time.Millisecond)
// Job 2, 3 is finished
// Job 4, 5 is processing
// Job 6 in Idle holding point
// Job 7 in queue
assert.Equal(t, 2, job2)
assert.Equal(t, 2, job3)
assert.Equal(t, 1, job4)
assert.Equal(t, 1, job5)
assert.Equal(t, 0, job6)
assert.Equal(t, 0, job7)
assert.True(t, job7InQueue)
assert.Len(t, pool.jobQueue, 1)
assert.Equal(t, 7, pool.SubmittedJobs())
assert.Equal(t, 5, pool.AssignedJobs())
time.Sleep(200 * time.Millisecond)
// Job 2, 3, 4, 5 is finished
// Job 6, 7 is processing
assert.Equal(t, 2, job2)
assert.Equal(t, 2, job3)
assert.Equal(t, 2, job4)
assert.Equal(t, 2, job5)
assert.Equal(t, 1, job6)
assert.Equal(t, 1, job7)
assert.Len(t, pool.jobQueue, 0)
assert.Equal(t, 7, pool.SubmittedJobs())
assert.Equal(t, 7, pool.AssignedJobs())
time.Sleep(200 * time.Millisecond)
// All jobs are finished
assert.Equal(t, 2, job2)
assert.Equal(t, 2, job3)
assert.Equal(t, 2, job4)
assert.Equal(t, 2, job5)
assert.Equal(t, 2, job6)
assert.Equal(t, 2, job7)
assert.Len(t, pool.jobQueue, 0)
assert.Equal(t, 7, pool.SubmittedJobs())
assert.Equal(t, 7, pool.AssignedJobs())
}
func TestPool_GivenAPoolFixedSize_WhenSubmitConfidentlyJob_ShouldRunCorrectly(t *testing.T) {
pool := startDummyPoolFixedSize(t, 2, 2)
job1 := 0
assert.NoError(t, pool.SubmitConfidently(NewCustomIdentifierJob("1", func() {
job1 = 1
pool.option.LogFunc("Job 1 is finished")
})))
time.Sleep(10 * time.Millisecond)
assert.Equal(t, 1, job1)
assert.Equal(t, 1, pool.SubmittedJobs())
assert.Equal(t, 1, pool.AssignedJobs())
job2 := 0
assert.NoError(t, pool.SubmitConfidently(NewCustomIdentifierJob("2", func() {
job2++
time.Sleep(200 * time.Millisecond)
pool.option.LogFunc("Job 2 is finished")
job2++
})))
job3 := 0
assert.NoError(t, pool.SubmitConfidently(NewCustomIdentifierJob("3", func() {
job3++
time.Sleep(200 * time.Millisecond)
pool.option.LogFunc("Job 3 is finished")
job3++
})))
pool.option.LogFunc("Wait for workers receive job 2, 3")
time.Sleep(10 * time.Millisecond)
// Job 5, 6 will be queued (job 4 will be in Idle holding point)
job4 := 0
assert.NoError(t, pool.SubmitConfidently(NewCustomIdentifierJob("4", func() {
job4++
time.Sleep(500 * time.Millisecond)
pool.option.LogFunc("Job 4 is finished")
job4++
})))
job5 := 0
assert.NoError(t, pool.SubmitConfidently(NewCustomIdentifierJob("5", func() {
job5++
time.Sleep(500 * time.Millisecond)
pool.option.LogFunc("Job 5 is finished")
job5++
})))
job6 := 0
assert.NoError(t, pool.SubmitConfidently(NewCustomIdentifierJob("6", func() {
job6++
time.Sleep(500 * time.Millisecond)
pool.option.LogFunc("Job 6 is finished")
job6++
})))
// Job 7 will be rejected due by queue full
job7 := 0
assert.ErrorIs(t, pool.SubmitConfidently(NewCustomIdentifierJob("7", func() {
job7++
time.Sleep(500 * time.Millisecond)
pool.option.LogFunc("Job 7 is finished")
job7++
})), ErrPoolFull)
pool.option.LogFunc("Submitted all jobs")
time.Sleep(100 * time.Millisecond)
assert.Equal(t, 1, job2)
assert.Equal(t, 1, job3)
assert.Equal(t, 0, job4)
assert.Equal(t, 0, job5)
assert.Equal(t, 0, job6)
assert.Equal(t, 0, job7)
assert.Equal(t, 7, pool.SubmittedJobs())
assert.Equal(t, 3, pool.AssignedJobs())
time.Sleep(150 * time.Millisecond)
assert.Equal(t, 2, job2)
assert.Equal(t, 2, job3)
assert.Equal(t, 1, job4)
assert.Equal(t, 1, job5)
assert.Equal(t, 0, job6)
assert.Equal(t, 0, job7)
assert.Equal(t, 7, pool.SubmittedJobs())
assert.Equal(t, 5, pool.AssignedJobs())
time.Sleep(500 * time.Millisecond)
assert.Equal(t, 2, job2)
assert.Equal(t, 2, job3)
assert.Equal(t, 2, job4)
assert.Equal(t, 2, job5)
assert.Equal(t, 1, job6)
assert.Equal(t, 0, job7)
assert.Equal(t, 7, pool.SubmittedJobs())
assert.Equal(t, 6, pool.AssignedJobs())
time.Sleep(500 * time.Millisecond)
assert.Equal(t, 2, job2)
assert.Equal(t, 2, job3)
assert.Equal(t, 2, job4)
assert.Equal(t, 2, job5)
assert.Equal(t, 2, job6)
assert.Equal(t, 0, job7)
assert.Equal(t, 7, pool.SubmittedJobs())
assert.Equal(t, 6, pool.AssignedJobs())
}