-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker-pool_test.go
312 lines (252 loc) · 7.13 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
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
// see LICENSE file
package spool
import (
"context"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/dc0d/actor"
"github.com/stretchr/testify/assert"
)
func Test_WorkerPool_New(t *testing.T) {
t.Run(`should set default mailbox size to zero`, func(t *testing.T) {
pool := New(-1)
defer pool.Stop()
assert.True(t, len(pool) == 0)
})
t.Run(`should not start any initial workers`, func(t *testing.T) {
pool := New(-1)
defer pool.Stop()
assert.Never(t, func() bool {
select {
case pool <- func() { panic("should not run") }:
return true
default:
}
return false
}, time.Millisecond*300, time.Millisecond*20)
})
}
func Test_WorkerPool_grow_should_spawn_workers_equal_to_growth(t *testing.T) {
var (
ctx = context.Background()
growth = 100
options []actor.Option
)
pool := New(-1)
exec := &CallbacksSpy[T]{
StoppedFunc: func() {},
}
executorFactory := func() actor.Callbacks[T] { return exec }
pool.grow(ctx, growth, executorFactory, options...)
pool.Stop()
assert.Eventually(t, func() bool {
return len(exec.StoppedCalls()) == growth
}, time.Millisecond*300, time.Millisecond*20)
}
func Test_WorkerPool_Blocking_should_serialize_the_jobs(t *testing.T) {
const n = 1000
pool := New(10)
defer pool.Stop()
pool.Grow(context.Background(), 1)
var (
counter, previous int64
)
wg := &sync.WaitGroup{}
wg.Add(n)
start := make(chan struct{})
for i := 0; i < n; i++ {
go func() {
_ = pool.Blocking(context.Background(), func() {
defer wg.Done()
<-start
previous = atomic.LoadInt64(&counter)
next := atomic.AddInt64(&counter, 1)
assert.Equal(t, previous+1, next)
})
}()
}
close(start) // signal all jobs they are green to go
wg.Wait()
assert.Equal(t, int64(n), counter)
}
func Test_WorkerPool_Blocking_should_respect_context_cancellation(t *testing.T) {
pool := New(-1)
defer pool.Stop()
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := pool.Blocking(ctx, func() { panic("should not be called") })
assert.Equal(t, context.Canceled, err)
}
func Test_WorkerPool_SemiBlocking_should_just_put_job_in_the_mailbox(t *testing.T) {
const n = 1000
pool := New(n)
defer pool.Stop()
pool.Grow(context.Background(), 1)
var counter int64 = 0
wg := &sync.WaitGroup{}
wg.Add(n)
for i := 0; i < n; i++ {
_ = pool.SemiBlocking(context.Background(), func() {
defer wg.Done()
atomic.AddInt64(&counter, 1)
})
}
wg.Wait()
assert.Equal(t, int64(n), counter)
}
func Test_WorkerPool_SemiBlocking_should_respect_context_cancellation(t *testing.T) {
pool := New(-1)
defer pool.Stop()
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := pool.SemiBlocking(ctx, func() { panic("should not be called") })
assert.Equal(t, context.Canceled, err)
}
func Test_WorkerPool_should_not_stop_because_of_panic(t *testing.T) {
pool := New(1)
defer pool.Stop()
pool.Grow(context.Background(), 1)
_ = pool.Blocking(context.Background(), func() {
panic("some error")
})
counter := 0
_ = pool.Blocking(context.Background(), func() {
counter++
})
assert.Equal(t, 1, counter)
}
func Test_WorkerPool_Grow_should_stop_extra_workers_with_absolute_timeout(t *testing.T) {
increased := 10
absoluteTimeout := time.Millisecond * 10
pool := New(9)
defer pool.Stop()
exec := &CallbacksSpy[T]{
StoppedFunc: func() {},
}
executorFactory := func() actor.Callbacks[T] { return exec }
pool.grow(context.Background(), increased, executorFactory, actor.WithAbsoluteTimeout(absoluteTimeout))
assert.Eventually(t, func() bool {
return len(exec.StoppedCalls()) == increased
}, time.Millisecond*500, time.Millisecond*50)
}
func Test_WorkerPool_Grow_should_stop_extra_workers_with_idle_timeout_when_there_are_no_more_jobs(t *testing.T) {
const n = 1000
increased := 10
idleTimeout := time.Millisecond * 50
pool := New(100)
defer pool.Stop()
exec := &CallbacksSpy[T]{
StoppedFunc: func() {},
ReceivedFunc: func(fn func()) { fn() },
}
executorFactory := func() actor.Callbacks[T] { return exec }
start := make(chan struct{}, n)
wg := &sync.WaitGroup{}
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
_ = pool.SemiBlocking(context.Background(), func() {
defer wg.Done()
<-start
})
}()
}
pool.grow(context.Background(), increased, executorFactory, actor.WithIdleTimeout(idleTimeout))
expectedNumberOfWorkers := increased
assert.Eventuallyf(t, func() bool {
return expectedNumberOfWorkers == len(exec.ReceivedCalls())
}, time.Millisecond*1000, time.Millisecond*20,
"expected %v actual %v", expectedNumberOfWorkers, func() int { return len(exec.ReceivedCalls()) }())
close(start)
wg.Wait()
expectedNumberOfStoppedWorkers := 10
assert.Eventually(t, func() bool {
return expectedNumberOfStoppedWorkers == len(exec.StoppedCalls())
}, time.Millisecond*5000, time.Millisecond*50)
}
func Test_WorkerPool_Grow_should_stop_extra_workers_when_context_is_canceled(t *testing.T) {
increased := 10
pool := New(10)
defer pool.Stop()
exec := &CallbacksSpy[T]{
StoppedFunc: func() {},
ReceivedFunc: func(fn func()) { fn() },
}
executorFactory := func() actor.Callbacks[T] { return exec }
pool.grow(context.Background(), 1, executorFactory)
ctx, cancel := context.WithCancel(context.Background())
pool.grow(ctx, increased, executorFactory)
cancel()
expectedNumberOfStoppedWorkers := increased
assert.Eventually(t, func() bool {
return expectedNumberOfStoppedWorkers == len(exec.StoppedCalls())
}, time.Millisecond*5000, time.Millisecond*50)
}
func Test_WorkerPool_Stop_should_close_the_pool(t *testing.T) {
pool := New(9)
pool.Stop()
assert.Panics(t, func() {
_ = pool.SemiBlocking(context.Background(), func() {})
})
}
func Test_WorkerPool_Stop_should_stop_the_workers(t *testing.T) {
pool := New(9)
increased := 10
exec := &CallbacksSpy[T]{
StoppedFunc: func() {},
ReceivedFunc: func(fn func()) { fn() },
}
executorFactory := func() actor.Callbacks[T] { return exec }
pool.grow(context.Background(), increased, executorFactory)
pool.Stop()
expectedNumberOfStoppedWorkers := increased
assert.Eventually(t, func() bool {
return expectedNumberOfStoppedWorkers == len(exec.StoppedCalls())
}, time.Millisecond*5000, time.Millisecond*50)
}
func ExampleWorkerPool_Blocking() {
pool := New(1)
defer pool.Stop()
pool.Grow(context.Background(), 1)
var state int64
job := func() { atomic.AddInt64(&state, 19) }
_ = pool.Blocking(context.Background(), job)
fmt.Println(atomic.LoadInt64(&state))
// Output:
// 19
}
func ExampleWorkerPool_SemiBlocking() {
pool := New(1)
defer pool.Stop()
pool.Grow(context.Background(), 1)
var state int64
jobDone := make(chan struct{})
job := func() {
defer close(jobDone)
atomic.AddInt64(&state, 19)
}
_ = pool.SemiBlocking(context.Background(), job)
<-jobDone
fmt.Println(state)
// Output:
// 19
}
func ExampleWorkerPool_Grow() {
const n = 19
pool := New(1)
defer pool.Stop()
pool.Grow(context.Background(), 3) // spin up three new workers
var state int64
wg := &sync.WaitGroup{}
wg.Add(n)
for i := 0; i < n; i++ {
_ = pool.SemiBlocking(context.Background(), func() { defer wg.Done(); atomic.AddInt64(&state, 1) })
}
wg.Wait()
fmt.Println(state)
// Output:
// 19
}