File: main_test.go

package info (click to toggle)
golang-golang-x-net 1%3A0.24.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 8,460 kB
  • sloc: asm: 18; makefile: 12; sh: 7
file content (57 lines) | stat: -rw-r--r-- 1,487 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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.
	if runtime.GOOS == "js" {
		// The js-wasm runtime creates an additional background goroutine.
		// Just skip the leak check there.
		return
	}
	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)
	}
}