Skip to content

Commit cd670a6

Browse files
committed
net/http: speed up tests, use t.Parallel when it's safe
Before: 8.9 seconds for go test -short After: 2.8 seconds There are still 250 tests without t.Parallel, but I got the important onces using a script: $ go test -short -v 2>&1 | go run ~/slowtests.go Where slowtests.go is https://play.golang.org/p/9mh5Wg1nLN The remaining 250 (the output lines from slowtests.go) all have a reported duration of 0ms, except one 50ms test which has to be serial. Where tests can't be parallel, I left a comment at the top saying why, so people don't add t.Parallel later and get surprised at failures. Updates #17751 Change-Id: Icbe32cbe2b996e23c89f1af6339287fa22af5115 Reviewed-on: https://go-review.googlesource.com/32684 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Josh Bleecher Snyder <[email protected]>
1 parent 9f58597 commit cd670a6

9 files changed

+127
-33
lines changed

src/net/http/client_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ func TestPostFormRequestFormat(t *testing.T) {
197197
}
198198

199199
func TestClientRedirects(t *testing.T) {
200+
setParallel(t)
200201
defer afterTest(t)
201202
var ts *httptest.Server
202203
ts = httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
@@ -294,6 +295,7 @@ func TestClientRedirects(t *testing.T) {
294295
}
295296

296297
func TestClientRedirectContext(t *testing.T) {
298+
setParallel(t)
297299
defer afterTest(t)
298300
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
299301
Redirect(w, r, "/", StatusTemporaryRedirect)
@@ -462,6 +464,7 @@ func testRedirectsByMethod(t *testing.T, method string, table []redirectTest, wa
462464
}
463465

464466
func TestClientRedirectUseResponse(t *testing.T) {
467+
setParallel(t)
465468
defer afterTest(t)
466469
const body = "Hello, world."
467470
var ts *httptest.Server
@@ -811,6 +814,7 @@ func TestClientWrites(t *testing.T) {
811814
}
812815

813816
func TestClientInsecureTransport(t *testing.T) {
817+
setParallel(t)
814818
defer afterTest(t)
815819
ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
816820
w.Write([]byte("Hello"))
@@ -1269,6 +1273,7 @@ func testClientTimeout_Headers(t *testing.T, h2 bool) {
12691273
func TestClientRedirectEatsBody_h1(t *testing.T) { testClientRedirectEatsBody(t, h1Mode) }
12701274
func TestClientRedirectEatsBody_h2(t *testing.T) { testClientRedirectEatsBody(t, h2Mode) }
12711275
func testClientRedirectEatsBody(t *testing.T, h2 bool) {
1276+
setParallel(t)
12721277
defer afterTest(t)
12731278
saw := make(chan string, 2)
12741279
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
@@ -1580,6 +1585,7 @@ func TestShouldCopyHeaderOnRedirect(t *testing.T) {
15801585
}
15811586

15821587
func TestClientRedirectTypes(t *testing.T) {
1588+
setParallel(t)
15831589
defer afterTest(t)
15841590

15851591
tests := [...]struct {

src/net/http/clientserver_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ func (tt h12Compare) reqFunc() reqFunc {
170170
}
171171

172172
func (tt h12Compare) run(t *testing.T) {
173+
setParallel(t)
173174
cst1 := newClientServerTest(t, false, HandlerFunc(tt.Handler), tt.Opts...)
174175
defer cst1.close()
175176
cst2 := newClientServerTest(t, true, HandlerFunc(tt.Handler), tt.Opts...)
@@ -938,6 +939,7 @@ func testStarRequest(t *testing.T, method string, h2 bool) {
938939

939940
// Issue 13957
940941
func TestTransportDiscardsUnneededConns(t *testing.T) {
942+
setParallel(t)
941943
defer afterTest(t)
942944
cst := newClientServerTest(t, h2Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
943945
fmt.Fprintf(w, "Hello, %v", r.RemoteAddr)
@@ -1022,6 +1024,7 @@ func TestTransportGCRequest_Body_h2(t *testing.T) { testTransportGCRequest(t,
10221024
func TestTransportGCRequest_NoBody_h1(t *testing.T) { testTransportGCRequest(t, h1Mode, false) }
10231025
func TestTransportGCRequest_NoBody_h2(t *testing.T) { testTransportGCRequest(t, h2Mode, false) }
10241026
func testTransportGCRequest(t *testing.T, h2, body bool) {
1027+
setParallel(t)
10251028
defer afterTest(t)
10261029
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
10271030
ioutil.ReadAll(r.Body)
@@ -1068,6 +1071,7 @@ func TestTransportRejectsInvalidHeaders_h2(t *testing.T) {
10681071
testTransportRejectsInvalidHeaders(t, h2Mode)
10691072
}
10701073
func testTransportRejectsInvalidHeaders(t *testing.T, h2 bool) {
1074+
setParallel(t)
10711075
defer afterTest(t)
10721076
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
10731077
fmt.Fprintf(w, "Handler saw headers: %q", r.Header)
@@ -1200,6 +1204,7 @@ func TestH12_AutoGzipWithDumpResponse(t *testing.T) {
12001204
func TestCloseIdleConnections_h1(t *testing.T) { testCloseIdleConnections(t, h1Mode) }
12011205
func TestCloseIdleConnections_h2(t *testing.T) { testCloseIdleConnections(t, h2Mode) }
12021206
func testCloseIdleConnections(t *testing.T, h2 bool) {
1207+
setParallel(t)
12031208
defer afterTest(t)
12041209
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
12051210
w.Header().Set("X-Addr", r.RemoteAddr)

src/net/http/fs_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ var ServeFileRangeTests = []struct {
6868
}
6969

7070
func TestServeFile(t *testing.T) {
71+
setParallel(t)
7172
defer afterTest(t)
7273
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
7374
ServeFile(w, r, "testdata/file")
@@ -1064,6 +1065,7 @@ func TestServeContentErrorMessages(t *testing.T) {
10641065

10651066
// verifies that sendfile is being used on Linux
10661067
func TestLinuxSendfile(t *testing.T) {
1068+
setParallel(t)
10671069
defer afterTest(t)
10681070
if runtime.GOOS != "linux" {
10691071
t.Skip("skipping; linux-only test")

src/net/http/http_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func TestCleanHost(t *testing.T) {
8282
// This catches accidental dependencies between the HTTP transport and
8383
// server code.
8484
func TestCmdGoNoHTTPServer(t *testing.T) {
85+
t.Parallel()
8586
goBin := testenv.GoToolPath(t)
8687
out, err := exec.Command(goBin, "tool", "nm", goBin).CombinedOutput()
8788
if err != nil {

src/net/http/npn_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
)
1919

2020
func TestNextProtoUpgrade(t *testing.T) {
21+
setParallel(t)
2122
defer afterTest(t)
2223
ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {
2324
fmt.Fprintf(w, "path=%s,proto=", r.URL.Path)

src/net/http/response_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ var readResponseCloseInMiddleTests = []struct {
589589
// reading only part of its contents advances the read to the end of
590590
// the request, right up until the next request.
591591
func TestReadResponseCloseInMiddle(t *testing.T) {
592+
t.Parallel()
592593
for _, test := range readResponseCloseInMiddleTests {
593594
fatalf := func(format string, args ...interface{}) {
594595
args = append([]interface{}{test.chunked, test.compressed}, args...)

0 commit comments

Comments
 (0)