aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing.go
diff options
context:
space:
mode:
authorVladimir Varankin <vladimir@varank.in>2024-08-20 09:19:22 +0000
committerGopher Robot <gobot@golang.org>2024-08-20 14:58:54 +0000
commitf432b5f756c223564fd397b7568bd2ce949c7c6f (patch)
tree119a05f7aec6246b8844201608ddfc1787a5c9f1 /src/testing/testing.go
parentc6c9634515e6128a5acb8645dced62581f5d1b1b (diff)
downloadgo-f432b5f756c223564fd397b7568bd2ce949c7c6f.tar.xz
testing: add Context
Adds a new Context method to testing.T, that returns a context, that is canceled before the end of its test function. Fixes #36532. Change-Id: I9315ad4dad25529d0b5be809e2d9db4e7528b5f2 GitHub-Last-Rev: 1c3fd6c4d8a9cc68a61f2df284d04d3d080216be GitHub-Pull-Request: golang/go#68828 Reviewed-on: https://go-review.googlesource.com/c/go/+/603959 Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Damien Neil <dneil@google.com>
Diffstat (limited to 'src/testing/testing.go')
-rw-r--r--src/testing/testing.go54
1 files changed, 41 insertions, 13 deletions
diff --git a/src/testing/testing.go b/src/testing/testing.go
index 49d14f5f66..eb6efed5a8 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -371,6 +371,7 @@ package testing
import (
"bytes"
+ "context"
"errors"
"flag"
"fmt"
@@ -633,6 +634,9 @@ type common struct {
tempDir string
tempDirErr error
tempDirSeq int32
+
+ ctx context.Context
+ cancelCtx context.CancelFunc
}
// Short reports whether the -test.short flag is set.
@@ -898,6 +902,7 @@ type TB interface {
Skipf(format string, args ...any)
Skipped() bool
TempDir() string
+ Context() context.Context
// A private method to prevent users implementing the
// interface and so future additions to it will not
@@ -1351,6 +1356,16 @@ func (c *common) Chdir(dir string) {
})
}
+// Context returns a context that is canceled just before
+// [T.Cleanup]-registered functions are called.
+//
+// Cleanup functions can wait for any resources
+// that shut down on Context.Done before the test completes.
+func (c *common) Context() context.Context {
+ c.checkFuzzFn("Context")
+ return c.ctx
+}
+
// panicHandling controls the panic handling used by runCleanup.
type panicHandling int
@@ -1383,6 +1398,10 @@ func (c *common) runCleanup(ph panicHandling) (panicVal any) {
}
}()
+ if c.cancelCtx != nil {
+ c.cancelCtx()
+ }
+
for {
var cleanup func()
c.mu.Lock()
@@ -1771,15 +1790,21 @@ func (t *T) Run(name string, f func(t *T)) bool {
// continue walking the stack into the parent test.
var pc [maxStackLen]uintptr
n := runtime.Callers(2, pc[:])
+
+ // There's no reason to inherit this context from parent. The user's code can't observe
+ // the difference between the background context and the one from the parent test.
+ ctx, cancelCtx := context.WithCancel(context.Background())
t = &T{
common: common{
- barrier: make(chan bool),
- signal: make(chan bool, 1),
- name: testName,
- parent: &t.common,
- level: t.level + 1,
- creator: pc[:n],
- chatty: t.chatty,
+ barrier: make(chan bool),
+ signal: make(chan bool, 1),
+ name: testName,
+ parent: &t.common,
+ level: t.level + 1,
+ creator: pc[:n],
+ chatty: t.chatty,
+ ctx: ctx,
+ cancelCtx: cancelCtx,
},
context: t.context,
}
@@ -2205,15 +2230,18 @@ func runTests(matchString func(pat, str string) (bool, error), tests []InternalT
// to keep trying.
break
}
- ctx := newTestContext(*parallel, newMatcher(matchString, *match, "-test.run", *skip))
- ctx.deadline = deadline
+ ctx, cancelCtx := context.WithCancel(context.Background())
+ tctx := newTestContext(*parallel, newMatcher(matchString, *match, "-test.run", *skip))
+ tctx.deadline = deadline
t := &T{
common: common{
- signal: make(chan bool, 1),
- barrier: make(chan bool),
- w: os.Stdout,
+ signal: make(chan bool, 1),
+ barrier: make(chan bool),
+ w: os.Stdout,
+ ctx: ctx,
+ cancelCtx: cancelCtx,
},
- context: ctx,
+ context: tctx,
}
if Verbose() {
t.chatty = newChattyPrinter(t.w)