-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
errgroup: use WithCancelCause to cancel context
Fixes golang/go#59355 Change-Id: Ib6a88e7e5fefe7b0d5672035af16d109aabcbf1e Reviewed-on: https://go-review.googlesource.com/c/sync/+/481255 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Auto-Submit: Bryan Mills <bcmills@google.com>
- Loading branch information
1 parent
4966af6
commit 93782cc
Showing
4 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2023 The Go Authors. 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 | ||
// +build go1.20 | ||
|
||
package errgroup | ||
|
||
import "context" | ||
|
||
func withCancelCause(parent context.Context) (context.Context, func(error)) { | ||
return context.WithCancelCause(parent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2023 The Go Authors. 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 | ||
// +build go1.20 | ||
|
||
package errgroup_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
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 := errgroup.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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2023 The Go Authors. 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 | ||
// +build !go1.20 | ||
|
||
package errgroup | ||
|
||
import "context" | ||
|
||
func withCancelCause(parent context.Context) (context.Context, func(error)) { | ||
ctx, cancel := context.WithCancel(parent) | ||
return ctx, func(error) { cancel() } | ||
} |