1
0
mirror of https://github.com/golang/go synced 2024-11-19 23:24:45 -07:00

net/http: fix data race in TestTransportResponseHeaderTimeout

Fixes #7264

LGTM=dvyukov
R=dvyukov
CC=golang-codereviews
https://golang.org/cl/87970045
This commit is contained in:
Brad Fitzpatrick 2014-04-16 11:32:16 -07:00
parent 0a8a719ded
commit 6ddd995af5
2 changed files with 16 additions and 8 deletions

View File

@ -5,7 +5,6 @@
package httptest
import (
"flag"
"io/ioutil"
"net/http"
"testing"
@ -30,15 +29,13 @@ func TestServer(t *testing.T) {
}
}
var testIssue7264 = flag.Bool("issue7264", false, "enable failing test for issue 7264")
func TestIssue7264(t *testing.T) {
if !*testIssue7264 {
t.Skip("skipping failing test for issue 7264")
}
for i := 0; i < 1000; i++ {
func() {
ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
inHandler := make(chan bool, 1)
ts := NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
inHandler <- true
}))
defer ts.Close()
tr := &http.Transport{
ResponseHeaderTimeout: time.Nanosecond,
@ -46,6 +43,7 @@ func TestIssue7264(t *testing.T) {
defer tr.CloseIdleConnections()
c := &http.Client{Transport: tr}
res, err := c.Get(ts.URL)
<-inHandler
if err == nil {
res.Body.Close()
}

View File

@ -1250,9 +1250,13 @@ func TestTransportResponseHeaderTimeout(t *testing.T) {
if testing.Short() {
t.Skip("skipping timeout test in -short mode")
}
inHandler := make(chan bool, 1)
mux := NewServeMux()
mux.HandleFunc("/fast", func(w ResponseWriter, r *Request) {})
mux.HandleFunc("/fast", func(w ResponseWriter, r *Request) {
inHandler <- true
})
mux.HandleFunc("/slow", func(w ResponseWriter, r *Request) {
inHandler <- true
time.Sleep(2 * time.Second)
})
ts := httptest.NewServer(mux)
@ -1275,6 +1279,12 @@ func TestTransportResponseHeaderTimeout(t *testing.T) {
}
for i, tt := range tests {
res, err := c.Get(ts.URL + tt.path)
select {
case <-inHandler:
case <-time.After(5 * time.Second):
t.Errorf("never entered handler for test index %d, %s", i, tt.path)
continue
}
if err != nil {
uerr, ok := err.(*url.Error)
if !ok {