From 1bab3a16db964e331097e314ef4c57e1ac44cc4a Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 26 Jun 2015 20:51:13 -0700 Subject: [PATCH] net/http: fix now-flaky TransportAndServerSharedBodyRace test 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 TryBot-Result: Gobot Gobot Reviewed-by: Andrew Gerrand --- src/net/http/serve_test.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index 02f1dbf20a..6c3c65641d 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -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 -- 2.50.0