Commit 4b30d7ce authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

http: improve a test

Should prevent failures on slow machines, such as:
http://godashboard.appspot.com/log/47b5cae591b7ad8908704e327f3b9b41945d7d5fecfc0c8c945d5545ece1a813

Verified the change (on a fast machine) by removing the
existing sleep, in which case the race happens ~50% of the
time with GOMAXPROCS > 1, but recovers gracefully with
retries.

R=rsc
CC=golang-dev
https://golang.org/cl/4441089
parent 54422c77
...@@ -256,26 +256,44 @@ func TestTransportServerClosingUnexpectedly(t *testing.T) { ...@@ -256,26 +256,44 @@ func TestTransportServerClosingUnexpectedly(t *testing.T) {
tr := &Transport{} tr := &Transport{}
c := &Client{Transport: tr} c := &Client{Transport: tr}
fetch := func(n int) string { fetch := func(n, retries int) string {
res, _, err := c.Get(ts.URL) condFatalf := func(format string, arg ...interface{}) {
if err != nil { if retries <= 0 {
t.Fatalf("error in req #%d, GET: %v", n, err) t.Fatalf(format, arg...)
}
t.Logf("retrying shortly after expected error: "+format, arg...)
time.Sleep(1e9 / int64(retries))
} }
body, err := ioutil.ReadAll(res.Body) for retries >= 0 {
if err != nil { retries--
t.Fatalf("error in req #%d, ReadAll: %v", n, err) res, _, err := c.Get(ts.URL)
if err != nil {
condFatalf("error in req #%d, GET: %v", n, err)
continue
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
condFatalf("error in req #%d, ReadAll: %v", n, err)
continue
}
res.Body.Close()
return string(body)
} }
res.Body.Close() panic("unreachable")
return string(body)
} }
body1 := fetch(1) body1 := fetch(1, 0)
body2 := fetch(2) body2 := fetch(2, 0)
ts.CloseClientConnections() // surprise! ts.CloseClientConnections() // surprise!
time.Sleep(25e6) // idle for a bit (test is inherently racey, but expectedly)
body3 := fetch(3) // This test has an expected race. Sleeping for 25 ms prevents
// it on most fast machines, causing the next fetch() call to
// succeed quickly. But if we do get errors, fetch() will retry 5
// times with some delays between.
time.Sleep(25e6)
body3 := fetch(3, 5)
if body1 != body2 { if body1 != body2 {
t.Errorf("expected body1 and body2 to be equal") t.Errorf("expected body1 and body2 to be equal")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment