mirror of
https://github.com/golang/go
synced 2024-11-23 10:30:03 -07:00
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 <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
parent
0bafe0e5b2
commit
1bab3a16db
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user