aboutsummaryrefslogtreecommitdiff
path: root/src/context/context_test.go
diff options
context:
space:
mode:
authorSameer Ajmani <sameer@google.com>2022-11-10 09:38:50 -0500
committerSameer Ajmani <sameer@golang.org>2023-01-20 21:41:01 +0000
commit8bec956360d63d2f39d45a889b200c0dedfe96a0 (patch)
treea62661a838523e74203146a8283db25add4dd4fb /src/context/context_test.go
parent59964663e58d60719c707d9d59d7424d6f2ea7e0 (diff)
downloadgo-8bec956360d63d2f39d45a889b200c0dedfe96a0.tar.xz
context: add APIs for setting a cancelation cause when deadline or timer expires
Fixes #56661 Change-Id: I1c23ebc52e6b7ae6ee956614e1a0a45d6ecbd5b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/449318 Run-TryBot: Sameer Ajmani <sameer@golang.org> Reviewed-by: Damien Neil <dneil@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/context/context_test.go')
-rw-r--r--src/context/context_test.go59
1 files changed, 57 insertions, 2 deletions
diff --git a/src/context/context_test.go b/src/context/context_test.go
index eb5a86b3c6..5311d8d4f4 100644
--- a/src/context/context_test.go
+++ b/src/context/context_test.go
@@ -793,8 +793,11 @@ func XTestCustomContextGoroutines(t testingT) {
func XTestCause(t testingT) {
var (
- parentCause = fmt.Errorf("parentCause")
- childCause = fmt.Errorf("childCause")
+ forever = 1e6 * time.Second
+ parentCause = fmt.Errorf("parentCause")
+ childCause = fmt.Errorf("childCause")
+ tooSlow = fmt.Errorf("tooSlow")
+ finishedEarly = fmt.Errorf("finishedEarly")
)
for _, test := range []struct {
name string
@@ -926,6 +929,58 @@ func XTestCause(t testingT) {
err: DeadlineExceeded,
cause: DeadlineExceeded,
},
+ {
+ name: "WithTimeout canceled",
+ ctx: func() Context {
+ ctx, cancel := WithTimeout(Background(), forever)
+ cancel()
+ return ctx
+ }(),
+ err: Canceled,
+ cause: Canceled,
+ },
+ {
+ name: "WithTimeoutCause",
+ ctx: func() Context {
+ ctx, cancel := WithTimeoutCause(Background(), 0, tooSlow)
+ cancel()
+ return ctx
+ }(),
+ err: DeadlineExceeded,
+ cause: tooSlow,
+ },
+ {
+ name: "WithTimeoutCause canceled",
+ ctx: func() Context {
+ ctx, cancel := WithTimeoutCause(Background(), forever, tooSlow)
+ cancel()
+ return ctx
+ }(),
+ err: Canceled,
+ cause: Canceled,
+ },
+ {
+ name: "WithTimeoutCause stacked",
+ ctx: func() Context {
+ ctx, cancel := WithCancelCause(Background())
+ ctx, _ = WithTimeoutCause(ctx, 0, tooSlow)
+ cancel(finishedEarly)
+ return ctx
+ }(),
+ err: DeadlineExceeded,
+ cause: tooSlow,
+ },
+ {
+ name: "WithTimeoutCause stacked canceled",
+ ctx: func() Context {
+ ctx, cancel := WithCancelCause(Background())
+ ctx, _ = WithTimeoutCause(ctx, forever, tooSlow)
+ cancel(finishedEarly)
+ return ctx
+ }(),
+ err: Canceled,
+ cause: finishedEarly,
+ },
} {
if got, want := test.ctx.Err(), test.err; want != got {
t.Errorf("%s: ctx.Err() = %v want %v", test.name, got, want)