Commit 28b95edf authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: deflake tests in full mode after t.Parallel additions

https://golang.org/cl/18087 added a bunch of t.Parallel calls, which
aren't compatible with the afterTest func. But in short mode, afterTest
is a no-op. To keep all.bash (short mode) fast, conditionally set
t.Parallel when in short mode, but keep it unset for compatibility with
afterFunc otherwise.

Fixes #13804

Change-Id: Ie841fbc2544e1ffbee43ba1afbe895774e290da0
Reviewed-on: https://go-review.googlesource.com/18143Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent fa8384df
...@@ -79,6 +79,15 @@ func goroutineLeaked() bool { ...@@ -79,6 +79,15 @@ func goroutineLeaked() bool {
return true return true
} }
// 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 afterTest(t testing.TB) { func afterTest(t testing.TB) {
http.DefaultTransport.(*http.Transport).CloseIdleConnections() http.DefaultTransport.(*http.Transport).CloseIdleConnections()
if testing.Short() { if testing.Short() {
......
...@@ -455,7 +455,7 @@ func TestServerTimeouts(t *testing.T) { ...@@ -455,7 +455,7 @@ func TestServerTimeouts(t *testing.T) {
if runtime.GOOS == "plan9" { if runtime.GOOS == "plan9" {
t.Skip("skipping test; see https://golang.org/issue/7237") t.Skip("skipping test; see https://golang.org/issue/7237")
} }
t.Parallel() setParallel(t)
defer afterTest(t) defer afterTest(t)
reqNum := 0 reqNum := 0
ts := httptest.NewUnstartedServer(HandlerFunc(func(res ResponseWriter, req *Request) { ts := httptest.NewUnstartedServer(HandlerFunc(func(res ResponseWriter, req *Request) {
...@@ -939,7 +939,7 @@ func TestTLSHandshakeTimeout(t *testing.T) { ...@@ -939,7 +939,7 @@ func TestTLSHandshakeTimeout(t *testing.T) {
if runtime.GOOS == "plan9" { if runtime.GOOS == "plan9" {
t.Skip("skipping test; see https://golang.org/issue/7237") t.Skip("skipping test; see https://golang.org/issue/7237")
} }
t.Parallel() setParallel(t)
defer afterTest(t) defer afterTest(t)
ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {})) ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
errc := make(chanWriter, 10) // but only expecting 1 errc := make(chanWriter, 10) // but only expecting 1
......
...@@ -436,7 +436,7 @@ func TestTransportMaxPerHostIdleConns(t *testing.T) { ...@@ -436,7 +436,7 @@ func TestTransportMaxPerHostIdleConns(t *testing.T) {
} }
func TestTransportServerClosingUnexpectedly(t *testing.T) { func TestTransportServerClosingUnexpectedly(t *testing.T) {
t.Parallel() setParallel(t)
defer afterTest(t) defer afterTest(t)
ts := httptest.NewServer(hostPortHandler) ts := httptest.NewServer(hostPortHandler)
defer ts.Close() defer ts.Close()
...@@ -969,7 +969,7 @@ func TestTransportGzipShort(t *testing.T) { ...@@ -969,7 +969,7 @@ func TestTransportGzipShort(t *testing.T) {
// tests that persistent goroutine connections shut down when no longer desired. // tests that persistent goroutine connections shut down when no longer desired.
func TestTransportPersistConnLeak(t *testing.T) { func TestTransportPersistConnLeak(t *testing.T) {
t.Parallel() setParallel(t)
defer afterTest(t) defer afterTest(t)
gotReqCh := make(chan bool) gotReqCh := make(chan bool)
unblockCh := make(chan bool) unblockCh := make(chan bool)
...@@ -1036,7 +1036,7 @@ func TestTransportPersistConnLeak(t *testing.T) { ...@@ -1036,7 +1036,7 @@ func TestTransportPersistConnLeak(t *testing.T) {
// golang.org/issue/4531: Transport leaks goroutines when // golang.org/issue/4531: Transport leaks goroutines when
// request.ContentLength is explicitly short // request.ContentLength is explicitly short
func TestTransportPersistConnLeakShortBody(t *testing.T) { func TestTransportPersistConnLeakShortBody(t *testing.T) {
t.Parallel() setParallel(t)
defer afterTest(t) defer afterTest(t)
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
})) }))
...@@ -1377,7 +1377,7 @@ func TestIssue4191_InfiniteGetToPutTimeout(t *testing.T) { ...@@ -1377,7 +1377,7 @@ func TestIssue4191_InfiniteGetToPutTimeout(t *testing.T) {
} }
func TestTransportResponseHeaderTimeout(t *testing.T) { func TestTransportResponseHeaderTimeout(t *testing.T) {
t.Parallel() setParallel(t)
defer afterTest(t) defer afterTest(t)
if testing.Short() { if testing.Short() {
t.Skip("skipping timeout test in -short mode") t.Skip("skipping timeout test in -short mode")
...@@ -1449,7 +1449,7 @@ func TestTransportResponseHeaderTimeout(t *testing.T) { ...@@ -1449,7 +1449,7 @@ func TestTransportResponseHeaderTimeout(t *testing.T) {
} }
func TestTransportCancelRequest(t *testing.T) { func TestTransportCancelRequest(t *testing.T) {
t.Parallel() setParallel(t)
defer afterTest(t) defer afterTest(t)
if testing.Short() { if testing.Short() {
t.Skip("skipping test in -short mode") t.Skip("skipping test in -short mode")
...@@ -1559,7 +1559,7 @@ Get = Get http://something.no-network.tld/: net/http: request canceled while wai ...@@ -1559,7 +1559,7 @@ Get = Get http://something.no-network.tld/: net/http: request canceled while wai
} }
func TestCancelRequestWithChannel(t *testing.T) { func TestCancelRequestWithChannel(t *testing.T) {
t.Parallel() setParallel(t)
defer afterTest(t) defer afterTest(t)
if testing.Short() { if testing.Short() {
t.Skip("skipping test in -short mode") t.Skip("skipping test in -short mode")
...@@ -1617,7 +1617,7 @@ func TestCancelRequestWithChannel(t *testing.T) { ...@@ -1617,7 +1617,7 @@ func TestCancelRequestWithChannel(t *testing.T) {
} }
func TestCancelRequestWithChannelBeforeDo(t *testing.T) { func TestCancelRequestWithChannelBeforeDo(t *testing.T) {
t.Parallel() setParallel(t)
defer afterTest(t) defer afterTest(t)
unblockc := make(chan bool) unblockc := make(chan bool)
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
...@@ -2495,7 +2495,7 @@ func TestRetryIdempotentRequestsOnError(t *testing.T) { ...@@ -2495,7 +2495,7 @@ func TestRetryIdempotentRequestsOnError(t *testing.T) {
// Issue 6981 // Issue 6981
func TestTransportClosesBodyOnError(t *testing.T) { func TestTransportClosesBodyOnError(t *testing.T) {
t.Parallel() setParallel(t)
defer afterTest(t) defer afterTest(t)
readBody := make(chan error, 1) readBody := make(chan error, 1)
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
......
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