...

Source file src/golang.org/x/net/internal/quic/main_test.go

Documentation: golang.org/x/net/internal/quic

     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  	if runtime.GOOS == "js" {
    26  		// The js-wasm runtime creates an additional background goroutine.
    27  		// Just skip the leak check there.
    28  		return
    29  	}
    30  	start := time.Now()
    31  	warned := false
    32  	for {
    33  		buf := make([]byte, 2<<20)
    34  		buf = buf[:runtime.Stack(buf, true)]
    35  		leaked := false
    36  		for _, g := range bytes.Split(buf, []byte("\n\n")) {
    37  			if bytes.Contains(g, []byte("quic.TestMain")) ||
    38  				bytes.Contains(g, []byte("created by os/signal.Notify")) ||
    39  				bytes.Contains(g, []byte("gotraceback_test.go")) {
    40  				continue
    41  			}
    42  			leaked = true
    43  		}
    44  		if !leaked {
    45  			break
    46  		}
    47  		if !warned && time.Since(start) > 1*time.Second {
    48  			// Print a warning quickly, in case this is an interactive session.
    49  			// Keep waiting until the test times out, in case this is a slow trybot.
    50  			fmt.Printf("Tests seem to have leaked some goroutines, still waiting.\n\n")
    51  			fmt.Print(string(buf))
    52  			warned = true
    53  		}
    54  		// Goroutines might still be shutting down.
    55  		time.Sleep(1 * time.Millisecond)
    56  	}
    57  }
    58  

View as plain text