summaryrefslogtreecommitdiff
path: root/src/pkg/net/http/z_last_test.go
blob: 44095a8d9f8953e9232888561492fed3006410d2 (plain)
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
58
59
60
// Copyright 2013 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.

package http_test

import (
	"net/http"
	"runtime"
	"strings"
	"testing"
	"time"
)

// Verify the other tests didn't leave any goroutines running.
// This is in a file named z_last_test.go so it sorts at the end.
func TestGoroutinesRunning(t *testing.T) {
	n := runtime.NumGoroutine()
	t.Logf("num goroutines = %d", n)
	if n > 20 {
		// Currently 14 on Linux (blocked in epoll_wait,
		// waiting for on fds that are closed?), but give some
		// slop for now.
		buf := make([]byte, 1<<20)
		buf = buf[:runtime.Stack(buf, true)]
		t.Errorf("Too many goroutines:\n%s", buf)
	}
}

func checkLeakedTransports(t *testing.T) {
	http.DefaultTransport.(*http.Transport).CloseIdleConnections()
	if testing.Short() {
		return
	}
	buf := make([]byte, 1<<20)
	var stacks string
	var bad string
	badSubstring := map[string]string{
		").readLoop(":                                  "a Transport",
		").writeLoop(":                                 "a Transport",
		"created by net/http/httptest.(*Server).Start": "an httptest.Server",
		"timeoutHandler":                               "a TimeoutHandler",
	}
	for i := 0; i < 4; i++ {
		bad = ""
		stacks = string(buf[:runtime.Stack(buf, true)])
		for substr, what := range badSubstring {
			if strings.Contains(stacks, substr) {
				bad = what
			}
		}
		if bad == "" {
			return
		}
		// Bad stuff found, but goroutines might just still be
		// shutting down, so give it some time.
		time.Sleep(250 * time.Millisecond)
	}
	t.Errorf("Test appears to have leaked %s:\n%s", bad, stacks)
}