Skip to content

Commit

Permalink
quic: avoid leaking tls goroutines in tests
Browse files Browse the repository at this point in the history
Change-Id: Iaf273294ba3245bfeb387a72e068c048d0fcf93a
Reviewed-on: https://go-review.googlesource.com/c/net/+/547736
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
neild committed Dec 7, 2023
1 parent 08a78b1 commit 65efbad
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions internal/quic/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ func newTestConnForConn(t *testing.T, endpoint *testEndpoint, conn *Conn) *testC
}
tc.peerTLSConn.SetTransportParameters(marshalTransportParameters(peerProvidedParams))
tc.peerTLSConn.Start(context.Background())
t.Cleanup(func() {
tc.peerTLSConn.Close()
})

return tc
}
Expand Down
52 changes: 52 additions & 0 deletions internal/quic/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.21

package quic

import (
"bytes"
"fmt"
"os"
"runtime"
"testing"
"time"
)

func TestMain(m *testing.M) {
defer os.Exit(m.Run())

// Look for leaked goroutines.
//
// Checking after every test makes it easier to tell which test is the culprit,
// but checking once at the end is faster and less likely to miss something.
start := time.Now()
warned := false
for {
buf := make([]byte, 2<<20)
buf = buf[:runtime.Stack(buf, true)]
leaked := false
for _, g := range bytes.Split(buf, []byte("\n\n")) {
if bytes.Contains(g, []byte("quic.TestMain")) ||
bytes.Contains(g, []byte("created by os/signal.Notify")) ||
bytes.Contains(g, []byte("gotraceback_test.go")) {
continue
}
leaked = true
}
if !leaked {
break
}
if !warned && time.Since(start) > 1*time.Second {
// Print a warning quickly, in case this is an interactive session.
// Keep waiting until the test times out, in case this is a slow trybot.
fmt.Printf("Tests seem to have leaked some goroutines, still waiting.\n\n")
fmt.Print(string(buf))
warned = true
}
// Goroutines might still be shutting down.
time.Sleep(1 * time.Millisecond)
}
}
4 changes: 3 additions & 1 deletion internal/quic/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,9 @@ func initialClientCrypto(t *testing.T, e *testEndpoint, p transportParameters) [
tlsClient := tls.QUICClient(config)
tlsClient.SetTransportParameters(marshalTransportParameters(p))
tlsClient.Start(context.Background())
//defer tlsClient.Close()
t.Cleanup(func() {
tlsClient.Close()
})
e.peerTLSConn = tlsClient
var data []byte
for {
Expand Down

0 comments on commit 65efbad

Please sign in to comment.