2014-10-22 03:35:16 +04: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 integration
|
|
|
|
|
|
|
|
import (
|
2015-12-19 20:25:26 +03:00
|
|
|
"fmt"
|
2014-10-22 03:35:16 +04:00
|
|
|
"net/http"
|
2015-12-19 20:25:26 +03:00
|
|
|
"os"
|
2014-10-22 03:35:16 +04:00
|
|
|
"runtime"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-12-20 01:08:16 +03:00
|
|
|
var atLeastGo15 bool = false
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var major, minor int
|
|
|
|
var discard string
|
|
|
|
i, err := fmt.Sscanf(runtime.Version(), "go%d.%d%s", &major, &minor, &discard)
|
|
|
|
atLeastGo15 = (err == nil && i == 3 && (major > 1 || major == 1 && minor >= 5))
|
|
|
|
}
|
|
|
|
|
2014-10-22 03:35:16 +04: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 testing.RunTests") ||
|
|
|
|
strings.Contains(stack, "testing.Main(") ||
|
|
|
|
strings.Contains(stack, "runtime.goexit") ||
|
2015-12-19 20:25:26 +03:00
|
|
|
strings.Contains(stack, "github.com/coreos/etcd/integration.interestingGoroutines") ||
|
|
|
|
strings.Contains(stack, "github.com/coreos/etcd/pkg/logutil.(*MergeLogger).outputLoop") ||
|
|
|
|
strings.Contains(stack, "github.com/golang/glog.(*loggingT).flushDaemon") ||
|
2014-10-22 03:35:16 +04:00
|
|
|
strings.Contains(stack, "created by runtime.gc") ||
|
|
|
|
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-12-19 20:25:26 +03:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
v := m.Run()
|
|
|
|
if v == 0 && goroutineLeaked() {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func goroutineLeaked() bool {
|
2014-10-22 03:35:16 +04:00
|
|
|
if testing.Short() {
|
2015-12-19 20:25:26 +03:00
|
|
|
// not counting goroutines for leakage in -short mode
|
|
|
|
return false
|
2014-10-22 03:35:16 +04:00
|
|
|
}
|
|
|
|
gs := interestingGoroutines()
|
|
|
|
|
|
|
|
n := 0
|
|
|
|
stackCount := make(map[string]int)
|
|
|
|
for _, g := range gs {
|
|
|
|
stackCount[g]++
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
|
2015-12-19 20:25:26 +03:00
|
|
|
if n == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, "Too many goroutines running after integration test(s).\n")
|
|
|
|
for stack, count := range stackCount {
|
|
|
|
fmt.Fprintf(os.Stderr, "%d instances of:\n%s\n", count, stack)
|
2014-10-22 03:35:16 +04:00
|
|
|
}
|
2015-12-19 20:25:26 +03:00
|
|
|
return true
|
2014-10-22 03:35:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func afterTest(t *testing.T) {
|
|
|
|
http.DefaultTransport.(*http.Transport).CloseIdleConnections()
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var bad string
|
|
|
|
badSubstring := map[string]string{
|
|
|
|
").writeLoop(": "a Transport",
|
|
|
|
"created by net/http/httptest.(*Server).Start": "an httptest.Server",
|
|
|
|
"timeoutHandler": "a TimeoutHandler",
|
2015-12-19 20:25:26 +03:00
|
|
|
"net.(*netFD).connect(": "a timing out dial",
|
|
|
|
").noteClientGone(": "a closenotifier sender",
|
2014-10-22 03:35:16 +04:00
|
|
|
}
|
2015-12-20 01:05:46 +03:00
|
|
|
|
|
|
|
// readLoop was buggy before go1.5:
|
|
|
|
// https://github.com/golang/go/issues/10457
|
2015-12-20 01:08:16 +03:00
|
|
|
if atLeastGo15 {
|
2015-12-20 01:05:46 +03:00
|
|
|
badSubstring[").readLoop("] = "a Transport"
|
|
|
|
}
|
|
|
|
|
2014-10-22 03:35:16 +04:00
|
|
|
var stacks string
|
|
|
|
for i := 0; i < 6; 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(50 * time.Millisecond)
|
|
|
|
}
|
|
|
|
t.Errorf("Test appears to have leaked %s:\n%s", bad, stacks)
|
|
|
|
}
|