Skip to content

Commit ef33bc0

Browse files
neildgopherbot
authored andcommitted
internal/http3: use bubbled context in synctest tests
Avoid using T.Context within a synctest bubble. If the Context's Done channel is created outside the bubble, waiting on it within the bubble is durably blocking. If it's created within the bubble, the testing package encounters a panic when closing it after CL 671960. Instead, create our own Context within the bubble and cancel it before the bubble is destroyed. This will be entirely obviated by synctest.Test, which creates a testing.T that returns a properly bubbled context. Change-Id: Iff93c296ccbc1ece8172cb0a60e626ea1bd895ad Reviewed-on: https://go-review.googlesource.com/c/net/+/675615 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Damien Neil <dneil@google.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
1 parent 919c6bc commit ef33bc0

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

internal/http3/http3_synctest_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@
77
package http3
88

99
import (
10+
"context"
1011
"slices"
1112
"testing"
1213
"testing/synctest"
1314
)
1415

1516
// runSynctest runs f in a synctest.Run bubble.
1617
// It arranges for t.Cleanup functions to run within the bubble.
18+
// TODO: Replace with synctest.Test, which handles all this properly.
1719
func runSynctest(t *testing.T, f func(t testing.TB)) {
1820
synctest.Run(func() {
19-
ct := &cleanupT{T: t}
21+
// Create a context within the bubble, rather than using t.Context.
22+
ctx, cancel := context.WithCancel(context.Background())
23+
ct := &cleanupT{
24+
T: t,
25+
ctx: ctx,
26+
cancel: cancel,
27+
}
2028
defer ct.done()
2129
f(ct)
2230
})
@@ -33,6 +41,8 @@ func runSynctestSubtest(t *testing.T, name string, f func(t testing.TB)) {
3341
// Used to execute cleanup functions within a synctest bubble.
3442
type cleanupT struct {
3543
*testing.T
44+
ctx context.Context
45+
cancel context.CancelFunc
3646
cleanups []func()
3747
}
3848

@@ -41,7 +51,13 @@ func (t *cleanupT) Cleanup(f func()) {
4151
t.cleanups = append(t.cleanups, f)
4252
}
4353

54+
// Context replaces T.Context.
55+
func (t *cleanupT) Context() context.Context {
56+
return t.ctx
57+
}
58+
4459
func (t *cleanupT) done() {
60+
t.cancel()
4561
for _, f := range slices.Backward(t.cleanups) {
4662
f()
4763
}

0 commit comments

Comments
 (0)