Skip to content

Commit f4f8397

Browse files
committed
net/http: deflake TestIssue4191_InfiniteGetTimeout
This test exercises the case where a net.Conn error occurs while writing a response body. It injects an error by setting a timeout on the Conn. If this timeout expires before response headers are written, the test fails. The test attempts to recover from this failure by extending the timeout and retrying. Set the timeout after the response headers are removed, and remove the retry loop. Fixes #56274. Change-Id: I293f8bedb7b20a21d14f43ea9bb48fc56b59441c Reviewed-on: https://go-review.googlesource.com/c/go/+/452175 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Damien Neil <[email protected]>
1 parent c6cdfd8 commit f4f8397

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

src/net/http/transport_test.go

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,57 +2224,37 @@ func testTransportConcurrency(t *testing.T, mode testMode) {
22242224

22252225
func TestIssue4191_InfiniteGetTimeout(t *testing.T) { run(t, testIssue4191_InfiniteGetTimeout) }
22262226
func testIssue4191_InfiniteGetTimeout(t *testing.T, mode testMode) {
2227-
const debug = false
22282227
mux := NewServeMux()
22292228
mux.HandleFunc("/get", func(w ResponseWriter, r *Request) {
22302229
io.Copy(w, neverEnding('a'))
22312230
})
22322231
ts := newClientServerTest(t, mode, mux).ts
2233-
timeout := 100 * time.Millisecond
22342232

2233+
connc := make(chan net.Conn, 1)
22352234
c := ts.Client()
22362235
c.Transport.(*Transport).Dial = func(n, addr string) (net.Conn, error) {
22372236
conn, err := net.Dial(n, addr)
22382237
if err != nil {
22392238
return nil, err
22402239
}
2241-
conn.SetDeadline(time.Now().Add(timeout))
2242-
if debug {
2243-
conn = NewLoggingConn("client", conn)
2240+
select {
2241+
case connc <- conn:
2242+
default:
22442243
}
22452244
return conn, nil
22462245
}
22472246

2248-
getFailed := false
2249-
nRuns := 5
2250-
if testing.Short() {
2251-
nRuns = 1
2252-
}
2253-
for i := 0; i < nRuns; i++ {
2254-
if debug {
2255-
println("run", i+1, "of", nRuns)
2256-
}
2257-
sres, err := c.Get(ts.URL + "/get")
2258-
if err != nil {
2259-
if !getFailed {
2260-
// Make the timeout longer, once.
2261-
getFailed = true
2262-
t.Logf("increasing timeout")
2263-
i--
2264-
timeout *= 10
2265-
continue
2266-
}
2267-
t.Errorf("Error issuing GET: %v", err)
2268-
break
2269-
}
2270-
_, err = io.Copy(io.Discard, sres.Body)
2271-
if err == nil {
2272-
t.Errorf("Unexpected successful copy")
2273-
break
2274-
}
2247+
res, err := c.Get(ts.URL + "/get")
2248+
if err != nil {
2249+
t.Fatalf("Error issuing GET: %v", err)
22752250
}
2276-
if debug {
2277-
println("tests complete; waiting for handlers to finish")
2251+
defer res.Body.Close()
2252+
2253+
conn := <-connc
2254+
conn.SetDeadline(time.Now().Add(1 * time.Millisecond))
2255+
_, err = io.Copy(io.Discard, res.Body)
2256+
if err == nil {
2257+
t.Errorf("Unexpected successful copy")
22782258
}
22792259
}
22802260

0 commit comments

Comments
 (0)