-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathparallel_test.go
More file actions
115 lines (102 loc) · 1.88 KB
/
Copy pathparallel_test.go
File metadata and controls
115 lines (102 loc) · 1.88 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
package bigopool
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestParallelNoErrors(t *testing.T) {
x := 0
m := sync.Mutex{}
ee := Parallel(
func() error {
m.Lock()
defer m.Unlock()
x++
return nil
},
func() error {
m.Lock()
defer m.Unlock()
x += 2
return nil
},
func() error {
m.Lock()
defer m.Unlock()
x += 3
return nil
},
)
assert.True(t, ee.IsEmpty())
assert.Equal(t, 6, x)
}
func TestParallelWithErrors(t *testing.T) {
y := 0
ee := Parallel(
func() error {
return errors.New("error 1")
},
func() error {
return errors.New("error 2")
},
func() error {
y--
return errors.New("error 3")
},
)
assert.False(t, ee.IsEmpty())
assert.Equal(t, 3, len(ee.All()))
assert.NotNil(t, ee.ToError())
assert.Equal(t, -1, y)
}
func TestCancelableParallelNoErrors(t *testing.T) {
x := 0
m := sync.Mutex{}
ee := CancelableParallel(context.Background(),
func(context.Context) error {
m.Lock()
defer m.Unlock()
x++
return nil
},
func(context.Context) error {
m.Lock()
defer m.Unlock()
x += 2
return nil
},
func(context.Context) error {
m.Lock()
defer m.Unlock()
x += 3
return nil
},
)
assert.True(t, ee.IsEmpty())
assert.Equal(t, 6, x)
}
func TestCancelableParallelWithError(t *testing.T) {
ee := CancelableParallel(context.Background(),
func(c context.Context) error {
return errors.New("test")
},
func(c context.Context) error {
// Set a timeout in case the context isn't canceled.
timer := time.NewTimer(100 * time.Millisecond)
select {
case <-c.Done():
return nil
case <-timer.C:
// Can't t.Fatal since we aren't in the main goroutine
t.Log("Did not cancel context due to previous error")
t.Fail()
return nil
}
},
)
assert.False(t, ee.IsEmpty())
assert.Len(t, ee.All(), 1)
}