]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: close HTTP response bodies in benchmark
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 28 Jan 2015 18:44:54 +0000 (12:44 -0600)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 28 Jan 2015 19:40:59 +0000 (19:40 +0000)
This should fix the race builders.

Change-Id: I9c9e7393d5e29d64ab797e346b34b1fa1dfe6d96
Reviewed-on: https://go-review.googlesource.com/3441
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
src/net/http/main_test.go
src/net/http/serve_test.go

index b8c71fd19fd21285d4ebb3721f58f1062433834b..c7407df707cea6c7457ee1b9719f5b4030e04a65 100644 (file)
@@ -75,7 +75,7 @@ func goroutineLeaked() bool {
        return true
 }
 
-func afterTest(t *testing.T) {
+func afterTest(t testing.TB) {
        http.DefaultTransport.(*http.Transport).CloseIdleConnections()
        if testing.Short() {
                return
index 68398656584367f57414b46737f41edc547d8bfd..85d5705137f0e29153d79a4848208d6091bde15e 100644 (file)
@@ -2902,11 +2902,23 @@ func BenchmarkServer(b *testing.B) {
        }
 }
 
+// getNoBody wraps Get but closes any Response.Body before returning the response.
+func getNoBody(urlStr string) (*Response, error) {
+       res, err := Get(urlStr)
+       if err != nil {
+               return nil, err
+       }
+       res.Body.Close()
+       return res, nil
+}
+
 // A benchmark for profiling the client without the HTTP server code.
 // The server code runs in a subprocess.
 func BenchmarkClient(b *testing.B) {
        b.ReportAllocs()
        b.StopTimer()
+       defer afterTest(b)
+
        port := os.Getenv("TEST_BENCH_SERVER_PORT") // can be set by user
        if port == "" {
                port = "39207"
@@ -2922,7 +2934,7 @@ func BenchmarkClient(b *testing.B) {
                        w.Header().Set("Content-Type", "text/html; charset=utf-8")
                        w.Write(data)
                })
-               log.Fatal(ListenAndServe(":"+port, nil))
+               log.Fatal(ListenAndServe("localhost:"+port, nil))
        }
 
        // Start server process.
@@ -2941,7 +2953,7 @@ func BenchmarkClient(b *testing.B) {
        url := "http://localhost:" + port + "/"
        for i := 0; i < 100; i++ {
                time.Sleep(50 * time.Millisecond)
-               if _, err := Get(url); err == nil {
+               if _, err := getNoBody(url); err == nil {
                        break
                }
                if i == 99 {
@@ -2968,7 +2980,7 @@ func BenchmarkClient(b *testing.B) {
        b.StopTimer()
 
        // Instruct server process to stop.
-       Get(url + "?stop=yes")
+       getNoBody(url + "?stop=yes")
        select {
        case err := <-done:
                if err != nil {
@@ -2977,7 +2989,6 @@ func BenchmarkClient(b *testing.B) {
        case <-time.After(5 * time.Second):
                b.Fatalf("subprocess did not stop")
        }
-       DefaultTransport.(*Transport).CloseIdleConnections()
 }
 
 func BenchmarkServerFakeConnNoKeepAlive(b *testing.B) {