internal/http3: fix build of tests with GOEXPERIMENT=nosynctest · golang/net@ebed060 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
1 +// Copyright 2024 The Go Authors. All rights reserved.
2 +// Use of this source code is governed by a BSD-style
3 +// license that can be found in the LICENSE file.
4 +
5 +//go:build go1.24 && goexperiment.synctest
6 +
7 +package http3
8 +
9 +import (
10 +"slices"
11 +"testing"
12 +"testing/synctest"
13 +)
14 +
15 +// runSynctest runs f in a synctest.Run bubble.
16 +// It arranges for t.Cleanup functions to run within the bubble.
17 +func runSynctest(t *testing.T, f func(t testing.TB)) {
18 +synctest.Run(func() {
19 +ct := &cleanupT{T: t}
20 +defer ct.done()
21 +f(ct)
22 + })
23 +}
24 +
25 +// runSynctestSubtest runs f in a subtest in a synctest.Run bubble.
26 +func runSynctestSubtest(t *testing.T, name string, f func(t testing.TB)) {
27 +t.Run(name, func(t *testing.T) {
28 +runSynctest(t, f)
29 + })
30 +}
31 +
32 +// cleanupT wraps a testing.T and adds its own Cleanup method.
33 +// Used to execute cleanup functions within a synctest bubble.
34 +type cleanupT struct {
35 +*testing.T
36 +cleanups []func()
37 +}
38 +
39 +// Cleanup replaces T.Cleanup.
40 +func (t *cleanupT) Cleanup(f func()) {
41 +t.cleanups = append(t.cleanups, f)
42 +}
43 +
44 +func (t *cleanupT) done() {
45 +for _, f := range slices.Backward(t.cleanups) {
46 +f()
47 + }
48 +}
Original file line number Diff line number Diff line change
@@ -2,17 +2,14 @@
2 2 // Use of this source code is governed by a BSD-style
3 3 // license that can be found in the LICENSE file.
4 4
5 -//go:build go1.24 && goexperiment.synctest
5 +//go:build go1.24
6 6
7 7 package http3
8 8
9 9 import (
10 10 "encoding/hex"
11 11 "os"
12 -"slices"
13 12 "strings"
14 -"testing"
15 -"testing/synctest"
16 13 )
17 14
18 15 func init() {
@@ -25,41 +22,6 @@ func init() {
25 22 os.Setenv("GODEBUG", os.Getenv("GODEBUG")+",asynctimerchan=0")
26 23 }
27 24
28 -// runSynctest runs f in a synctest.Run bubble.
29 -// It arranges for t.Cleanup functions to run within the bubble.
30 -func runSynctest(t *testing.T, f func(t testing.TB)) {
31 -synctest.Run(func() {
32 -ct := &cleanupT{T: t}
33 -defer ct.done()
34 -f(ct)
35 - })
36 -}
37 -
38 -// runSynctestSubtest runs f in a subtest in a synctest.Run bubble.
39 -func runSynctestSubtest(t *testing.T, name string, f func(t testing.TB)) {
40 -t.Run(name, func(t *testing.T) {
41 -runSynctest(t, f)
42 - })
43 -}
44 -
45 -// cleanupT wraps a testing.T and adds its own Cleanup method.
46 -// Used to execute cleanup functions within a synctest bubble.
47 -type cleanupT struct {
48 -*testing.T
49 -cleanups []func()
50 -}
51 -
52 -// Cleanup replaces T.Cleanup.
53 -func (t *cleanupT) Cleanup(f func()) {
54 -t.cleanups = append(t.cleanups, f)
55 -}
56 -
57 -func (t *cleanupT) done() {
58 -for _, f := range slices.Backward(t.cleanups) {
59 -f()
60 - }
61 -}
62 -
63 25 func unhex(s string) []byte {
64 26 b, err := hex.DecodeString(strings.Map(func(c rune) rune {
65 27 switch c {
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
2 2 // Use of this source code is governed by a BSD-style
3 3 // license that can be found in the LICENSE file.
4 4
5 -//go:build go1.24
5 +//go:build go1.24 && goexperiment.synctest
6 6
7 7 package http3
8 8