-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_test.go
81 lines (69 loc) · 1.7 KB
/
group_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
package syncerr
import (
"errors"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGroup(t *testing.T) {
t.Run("instantiate", func(t *testing.T) {
s := new(Group)
assert.Nil(t, s.errs)
assert.Nil(t, s.Error())
})
t.Run("one goroutine with error", func(t *testing.T) {
s := new(Group)
s.Go(func() error {
return errors.New("problem")
})
s.Wait()
require.NotNil(t, s.errs)
assert.Len(t, s.errs, 1)
assert.NotNil(t, s.Error())
})
t.Run("three goroutines with two errors", func(t *testing.T) {
s := new(Group)
s.Go(func() error {
<-time.NewTimer(100 * time.Millisecond).C
return errors.New("error 1")
})
s.Go(func() error {
return errors.New("error 2")
})
s.Go(func() error {
<-time.NewTimer(100 * time.Millisecond).C
return nil
})
select {
case <-s.Done():
// no-op
case <-time.NewTimer(500 * time.Millisecond).C:
assert.FailNow(t, "timeout")
}
assert.Len(t, s.errs, 1) // only one item queued
err := s.Error()
assert.NotNil(t, err)
wrapper, ok := err.(interface{ Unwrap() []error })
require.True(t, ok, "err does not implement `interface{ Unwrap() []error }`")
errs := wrapper.Unwrap()
// in this test the order of the errors is predictable
assert.Equal(t, "error 2", errs[0].Error())
assert.Equal(t, "error 1", errs[1].Error())
})
t.Run("all errors", func(t *testing.T) {
s := new(Group)
s.Go(func() error {
return errors.New("foo")
})
s.Go(func() error {
return errors.New("bar")
})
s.Go(func() error {
return errors.New("baz")
})
s.Wait()
assert.ElementsMatch(t, []string{"foo", "bar", "baz"}, strings.Split(s.Error().Error(), "\n"))
})
}