Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package testing

import (
"bytes"
"context"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -78,6 +79,9 @@ type common struct {
tempDir string
tempDirErr error
tempDirSeq int32

ctx context.Context
cancelCtx context.CancelFunc
}

type logger struct {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -447,6 +461,9 @@ func (c *common) runCleanup() {
if cleanup == nil {
return
}
if c.cancelCtx != nil {
c.cancelCtx()
}
cleanup()
}
}
Expand Down Expand Up @@ -488,12 +505,15 @@ func (t *T) Run(name string, f func(t *T)) bool {
}

// Create a subtest.
ctx, cancelCtx := context.WithCancel(context.Background())
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 testing.T, but this seems to be how it is in the Go stdlib.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:

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.

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,
}
Expand Down Expand Up @@ -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,
}
Expand Down
Loading