]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: fix now-flaky TransportAndServerSharedBodyRace test
authorBrad Fitzpatrick <bradfitz@golang.org>
Sat, 27 Jun 2015 03:51:13 +0000 (20:51 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 29 Jun 2015 05:19:36 +0000 (05:19 +0000)
TestTransportAndServerSharedBodyRace got flaky after
issue #9662 was fixed by https://golang.org/cl/11412, which made
servers hang up on clients when a Handler stopped reading its body
early.

This test was affected by a race between the the two goroutines in the
test both only reading part of the request, which was an unnecessary
detail for what the test was trying to test (concurrent Read/Close
races on an *http.body)

Also remove an unused remnant from an old test from which this one was
derived. And make the test not deadlock when it fails. (which was why
the test was showing up as 2m timeouts on the dashboard)

Fixes #11418

Change-Id: Ic83d18aef7e09a9cd56ac15e22ebed75713026cb
Reviewed-on: https://go-review.googlesource.com/11610
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
src/net/http/serve_test.go

index 02f1dbf20a798da28378f7fcbd5f8da7fdcad04e..6c3c65641d3aa59c1c7ad295cc86f8b6e63a4c62 100644 (file)
@@ -2455,17 +2455,13 @@ func TestTransportAndServerSharedBodyRace(t *testing.T) {
 
        unblockBackend := make(chan bool)
        backend := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {
-               io.CopyN(rw, req.Body, bodySize/2)
+               io.CopyN(rw, req.Body, bodySize)
                <-unblockBackend
        }))
        defer backend.Close()
 
        backendRespc := make(chan *Response, 1)
        proxy := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {
-               if req.RequestURI == "/foo" {
-                       rw.Write([]byte("bar"))
-                       return
-               }
                req2, _ := NewRequest("POST", backend.URL, req.Body)
                req2.ContentLength = bodySize
 
@@ -2474,7 +2470,7 @@ func TestTransportAndServerSharedBodyRace(t *testing.T) {
                        t.Errorf("Proxy outbound request: %v", err)
                        return
                }
-               _, err = io.CopyN(ioutil.Discard, bresp.Body, bodySize/4)
+               _, err = io.CopyN(ioutil.Discard, bresp.Body, bodySize/2)
                if err != nil {
                        t.Errorf("Proxy copy error: %v", err)
                        return
@@ -2488,6 +2484,7 @@ func TestTransportAndServerSharedBodyRace(t *testing.T) {
        }))
        defer proxy.Close()
 
+       defer close(unblockBackend)
        req, _ := NewRequest("POST", proxy.URL, io.LimitReader(neverEnding('a'), bodySize))
        res, err := DefaultClient.Do(req)
        if err != nil {
@@ -2496,8 +2493,12 @@ func TestTransportAndServerSharedBodyRace(t *testing.T) {
 
        // Cleanup, so we don't leak goroutines.
        res.Body.Close()
-       close(unblockBackend)
-       (<-backendRespc).Body.Close()
+       select {
+       case res := <-backendRespc:
+               res.Body.Close()
+       default:
+               // We failed earlier. (e.g. on DefaultClient.Do(req2))
+       }
 }
 
 // Test that a hanging Request.Body.Read from another goroutine can't