-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
205 lines (169 loc) · 4.03 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
package bounded
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
)
func TestPool(t *testing.T) {
poolSize := 5
pool, _ := NewPool(context.Background(), uint32(poolSize))
results := make(chan int)
for i := 0; i < poolSize; i++ {
i := i
pool.Go(func() error {
results <- i
return nil
})
}
for i := 0; i < poolSize; i++ {
<-results
}
if err := pool.Wait(); err != nil {
t.Errorf("unexpected error: %v", err)
}
}
func TestPool_limits_goroutines(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
poolSize := 50
pool, ctx := NewPool(ctx, uint32(poolSize))
for i := 0; i < 100; i++ {
pool.Go(func() error {
// "heavy" task
time.Sleep(50 * time.Millisecond)
return nil
})
}
if err := pool.Wait(); err != nil {
t.Errorf("expected no errors, got: %v", err)
}
if size := pool.Size(); size > uint32(poolSize) {
t.Errorf("expected goroutines to cap at %v, got: %v", poolSize, size)
}
}
func TestPool_lazily_loads_goroutines(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
poolSize := 50
pool, ctx := NewPool(ctx, uint32(poolSize))
for i := 0; i < poolSize; i++ {
pool.Go(func() error {
// "light" task
return nil
})
time.Sleep(5 * time.Millisecond)
}
if err := pool.Wait(); err != nil {
t.Errorf("expected no errors, got: %v", err)
}
if size := pool.Size(); size >= uint32(poolSize) {
t.Errorf("expected goroutines to run lazily, got: %v", size)
}
}
func TestPool_exits_when_context_is_cancelled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
pool, ctx := NewPool(ctx, 5)
var events int32
for i := 0; i < 100; i++ {
if i >= 80 {
cancel()
}
pool.Go(func() error {
atomic.AddInt32(&events, 1)
return nil
})
}
if err := pool.Wait(); err != context.Canceled {
t.Errorf("expected error to be context.Canceled, got: %v", err)
}
if events >= 100 {
t.Errorf("expected goroutines to exit early, got: %v", events)
}
}
func TestPool_exits_on_error(t *testing.T) {
expectedErr := errors.New("error")
pool, _ := NewPool(context.Background(), 5)
var events int32
for _, err := range []error{
expectedErr,
errors.New("pool_test: 1"),
errors.New("pool_test: 2"),
} {
err := err
pool.Go(func() error {
atomic.AddInt32(&events, 1)
return err
})
time.Sleep(5 * time.Millisecond)
}
if err := pool.Wait(); expectedErr != err {
t.Errorf("expected error to be %v, got: %v", expectedErr, err)
}
if events != 1 {
t.Errorf("expected only 1 goroutine to run and error, got: %v", events)
}
}
func TestZeroGroup(t *testing.T) {
err1 := errors.New("pool_test: 1")
err2 := errors.New("pool_test: 2")
cases := []struct {
errs []error
}{
{errs: []error{}},
{errs: []error{nil}},
{errs: []error{err1}},
{errs: []error{err1, nil}},
{errs: []error{err1, nil, err2}},
}
for _, tc := range cases {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var firstErr error
pool, _ := NewPool(ctx, 1)
for _, err := range tc.errs {
err := err
pool.Go(func() error { return err })
if firstErr == nil && err != nil {
firstErr = err
}
}
if actualErr := pool.Wait(); actualErr != firstErr {
t.Errorf("unexpected error, expected: %v, got: %v", firstErr, actualErr)
}
}
}
func TestWithContext(t *testing.T) {
errDoom := errors.New("group_test: doomed")
cases := []struct {
errs []error
want error
}{
{want: nil},
{errs: []error{nil}, want: nil},
{errs: []error{errDoom}, want: errDoom},
{errs: []error{errDoom, nil}, want: errDoom},
}
for _, tc := range cases {
ctx := context.Background()
pool, ctx := NewPool(ctx, 5)
for _, err := range tc.errs {
err := err
pool.Go(func() error { return err })
}
if err := pool.Wait(); err != tc.want {
t.Errorf("unexpected error, expected: %v, got: %v", tc.want, err)
}
canceled := false
select {
case <-ctx.Done():
canceled = true
default:
}
if !canceled {
t.Error("expected context to be canceled")
}
}
}