Commit 6ddd995a authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: fix data race in TestTransportResponseHeaderTimeout

Fixes #7264

LGTM=dvyukov
R=dvyukov
CC=golang-codereviews
https://golang.org/cl/87970045
parent 0a8a719d
......@@ -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()
}
......
......@@ -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 {
......
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