-
Notifications
You must be signed in to change notification settings - Fork 0
/
go120_test.go
55 lines (44 loc) · 1.24 KB
/
go120_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
// Copyright 2023 The Go Authors. All rights reserved.
// Copyright 2024 Volodymyr Kucherenko <kucherenkovova@gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.20
package safegroup_test
import (
"context"
"errors"
"testing"
"github.com/kucherenkovova/safegroup"
)
func TestCancelCause(t *testing.T) {
errDoom := errors.New("group_test: doomed")
cases := []struct {
errs []error
want error
}{
{want: nil},
{errs: []error{nil}, want: nil},
{errs: []error{errDoom}, want: errDoom},
{errs: []error{errDoom, nil}, want: errDoom},
}
for _, tc := range cases {
g, ctx := safegroup.WithContext(context.Background())
for _, err := range tc.errs {
err := err
g.TryGo(func() error { return err })
}
if err := g.Wait(); err != tc.want {
t.Errorf("after %T.TryGo(func() error { return err }) for err in %v\n"+
"g.Wait() = %v; want %v",
g, tc.errs, err, tc.want)
}
if tc.want == nil {
tc.want = context.Canceled
}
if err := context.Cause(ctx); err != tc.want {
t.Errorf("after %T.TryGo(func() error { return err }) for err in %v\n"+
"context.Cause(ctx) = %v; tc.want %v",
g, tc.errs, err, tc.want)
}
}
}