-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
302 lines (243 loc) · 5.94 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
package bees
import (
"context"
"io"
"log"
"sync"
"sync/atomic"
"testing"
"time"
)
var emptyTask = func(ctx context.Context) {}
func TestClose(t *testing.T) {
t.Parallel()
defer func() {
if err := recover(); err != nil {
t.Fatalf("uncatched panic: %+v", err)
}
}()
pool := Create(context.Background(), WithCapacity(3), WithKeepAlive(50*time.Second), WithJitter(1))
pool.SetLogger(log.Default())
for i := 0; i < 100; i++ {
go pool.Submit(emptyTask)
go pool.Submit(emptyTask)
}
pool.Close()
}
func TestShutdownWithStacked(t *testing.T) {
t.Parallel()
defer func() {
if err := recover(); err != nil {
t.Fatalf("uncatched panic: %+v", err)
}
}()
task := func(ctx context.Context) {
select {
case <-time.After(time.Hour):
case <-ctx.Done():
}
}
pool := Create(context.Background(), WithCapacity(1), WithKeepAlive(time.Hour))
var wg sync.WaitGroup
// fill up worker pool by tasks
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
pool.Submit(task)
}()
}
pool.Close()
wg.Wait()
}
func TestConfigDefaultValues(t *testing.T) {
t.Parallel()
defer func() {
if err := recover(); err != nil {
t.Fatalf("uncatched panic: %+v", err)
}
}()
pool := Create(context.Background(), WithKeepAlive(0), WithJitter(0), WithCapacity(0))
if pool.cfg.KeepAliveTimeout == 0 || pool.cfg.TimeoutJitter == 0 || pool.cfg.Capacity == 0 {
t.Fail()
}
}
func TestRecoverAfterPanicOnSingleWorker(t *testing.T) {
t.Parallel()
defer func() {
if err := recover(); err != nil {
t.Fatalf("uncatched panic: %+v", err)
}
}()
checkCh := make(chan struct{})
task := func(ctx context.Context) {
checkCh <- struct{}{}
panic("aaaaaa")
}
pool := Create(context.Background(), WithKeepAlive(time.Hour), WithJitter(10), WithCapacity(1))
pool.SetLogger(log.New(io.Discard, "", 0))
pool.Submit(task)
<-checkCh // check first execution
pool.Submit(task)
<-checkCh // check second try to execute
}
func TestWorkerExpiration(t *testing.T) {
t.Parallel()
defer func() {
if err := recover(); err != nil {
t.Fatalf("uncatched panic: %+v", err)
}
}()
pool := Create(context.Background(), WithKeepAlive(10*time.Millisecond), WithJitter(1), WithCapacity(1))
for i := 0; i < 100; i++ {
pool.Submit(emptyTask)
}
time.Sleep(100 * time.Millisecond)
if atomic.LoadInt64(pool.activeWorkers) != 0 || atomic.LoadInt64(pool.freeWorkers) != 0 {
t.Fatalf("active workers found")
}
}
func TestWait(t *testing.T) {
t.Parallel()
const testCount = 1000
counter := ptrOfInt64(0)
pool := Create(
context.Background(),
WithJitter(1),
WithCapacity(testCount),
)
task := func(ctx context.Context) { time.Sleep(time.Millisecond); atomic.AddInt64(counter, 1) }
stopper := ptrOfInt64(0)
var wg sync.WaitGroup
wg.Add(1)
go func() {
for atomic.LoadInt64(stopper) == 0 {
time.Sleep(time.Microsecond)
}
for i := 0; i < testCount; i++ {
pool.Submit(task)
}
wg.Done()
}()
atomic.AddInt64(stopper, 1)
wg.Wait()
pool.Wait()
if actualCounter := atomic.LoadInt64(counter); actualCounter != testCount {
t.Fatalf("counter not equal: expected: %d, actual: %d", testCount, actualCounter)
}
}
func TestOnPanic(t *testing.T) {
t.Parallel()
const testCount = 1000
pool := Create(
context.Background(),
WithJitter(1),
WithCapacity(testCount),
WithTaskLen(0),
)
pool.SetLogger(log.New(io.Discard, "", 0))
task := func(ctx context.Context) { time.Sleep(time.Millisecond); panic("foo") }
stopper := ptrOfInt64(0)
var wg sync.WaitGroup
wg.Add(1)
go func() {
for atomic.LoadInt64(stopper) == 0 {
time.Sleep(time.Microsecond)
}
for i := 0; i < testCount; i++ {
pool.Submit(task)
}
wg.Done()
}()
atomic.AddInt64(stopper, 1)
wg.Wait()
pool.Wait()
taskCount := atomic.LoadInt64(pool.taskCount)
taskChLen := len(pool.taskCh)
if taskCount != 0 || taskChLen != 0 {
t.Fatalf("unconsistent task count: taskCount: %d, channel len: %d", taskCount, taskChLen)
}
pool.Close()
if aw := atomic.LoadInt64(pool.activeWorkers); aw != 0 {
t.Fatalf("unconsistent active workers count: expected zero, actual: %d", pool.activeWorkers)
}
if fw := atomic.LoadInt64(pool.freeWorkers); fw != 0 {
t.Fatalf("unconsistent free workers count: expected zero, actual: %d", fw)
}
if atomic.LoadInt64(pool.isClosed) != 1 {
t.Fatalf("isClosed must be one")
}
}
func TestCloseGracefullyByTimeout(t *testing.T) {
t.Parallel()
counter := ptrOfInt64(0)
pool := Create(
context.Background(),
WithJitter(1),
WithKeepAlive(time.Minute),
WithCapacity(1),
WithGracefulTimeout(3*time.Second),
WithTaskLen(2),
)
task := func(ctx context.Context) {
time.Sleep(time.Second)
atomic.AddInt64(counter, 1)
}
for i := 0; i < 100; i++ {
go pool.Submit(task)
}
start := time.Now()
pool.CloseGracefully()
if end := time.Since(start); end.Round(time.Second) > 6*time.Second {
t.Fatalf("too big wait time: %s", end)
}
}
func TestScale(t *testing.T) {
t.Parallel()
pool := Create(context.Background(), WithCapacity(1))
var wg sync.WaitGroup
wg.Add(1)
task := func(ctx context.Context) {
time.Sleep(time.Second)
}
go func() {
defer wg.Done()
pool.Submit(task)
}()
wg.Wait()
pool.Wait()
if wCap := atomic.LoadInt64(pool.workersCapacity); wCap != 1 {
t.Fatalf("wrong capacity: %d", wCap)
}
pool.Scale(100)
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
go pool.Submit(task)
}
}()
if wCap := atomic.LoadInt64(pool.workersCapacity); wCap != 101 {
t.Fatalf("wrong capacity: %d", wCap)
}
wg.Wait()
pool.Wait()
pool.Scale(-100)
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 2; i++ {
go pool.Submit(task)
}
}()
if wCap := atomic.LoadInt64(pool.workersCapacity); wCap != 1 {
t.Fatalf("wrong capacity: %d", wCap)
}
aw := atomic.LoadInt64(pool.activeWorkers)
fw := atomic.LoadInt64(pool.freeWorkers)
if fw < 0 && aw < 0 {
t.Fatalf("must be grater than zero, because some workers still alive after decrease worker count")
}
wg.Wait()
pool.Wait()
}