Skip to content

Commit

Permalink
net/http: speed up tests, use t.Parallel when it's safe
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bradfitz committed Nov 4, 2016
1 parent 9f58597 commit cd670a6
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 33 deletions.
6 changes: 6 additions & 0 deletions src/net/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func TestPostFormRequestFormat(t *testing.T) {
}

func TestClientRedirects(t *testing.T) {
setParallel(t)
defer afterTest(t)
var ts *httptest.Server
ts = httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
Expand Down Expand Up @@ -294,6 +295,7 @@ func TestClientRedirects(t *testing.T) {
}

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

func TestClientRedirectUseResponse(t *testing.T) {
setParallel(t)
defer afterTest(t)
const body = "Hello, world."
var ts *httptest.Server
Expand Down Expand Up @@ -811,6 +814,7 @@ func TestClientWrites(t *testing.T) {
}

func TestClientInsecureTransport(t *testing.T) {
setParallel(t)
defer afterTest(t)
ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
w.Write([]byte("Hello"))
Expand Down Expand Up @@ -1269,6 +1273,7 @@ func testClientTimeout_Headers(t *testing.T, h2 bool) {
func TestClientRedirectEatsBody_h1(t *testing.T) { testClientRedirectEatsBody(t, h1Mode) }
func TestClientRedirectEatsBody_h2(t *testing.T) { testClientRedirectEatsBody(t, h2Mode) }
func testClientRedirectEatsBody(t *testing.T, h2 bool) {
setParallel(t)
defer afterTest(t)
saw := make(chan string, 2)
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
Expand Down Expand Up @@ -1580,6 +1585,7 @@ func TestShouldCopyHeaderOnRedirect(t *testing.T) {
}

func TestClientRedirectTypes(t *testing.T) {
setParallel(t)
defer afterTest(t)

tests := [...]struct {
Expand Down
5 changes: 5 additions & 0 deletions src/net/http/clientserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (tt h12Compare) reqFunc() reqFunc {
}

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

// Issue 13957
func TestTransportDiscardsUnneededConns(t *testing.T) {
setParallel(t)
defer afterTest(t)
cst := newClientServerTest(t, h2Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
fmt.Fprintf(w, "Hello, %v", r.RemoteAddr)
Expand Down Expand Up @@ -1022,6 +1024,7 @@ func TestTransportGCRequest_Body_h2(t *testing.T) { testTransportGCRequest(t,
func TestTransportGCRequest_NoBody_h1(t *testing.T) { testTransportGCRequest(t, h1Mode, false) }
func TestTransportGCRequest_NoBody_h2(t *testing.T) { testTransportGCRequest(t, h2Mode, false) }
func testTransportGCRequest(t *testing.T, h2, body bool) {
setParallel(t)
defer afterTest(t)
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
ioutil.ReadAll(r.Body)
Expand Down Expand Up @@ -1068,6 +1071,7 @@ func TestTransportRejectsInvalidHeaders_h2(t *testing.T) {
testTransportRejectsInvalidHeaders(t, h2Mode)
}
func testTransportRejectsInvalidHeaders(t *testing.T, h2 bool) {
setParallel(t)
defer afterTest(t)
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
fmt.Fprintf(w, "Handler saw headers: %q", r.Header)
Expand Down Expand Up @@ -1200,6 +1204,7 @@ func TestH12_AutoGzipWithDumpResponse(t *testing.T) {
func TestCloseIdleConnections_h1(t *testing.T) { testCloseIdleConnections(t, h1Mode) }
func TestCloseIdleConnections_h2(t *testing.T) { testCloseIdleConnections(t, h2Mode) }
func testCloseIdleConnections(t *testing.T, h2 bool) {
setParallel(t)
defer afterTest(t)
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
w.Header().Set("X-Addr", r.RemoteAddr)
Expand Down
2 changes: 2 additions & 0 deletions src/net/http/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var ServeFileRangeTests = []struct {
}

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

// verifies that sendfile is being used on Linux
func TestLinuxSendfile(t *testing.T) {
setParallel(t)
defer afterTest(t)
if runtime.GOOS != "linux" {
t.Skip("skipping; linux-only test")
Expand Down
1 change: 1 addition & 0 deletions src/net/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func TestCleanHost(t *testing.T) {
// This catches accidental dependencies between the HTTP transport and
// server code.
func TestCmdGoNoHTTPServer(t *testing.T) {
t.Parallel()
goBin := testenv.GoToolPath(t)
out, err := exec.Command(goBin, "tool", "nm", goBin).CombinedOutput()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions src/net/http/npn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
)

func TestNextProtoUpgrade(t *testing.T) {
setParallel(t)
defer afterTest(t)
ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {
fmt.Fprintf(w, "path=%s,proto=", r.URL.Path)
Expand Down
1 change: 1 addition & 0 deletions src/net/http/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ var readResponseCloseInMiddleTests = []struct {
// reading only part of its contents advances the read to the end of
// the request, right up until the next request.
func TestReadResponseCloseInMiddle(t *testing.T) {
t.Parallel()
for _, test := range readResponseCloseInMiddleTests {
fatalf := func(format string, args ...interface{}) {
args = append([]interface{}{test.chunked, test.compressed}, args...)
Expand Down
Loading

0 comments on commit cd670a6

Please sign in to comment.