...
1
2
3
4
5
6
7 package errgroup_test
8
9 import (
10 "context"
11 "errors"
12 "testing"
13
14 "golang.org/x/sync/errgroup"
15 )
16
17 func TestCancelCause(t *testing.T) {
18 errDoom := errors.New("group_test: doomed")
19
20 cases := []struct {
21 errs []error
22 want error
23 }{
24 {want: nil},
25 {errs: []error{nil}, want: nil},
26 {errs: []error{errDoom}, want: errDoom},
27 {errs: []error{errDoom, nil}, want: errDoom},
28 }
29
30 for _, tc := range cases {
31 g, ctx := errgroup.WithContext(context.Background())
32
33 for _, err := range tc.errs {
34 err := err
35 g.TryGo(func() error { return err })
36 }
37
38 if err := g.Wait(); err != tc.want {
39 t.Errorf("after %T.TryGo(func() error { return err }) for err in %v\n"+
40 "g.Wait() = %v; want %v",
41 g, tc.errs, err, tc.want)
42 }
43
44 if tc.want == nil {
45 tc.want = context.Canceled
46 }
47
48 if err := context.Cause(ctx); err != tc.want {
49 t.Errorf("after %T.TryGo(func() error { return err }) for err in %v\n"+
50 "context.Cause(ctx) = %v; tc.want %v",
51 g, tc.errs, err, tc.want)
52 }
53 }
54 }
55
View as plain text