Commit 93e5cc22 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: replace z_last_test hack with testing.Main

LGTM=adg
R=rsc, adg
CC=golang-codereviews
https://golang.org/cl/144240043
parent 0be3176a
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
package http_test package http_test
import ( import (
"fmt"
"net/http" "net/http"
"os"
"runtime" "runtime"
"sort" "sort"
"strings" "strings"
...@@ -13,6 +15,14 @@ import ( ...@@ -13,6 +15,14 @@ import (
"time" "time"
) )
func TestMain(m *testing.M) {
v := m.Run()
if v == 0 && goroutineLeaked() {
os.Exit(1)
}
os.Exit(v)
}
func interestingGoroutines() (gs []string) { func interestingGoroutines() (gs []string) {
buf := make([]byte, 2<<20) buf := make([]byte, 2<<20)
buf = buf[:runtime.Stack(buf, true)] buf = buf[:runtime.Stack(buf, true)]
...@@ -30,6 +40,7 @@ func interestingGoroutines() (gs []string) { ...@@ -30,6 +40,7 @@ func interestingGoroutines() (gs []string) {
// These only show up with GOTRACEBACK=2; Issue 5005 (comment 28) // These only show up with GOTRACEBACK=2; Issue 5005 (comment 28)
strings.Contains(stack, "runtime.goexit") || strings.Contains(stack, "runtime.goexit") ||
strings.Contains(stack, "created by runtime.gc") || strings.Contains(stack, "created by runtime.gc") ||
strings.Contains(stack, "net/http_test.interestingGoroutines") ||
strings.Contains(stack, "runtime.MHeap_Scavenger") { strings.Contains(stack, "runtime.MHeap_Scavenger") {
continue continue
} }
...@@ -40,10 +51,10 @@ func interestingGoroutines() (gs []string) { ...@@ -40,10 +51,10 @@ func interestingGoroutines() (gs []string) {
} }
// Verify the other tests didn't leave any goroutines running. // 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 goroutineLeaked() bool {
func TestGoroutinesRunning(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("not counting goroutines for leakage in -short mode") // not counting goroutines for leakage in -short mode
return false
} }
gs := interestingGoroutines() gs := interestingGoroutines()
...@@ -54,13 +65,14 @@ func TestGoroutinesRunning(t *testing.T) { ...@@ -54,13 +65,14 @@ func TestGoroutinesRunning(t *testing.T) {
n++ n++
} }
t.Logf("num goroutines = %d", n) if n == 0 {
if n > 0 { return false
t.Error("Too many goroutines.") }
for stack, count := range stackCount { fmt.Fprintf(os.Stderr, "Too many goroutines running after net/http test(s).\n")
t.Logf("%d instances of:\n%s", count, stack) for stack, count := range stackCount {
} fmt.Fprintf(os.Stderr, "%d instances of:\n%s", count, stack)
} }
return true
} }
func afterTest(t *testing.T) { func afterTest(t *testing.T) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment