-
Notifications
You must be signed in to change notification settings - Fork 6
/
invoke_all_test.go
86 lines (79 loc) · 1.72 KB
/
invoke_all_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
// Copyright 2019 Grabtaxi Holdings PTE LTE (GRAB), All rights reserved.
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
package async
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestInvokeAll(t *testing.T) {
resChan := make(chan int, 6)
works := make([]Work, 6, 6)
for i := range works {
j := i
works[j] = func(context.Context) (interface{}, error) {
resChan <- j / 2
time.Sleep(time.Millisecond * 10)
return nil, nil
}
}
tasks := NewTasks(works...)
InvokeAll(context.Background(), 2, tasks)
WaitAll(tasks)
close(resChan)
res := []int{}
for r := range resChan {
res = append(res, r)
}
assert.Equal(t, []int{0, 0, 1, 1, 2, 2}, res)
}
func TestInvokeAllWithZeroConcurrency(t *testing.T) {
resChan := make(chan int, 6)
works := make([]Work, 6, 6)
for i := range works {
j := i
works[j] = func(context.Context) (interface{}, error) {
resChan <- 1
time.Sleep(time.Millisecond * 10)
return nil, nil
}
}
tasks := NewTasks(works...)
InvokeAll(context.Background(), 0, tasks)
WaitAll(tasks)
close(resChan)
res := []int{}
for r := range resChan {
res = append(res, r)
}
assert.Equal(t, []int{1, 1, 1, 1, 1, 1}, res)
}
func ExampleInvokeAll() {
resChan := make(chan int, 6)
works := make([]Work, 6, 6)
for i := range works {
j := i
works[j] = func(context.Context) (interface{}, error) {
fmt.Println(j / 2)
time.Sleep(time.Millisecond * 10)
return nil, nil
}
}
tasks := NewTasks(works...)
InvokeAll(context.Background(), 2, tasks)
WaitAll(tasks)
close(resChan)
res := []int{}
for r := range resChan {
res = append(res, r)
}
// Output:
// 0
// 0
// 1
// 1
// 2
// 2
}