net/http: use synctest.Test rather than Run

Use the non-experimental Test function.
As a bonus, this lets us drop the hacks we were doing to support
t.Cleanup inside bubbles.

Change-Id: I070624e1384494e9d5fcfee594cfbb7680c1beda
Reviewed-on: https://go-review.googlesource.com/c/go/+/675315
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>
This commit is contained in:
Damien Neil 2025-05-21 13:24:49 -07:00 committed by Gopher Robot
parent 3cc8b532f9
commit a473a0dbc4
3 changed files with 10 additions and 30 deletions

View File

@ -15,7 +15,6 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"hash" "hash"
"internal/synctest"
"io" "io"
"log" "log"
"maps" "maps"
@ -34,6 +33,7 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
"testing/synctest"
"time" "time"
) )
@ -95,33 +95,13 @@ func run[T TBRun[T]](t T, f func(t T, mode testMode), opts ...any) {
} }
} }
// cleanupT wraps a testing.T and adds its own Cleanup method.
// Used to execute cleanup functions within a synctest bubble.
type cleanupT struct {
*testing.T
cleanups []func()
}
// Cleanup replaces T.Cleanup.
func (t *cleanupT) Cleanup(f func()) {
t.cleanups = append(t.cleanups, f)
}
func (t *cleanupT) done() {
for _, f := range slices.Backward(t.cleanups) {
f()
}
}
// runSynctest is run combined with synctest.Run. // runSynctest is run combined with synctest.Run.
// //
// The TB passed to f arranges for cleanup functions to be run in the synctest bubble. // The TB passed to f arranges for cleanup functions to be run in the synctest bubble.
func runSynctest(t *testing.T, f func(t testing.TB, mode testMode), opts ...any) { func runSynctest(t *testing.T, f func(t *testing.T, mode testMode), opts ...any) {
run(t, func(t *testing.T, mode testMode) { run(t, func(t *testing.T, mode testMode) {
synctest.Run(func() { synctest.Test(t, func(t *testing.T) {
ct := &cleanupT{T: t} f(t, mode)
defer ct.done()
f(ct, mode)
}) })
}, opts...) }, opts...)
} }
@ -292,12 +272,12 @@ func TestNewClientServerTest(t *testing.T) {
}, modes) }, modes)
}) })
t.Run("synctest", func(t *testing.T) { t.Run("synctest", func(t *testing.T) {
runSynctest(t, func(t testing.TB, mode testMode) { runSynctest(t, func(t *testing.T, mode testMode) {
testNewClientServerTest(t, mode, optFakeNet) testNewClientServerTest(t, mode, optFakeNet)
}, modes) }, modes)
}) })
} }
func testNewClientServerTest(t testing.TB, mode testMode, opts ...any) { func testNewClientServerTest(t *testing.T, mode testMode, opts ...any) {
var got struct { var got struct {
sync.Mutex sync.Mutex
proto string proto string

View File

@ -5855,7 +5855,7 @@ func testServerShutdown(t *testing.T, mode testMode) {
} }
func TestServerShutdownStateNew(t *testing.T) { runSynctest(t, testServerShutdownStateNew) } func TestServerShutdownStateNew(t *testing.T) { runSynctest(t, testServerShutdownStateNew) }
func testServerShutdownStateNew(t testing.TB, mode testMode) { func testServerShutdownStateNew(t *testing.T, mode testMode) {
if testing.Short() { if testing.Short() {
t.Skip("test takes 5-6 seconds; skipping in short mode") t.Skip("test takes 5-6 seconds; skipping in short mode")
} }

View File

@ -4230,7 +4230,7 @@ func TestTransportIdleConnRacesRequest(t *testing.T) {
// block the connection closing. // block the connection closing.
runSynctest(t, testTransportIdleConnRacesRequest, []testMode{http1Mode, http2UnencryptedMode}) runSynctest(t, testTransportIdleConnRacesRequest, []testMode{http1Mode, http2UnencryptedMode})
} }
func testTransportIdleConnRacesRequest(t testing.TB, mode testMode) { func testTransportIdleConnRacesRequest(t *testing.T, mode testMode) {
if mode == http2UnencryptedMode { if mode == http2UnencryptedMode {
t.Skip("remove skip when #70515 is fixed") t.Skip("remove skip when #70515 is fixed")
} }
@ -4305,7 +4305,7 @@ func testTransportIdleConnRacesRequest(t testing.TB, mode testMode) {
func TestTransportRemovesConnsAfterIdle(t *testing.T) { func TestTransportRemovesConnsAfterIdle(t *testing.T) {
runSynctest(t, testTransportRemovesConnsAfterIdle) runSynctest(t, testTransportRemovesConnsAfterIdle)
} }
func testTransportRemovesConnsAfterIdle(t testing.TB, mode testMode) { func testTransportRemovesConnsAfterIdle(t *testing.T, mode testMode) {
if testing.Short() { if testing.Short() {
t.Skip("skipping in short mode") t.Skip("skipping in short mode")
} }
@ -4351,7 +4351,7 @@ func testTransportRemovesConnsAfterIdle(t testing.TB, mode testMode) {
func TestTransportRemovesConnsAfterBroken(t *testing.T) { func TestTransportRemovesConnsAfterBroken(t *testing.T) {
runSynctest(t, testTransportRemovesConnsAfterBroken) runSynctest(t, testTransportRemovesConnsAfterBroken)
} }
func testTransportRemovesConnsAfterBroken(t testing.TB, mode testMode) { func testTransportRemovesConnsAfterBroken(t *testing.T, mode testMode) {
if testing.Short() { if testing.Short() {
t.Skip("skipping in short mode") t.Skip("skipping in short mode")
} }