diff options
| author | Vladimir Varankin <vladimir@varank.in> | 2024-08-20 09:19:22 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2024-08-20 14:58:54 +0000 |
| commit | f432b5f756c223564fd397b7568bd2ce949c7c6f (patch) | |
| tree | 119a05f7aec6246b8844201608ddfc1787a5c9f1 /src/testing/testing_test.go | |
| parent | c6c9634515e6128a5acb8645dced62581f5d1b1b (diff) | |
| download | go-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_test.go')
| -rw-r--r-- | src/testing/testing_test.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/testing/testing_test.go b/src/testing/testing_test.go index af6035fd27..ff674fc3d1 100644 --- a/src/testing/testing_test.go +++ b/src/testing/testing_test.go @@ -6,6 +6,8 @@ package testing_test import ( "bytes" + "context" + "errors" "fmt" "internal/race" "internal/testenv" @@ -918,3 +920,29 @@ func TestParentRun(t1 *testing.T) { }) }) } + +func TestContext(t *testing.T) { + ctx := t.Context() + if err := ctx.Err(); err != nil { + t.Fatalf("expected non-canceled context, got %v", err) + } + + var innerCtx context.Context + t.Run("inner", func(t *testing.T) { + innerCtx = t.Context() + if err := innerCtx.Err(); err != nil { + t.Fatalf("expected inner test to not inherit canceled context, got %v", err) + } + }) + t.Run("inner2", func(t *testing.T) { + if !errors.Is(innerCtx.Err(), context.Canceled) { + t.Fatal("expected context of sibling test to be canceled after its test function finished") + } + }) + + t.Cleanup(func() { + if !errors.Is(ctx.Err(), context.Canceled) { + t.Fatal("expected context canceled before cleanup") + } + }) +} |
