-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultierrgroup_test.go
58 lines (50 loc) · 1.06 KB
/
multierrgroup_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
package multierrgroup_test
import (
"context"
"errors"
"fmt"
"testing"
"go.ptx.dk/multierrgroup"
"go.uber.org/multierr"
)
func ExampleGroup() {
g := multierrgroup.Group{}
g.Go(func() error {
return errors.New("error 1")
})
g.Go(func() error {
return errors.New("error 2")
})
err := g.Wait()
// Using golang.org/x/sync/errgroup would return a return error depending on which goroutine was scheduled first
errs := multierr.Errors(err)
fmt.Println("Got", len(errs), "errors")
// Output: Got 2 errors
}
func TestWithContext(t *testing.T) {
err1 := errors.New("error 1")
err2 := errors.New("error 2")
g, ctx := multierrgroup.WithContext(context.Background())
g.Go(func() error {
return err1
})
g.Go(func() error {
return err2
})
err := g.Wait()
if !errors.Is(err, err1) {
t.Errorf("error: %s should be: %s", err, err1)
}
if !errors.Is(err, err2) {
t.Errorf("error: %s should be: %s", err, err2)
}
canceled := false
select {
case <-ctx.Done():
canceled = true
default:
}
if !canceled {
t.Errorf("context should have been canceled!")
}
}