mirror of
https://github.com/golang/go
synced 2024-11-20 01:14:40 -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:
parent
0a8a719ded
commit
6ddd995af5
@ -5,7 +5,6 @@
|
|||||||
package httptest
|
package httptest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"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) {
|
func TestIssue7264(t *testing.T) {
|
||||||
if !*testIssue7264 {
|
|
||||||
t.Skip("skipping failing test for issue 7264")
|
|
||||||
}
|
|
||||||
for i := 0; i < 1000; i++ {
|
for i := 0; i < 1000; i++ {
|
||||||
func() {
|
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()
|
defer ts.Close()
|
||||||
tr := &http.Transport{
|
tr := &http.Transport{
|
||||||
ResponseHeaderTimeout: time.Nanosecond,
|
ResponseHeaderTimeout: time.Nanosecond,
|
||||||
@ -46,6 +43,7 @@ func TestIssue7264(t *testing.T) {
|
|||||||
defer tr.CloseIdleConnections()
|
defer tr.CloseIdleConnections()
|
||||||
c := &http.Client{Transport: tr}
|
c := &http.Client{Transport: tr}
|
||||||
res, err := c.Get(ts.URL)
|
res, err := c.Get(ts.URL)
|
||||||
|
<-inHandler
|
||||||
if err == nil {
|
if err == nil {
|
||||||
res.Body.Close()
|
res.Body.Close()
|
||||||
}
|
}
|
||||||
|
@ -1250,9 +1250,13 @@ func TestTransportResponseHeaderTimeout(t *testing.T) {
|
|||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("skipping timeout test in -short mode")
|
t.Skip("skipping timeout test in -short mode")
|
||||||
}
|
}
|
||||||
|
inHandler := make(chan bool, 1)
|
||||||
mux := NewServeMux()
|
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) {
|
mux.HandleFunc("/slow", func(w ResponseWriter, r *Request) {
|
||||||
|
inHandler <- true
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
})
|
})
|
||||||
ts := httptest.NewServer(mux)
|
ts := httptest.NewServer(mux)
|
||||||
@ -1275,6 +1279,12 @@ func TestTransportResponseHeaderTimeout(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
res, err := c.Get(ts.URL + tt.path)
|
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 {
|
if err != nil {
|
||||||
uerr, ok := err.(*url.Error)
|
uerr, ok := err.(*url.Error)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
Loading…
Reference in New Issue
Block a user