Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 65efbad

Browse files
committed
quic: avoid leaking tls goroutines in tests
Change-Id: Iaf273294ba3245bfeb387a72e068c048d0fcf93a Reviewed-on: https://go-review.googlesource.com/c/net/+/547736 Reviewed-by: Jonathan Amsterdam <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 08a78b1 commit 65efbad

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

internal/quic/conn_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ func newTestConnForConn(t *testing.T, endpoint *testEndpoint, conn *Conn) *testC
279279
}
280280
tc.peerTLSConn.SetTransportParameters(marshalTransportParameters(peerProvidedParams))
281281
tc.peerTLSConn.Start(context.Background())
282+
t.Cleanup(func() {
283+
tc.peerTLSConn.Close()
284+
})
282285

283286
return tc
284287
}

internal/quic/main_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2023 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.21
6+
7+
package quic
8+
9+
import (
10+
"bytes"
11+
"fmt"
12+
"os"
13+
"runtime"
14+
"testing"
15+
"time"
16+
)
17+
18+
func TestMain(m *testing.M) {
19+
defer os.Exit(m.Run())
20+
21+
// Look for leaked goroutines.
22+
//
23+
// Checking after every test makes it easier to tell which test is the culprit,
24+
// but checking once at the end is faster and less likely to miss something.
25+
start := time.Now()
26+
warned := false
27+
for {
28+
buf := make([]byte, 2<<20)
29+
buf = buf[:runtime.Stack(buf, true)]
30+
leaked := false
31+
for _, g := range bytes.Split(buf, []byte("\n\n")) {
32+
if bytes.Contains(g, []byte("quic.TestMain")) ||
33+
bytes.Contains(g, []byte("created by os/signal.Notify")) ||
34+
bytes.Contains(g, []byte("gotraceback_test.go")) {
35+
continue
36+
}
37+
leaked = true
38+
}
39+
if !leaked {
40+
break
41+
}
42+
if !warned && time.Since(start) > 1*time.Second {
43+
// Print a warning quickly, in case this is an interactive session.
44+
// Keep waiting until the test times out, in case this is a slow trybot.
45+
fmt.Printf("Tests seem to have leaked some goroutines, still waiting.\n\n")
46+
fmt.Print(string(buf))
47+
warned = true
48+
}
49+
// Goroutines might still be shutting down.
50+
time.Sleep(1 * time.Millisecond)
51+
}
52+
}

internal/quic/retry_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,9 @@ func initialClientCrypto(t *testing.T, e *testEndpoint, p transportParameters) [
533533
tlsClient := tls.QUICClient(config)
534534
tlsClient.SetTransportParameters(marshalTransportParameters(p))
535535
tlsClient.Start(context.Background())
536-
//defer tlsClient.Close()
536+
t.Cleanup(func() {
537+
tlsClient.Close()
538+
})
537539
e.peerTLSConn = tlsClient
538540
var data []byte
539541
for {

0 commit comments

Comments
 (0)