-
Notifications
You must be signed in to change notification settings - Fork 993
Add testing.T.Context() and testing.B.Context()
#5058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ package testing | |
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "flag" | ||
| "fmt" | ||
|
|
@@ -78,6 +79,9 @@ type common struct { | |
| tempDir string | ||
| tempDirErr error | ||
| tempDirSeq int32 | ||
|
|
||
| ctx context.Context | ||
| cancelCtx context.CancelFunc | ||
| } | ||
|
|
||
| type logger struct { | ||
|
|
@@ -152,6 +156,7 @@ func fmtDuration(d time.Duration) string { | |
| // TB is the interface common to T and B. | ||
| type TB interface { | ||
| Cleanup(func()) | ||
| Context() context.Context | ||
| Error(args ...interface{}) | ||
| Errorf(format string, args ...interface{}) | ||
| Fail() | ||
|
|
@@ -307,6 +312,15 @@ func (c *common) Cleanup(f func()) { | |
| c.cleanups = append(c.cleanups, f) | ||
| } | ||
|
|
||
| // Context returns a context that is canceled just before | ||
| // Cleanup-registered functions are called. | ||
| // | ||
| // Cleanup functions can wait for any resources | ||
| // that shut down on [context.Context.Done] before the test or benchmark completes. | ||
| func (c *common) Context() context.Context { | ||
| return c.ctx | ||
| } | ||
|
|
||
| // TempDir returns a temporary directory for the test to use. | ||
| // The directory is automatically removed by Cleanup when the test and | ||
| // all its subtests complete. | ||
|
|
@@ -447,6 +461,9 @@ func (c *common) runCleanup() { | |
| if cleanup == nil { | ||
| return | ||
| } | ||
| if c.cancelCtx != nil { | ||
| c.cancelCtx() | ||
| } | ||
| cleanup() | ||
| } | ||
| } | ||
|
|
@@ -488,12 +505,15 @@ func (t *T) Run(name string, f func(t *T)) bool { | |
| } | ||
|
|
||
| // Create a subtest. | ||
| ctx, cancelCtx := context.WithCancel(context.Background()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems kinda weird that the parent context is not from the parent There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh wait, there's specifically a comment on this in the Go stdlib:
|
||
| sub := T{ | ||
| common: common{ | ||
| output: &logger{logToStdout: flagVerbose}, | ||
| name: testName, | ||
| parent: &t.common, | ||
| level: t.level + 1, | ||
| output: &logger{logToStdout: flagVerbose}, | ||
| name: testName, | ||
| parent: &t.common, | ||
| level: t.level + 1, | ||
| ctx: ctx, | ||
| cancelCtx: cancelCtx, | ||
| }, | ||
| context: t.context, | ||
| } | ||
|
|
@@ -606,9 +626,12 @@ func runTests(matchString func(pat, str string) (bool, error), tests []InternalT | |
| ok = true | ||
|
|
||
| ctx := newTestContext(newMatcher(matchString, flagRunRegexp, "-test.run", flagSkipRegexp)) | ||
| runCtx, cancelCtx := context.WithCancel(context.Background()) | ||
| t := &T{ | ||
| common: common{ | ||
| output: &logger{logToStdout: flagVerbose}, | ||
| output: &logger{logToStdout: flagVerbose}, | ||
| ctx: runCtx, | ||
| cancelCtx: cancelCtx, | ||
| }, | ||
| context: ctx, | ||
| } | ||
|
|
||