Retro68/gcc/libgo/go/net/http/main_test.go

132 lines
3.4 KiB
Go
Raw Normal View History

2014-09-21 17:33:12 +00:00
// 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 (
2017-04-10 11:32:00 +00:00
"flag"
2015-08-28 15:33:40 +00:00
"fmt"
2014-09-21 17:33:12 +00:00
"net/http"
2015-08-28 15:33:40 +00:00
"os"
2014-09-21 17:33:12 +00:00
"runtime"
"sort"
"strings"
"testing"
"time"
)
2017-04-10 11:32:00 +00:00
var flaky = flag.Bool("flaky", false, "run known-flaky tests too")
2015-08-28 15:33:40 +00:00
func TestMain(m *testing.M) {
v := m.Run()
if v == 0 && goroutineLeaked() {
os.Exit(1)
}
os.Exit(v)
}
2014-09-21 17:33:12 +00:00
func interestingGoroutines() (gs []string) {
buf := make([]byte, 2<<20)
buf = buf[:runtime.Stack(buf, true)]
for _, g := range strings.Split(string(buf), "\n\n") {
sl := strings.SplitN(g, "\n", 2)
if len(sl) != 2 {
continue
}
stack := strings.TrimSpace(sl[1])
if stack == "" ||
strings.Contains(stack, "created by net.startServer") ||
strings.Contains(stack, "created by testing.RunTests") ||
strings.Contains(stack, "closeWriteAndWait") ||
strings.Contains(stack, "testing.Main(") ||
// These only show up with GOTRACEBACK=2; Issue 5005 (comment 28)
strings.Contains(stack, "runtime.goexit") ||
strings.Contains(stack, "created by runtime.gc") ||
2015-08-28 15:33:40 +00:00
strings.Contains(stack, "net/http_test.interestingGoroutines") ||
2014-09-21 17:33:12 +00:00
strings.Contains(stack, "runtime.MHeap_Scavenger") {
continue
}
gs = append(gs, stack)
}
sort.Strings(gs)
return
}
// Verify the other tests didn't leave any goroutines running.
2015-08-28 15:33:40 +00:00
func goroutineLeaked() bool {
2014-09-21 17:33:12 +00:00
if testing.Short() {
2015-08-28 15:33:40 +00:00
// not counting goroutines for leakage in -short mode
return false
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
var stackCount map[string]int
for i := 0; i < 5; i++ {
n := 0
stackCount = make(map[string]int)
gs := interestingGoroutines()
for _, g := range gs {
stackCount[g]++
n++
}
if n == 0 {
return false
}
// Wait for goroutines to schedule and die off:
time.Sleep(100 * time.Millisecond)
2015-08-28 15:33:40 +00:00
}
fmt.Fprintf(os.Stderr, "Too many goroutines running after net/http test(s).\n")
for stack, count := range stackCount {
fmt.Fprintf(os.Stderr, "%d instances of:\n%s\n", count, stack)
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
return true
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
// setParallel marks t as a parallel test if we're in short mode
// (all.bash), but as a serial test otherwise. Using t.Parallel isn't
// compatible with the afterTest func in non-short mode.
func setParallel(t *testing.T) {
if testing.Short() {
t.Parallel()
}
}
func setFlaky(t *testing.T, issue int) {
if !*flaky {
t.Skipf("skipping known flaky test; see golang.org/issue/%d", issue)
}
}
func afterTest(t testing.TB) {
2014-09-21 17:33:12 +00:00
http.DefaultTransport.(*http.Transport).CloseIdleConnections()
if testing.Short() {
return
}
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",
"net.(*netFD).connect(": "a timing out dial",
").noteClientGone(": "a closenotifier sender",
}
var stacks string
for i := 0; i < 4; i++ {
bad = ""
stacks = strings.Join(interestingGoroutines(), "\n\n")
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)
}