-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskgroup_test.go
More file actions
321 lines (297 loc) · 8.06 KB
/
taskgroup_test.go
File metadata and controls
321 lines (297 loc) · 8.06 KB
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
package taskgroup_test
import (
"context"
"fmt"
"testing"
"time"
tg "github.com/weirdname404/taskgroup"
)
const (
N = 10
TestTaskTime = 100
)
var ERROR = fmt.Errorf("ERROR")
var ExpectedRes []int
type HasFatal interface {
Fatal(args ...any)
Fatalf(format string, args ...any)
}
func testFunc(i int) (int, error) {
time.Sleep((time.Duration(TestTaskTime-i*10) * time.Millisecond))
return i, nil
}
func errFunc(i int) (int, error) {
time.Sleep((time.Duration(TestTaskTime-i*10) * time.Millisecond))
return 0, ERROR
}
func addNTestFunc(g *tg.TaskGroup[int]) error {
for i := range N {
f := func() (int, error) {
return testFunc(i)
}
err := g.Add(f)
if err != nil {
return err
}
}
return nil
}
func addTasksAndGetResults(g *tg.TaskGroup[int]) ([]int, error) {
err := addNTestFunc(g)
if err != nil {
return nil, err
}
res := make([]int, 0, len(ExpectedRes))
for v := range g.Start() {
res = append(res, v)
}
return res, nil
}
func AddTasksAndCheckResults(g *tg.TaskGroup[int], t HasFatal) {
res, err := addTasksAndGetResults(g)
if err != nil {
t.Fatalf("Unexpected error - %v", err)
}
if err := g.Error(); err != nil {
t.Fatalf("Unexpected error - %v", err)
}
if !(len(res) == len(ExpectedRes)) {
t.Fatalf("Unexpected length of the results - expected: %d got: %d", len(ExpectedRes), len(res))
}
for i := range res {
if !(ExpectedRes[i] == res[i]) {
t.Fatalf("Unexpected result, expected: %v got: %v", ExpectedRes[i], res[i])
}
}
}
func ErrorScenario(g *tg.TaskGroup[int]) ([]int, error) {
for i := range N {
f := func(ctx context.Context) (int, error) {
return testFunc(i)
}
if i%2 == 0 {
err := g.Add(func(ctx context.Context) (int, error) {
return errFunc(i)
})
if err != nil {
return nil, err
}
} else {
err := g.Add(f)
if err != nil {
return nil, err
}
}
}
res := make([]int, 0, len(ExpectedRes))
for v := range g.Start() {
res = append(res, v)
}
return res, nil
}
func TestTaskGroupNoTasks(t *testing.T) {
g := tg.NewTaskGroup[any](context.Background())
res := make([]any, 0)
ch := g.Start()
for v := range ch {
res = append(res, v)
}
if !(len(res) == 0) {
t.Fatalf("Unexpected results")
}
_, open := <-ch
if open {
t.Fatalf("Expected result channel to be closed")
}
}
func TestTaskGroup(t *testing.T) {
g := tg.NewTaskGroup[int](context.Background())
AddTasksAndCheckResults(g, t)
}
func TestTaskGroupRemainder(t *testing.T) {
// N = 10 - number of tasks
// m = 3 - chunks size
// 10 % 3 = 1 - remainder
g := tg.NewTaskGroup[int](context.Background()).Limit(3)
AddTasksAndCheckResults(g, t)
}
func TestNoLimitTaskGroup(t *testing.T) {
g := tg.NewTaskGroup[int](context.Background()).Limit(0)
AddTasksAndCheckResults(g, t)
}
func TestLimitIsGreaterThanNTasks(t *testing.T) {
g := tg.NewTaskGroup[int](context.Background()).Limit(N * 10)
AddTasksAndCheckResults(g, t)
}
func TestTaskGroupErr(t *testing.T) {
g := tg.NewTaskGroup[int](context.Background())
res, err := ErrorScenario(g)
if !(err == nil) {
t.Fatalf("Unexpected error - expected: nothing got: %s", err.Error())
}
gErr := g.Error()
if !(gErr != nil) {
t.Fatalf("Expected error but got nothing")
}
if gErr.Error() != ERROR.Error() {
t.Fatalf("Unexpected error - expected: %s got: %s", ERROR.Error(), gErr.Error())
}
// error in the first chunk -> no chunks -> no results
if len(res) != 0 {
t.Fatalf("Unexpected result, expected: nothing got: %v", res)
}
// calling g.Error again will return nothing
if !(g.Error() == nil) {
t.Fatalf("Expected nothing")
}
}
func TestTaskGroupAutoClear(t *testing.T) {
g := tg.NewTaskGroup[int](context.Background()).Limit(5)
AddTasksAndCheckResults(g, t)
// it is expected that the group will auto clear itself after successful processing
AddTasksAndCheckResults(g, t)
// by the way `Clear` is idempotent, so you can call it anytime
g.Clear()
AddTasksAndCheckResults(g, t)
}
func TestTaskGroupManualClear(t *testing.T) {
g := tg.NewTaskGroup[int](context.Background()).Limit(5)
// it can be a case when something went wrong while adding tasks
for i := range N {
f := func(ctx context.Context) (int, error) {
return testFunc(i)
}
// something is happening...
if i+1 == N {
// at this point let's pretend some error occured that makes futher addition of the task pointless
break
}
err := g.Add(f)
if !(err == nil) {
t.Fatalf("Unexpected error - expected: nothing got: %s", err.Error())
}
}
// if the user forgets to clear the group and tries to add tasks again, the further results might be unexpected
err := addNTestFunc(g)
if !(err == nil) {
t.Fatalf("Unexpected error - expected: nothing got: %s", err.Error())
}
// old tasks are present
res := make([]int, 0, 20)
for v := range g.Start() {
res = append(res, v)
}
if !(len(res) > len(ExpectedRes)) {
t.Fatalf("Unexpected amount of tasks, expected: %v got: %v", len(ExpectedRes), len(res))
}
// g.Start is pointless as the number of the results will be different from the expected one
// the group must be cleared
g.Clear()
AddTasksAndCheckResults(g, t)
}
func TestCustomTaskGroupCtxCancel(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), TestTaskTime*(2*time.Millisecond))
defer cancel()
// process tasks sequentially
g := tg.NewTaskGroup[int](ctx).Limit(1)
res, err := addTasksAndGetResults(g)
if !(err == nil) {
t.Fatalf("Unexpected error - expected: nothing got: %s", err.Error())
}
// context error goes silently
if !(g.Error() != nil) {
t.Fatalf("Expected error - got nothing")
}
// 200ms is time for 2 tasks
if len(res) != 2 {
t.Fatalf("Unexpected result, expected: exactly 2 results got: %v", res)
}
}
func TestErrUnknownTaskType(t *testing.T) {
g := tg.NewTaskGroup[int](context.Background())
// func with no target result
err := g.Add(func() any { return nil })
if !(err == tg.ErrUnknownTaskType) {
t.Fatal("Expected tg.ErrUnknownTaskType")
}
// func with no result
err = g.Add(func() {})
if !(err == tg.ErrUnknownTaskType) {
t.Fatal("Expected tg.ErrUnknownTaskType")
}
// func with unknown args
err = g.Add(func(_ any) {})
if !(err == tg.ErrUnknownTaskType) {
t.Fatal("Expected tg.ErrUnknownTaskType")
}
// func with unknown results
err = g.Add(func() (int, error, any) { return 0, nil, nil })
if !(err == tg.ErrUnknownTaskType) {
t.Fatal("Expected tg.ErrUnknownTaskType")
}
// nil
err = g.Add(nil)
if !(err == tg.ErrUnknownTaskType) {
t.Fatal("Expected ErrUnknownTaskType")
}
}
func TestTaskType(t *testing.T) {
g := tg.NewTaskGroup[int](context.Background())
// func with target result
err := g.Add(func() int { return 0 })
if !(err == nil) {
t.Fatalf("Unexpected error - expected: nothing got: %s", err.Error())
}
// func with target result and error
err = g.Add(func() (int, error) { return 0, nil })
if !(err == nil) {
t.Fatalf("Unexpected error - expected: nothing got: %s", err.Error())
}
// func with target result and context
err = g.Add(func(_ context.Context) int { return 0 })
if !(err == nil) {
t.Fatalf("Unexpected error - expected: nothing got: %s", err.Error())
}
// func with target result, error and context
err = g.Add(func(_ context.Context) (int, error) { return 0, nil })
if !(err == nil) {
t.Fatalf("Unexpected error - expected: nothing got: %s", err.Error())
}
}
// go test -bench=. -benchmem .
func Benchmark(b *testing.B) {
for _, bechN := range []int{100, 1_000, 10_000} {
for _, limit := range []int{bechN / 4, bechN / 3, 10_000} {
g := tg.NewTaskGroup[int](context.Background()).Limit(limit)
tasks := make([]func() (int, error), bechN)
for i := range bechN {
f := func() (int, error) {
return testFunc(i)
}
tasks[i] = f
}
b.ResetTimer()
b.ReportAllocs()
b.Run(fmt.Sprintf("tasks=%d,limit=%d", bechN, limit), func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, task := range tasks {
err := g.Add(task)
if err != nil {
b.Fatal(err)
}
}
for v := range g.Start() {
_ = v
}
}
})
}
}
}
func TestMain(m *testing.M) {
ExpectedRes = make([]int, N)
for i := range N {
ExpectedRes[i] = i
}
m.Run()
}