1
0
mirror of https://github.com/golang/go synced 2024-11-21 17:54:39 -07:00

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
This commit is contained in:
Brad Fitzpatrick 2011-05-03 11:25:35 -07:00
parent 54422c7750
commit 4b30d7cead

View File

@ -256,26 +256,44 @@ func TestTransportServerClosingUnexpectedly(t *testing.T) {
tr := &Transport{}
c := &Client{Transport: tr}
fetch := func(n int) string {
fetch := func(n, retries int) string {
condFatalf := func(format string, arg ...interface{}) {
if retries <= 0 {
t.Fatalf(format, arg...)
}
t.Logf("retrying shortly after expected error: "+format, arg...)
time.Sleep(1e9 / int64(retries))
}
for retries >= 0 {
retries--
res, _, err := c.Get(ts.URL)
if err != nil {
t.Fatalf("error in req #%d, GET: %v", n, err)
condFatalf("error in req #%d, GET: %v", n, err)
continue
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("error in req #%d, ReadAll: %v", n, err)
condFatalf("error in req #%d, ReadAll: %v", n, err)
continue
}
res.Body.Close()
return string(body)
}
panic("unreachable")
}
body1 := fetch(1)
body2 := fetch(2)
body1 := fetch(1, 0)
body2 := fetch(2, 0)
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 {
t.Errorf("expected body1 and body2 to be equal")