diff options
author | Damien Neil <dneil@google.com> | 2022-11-18 14:10:50 -0800 |
---|---|---|
committer | Damien Neil <dneil@google.com> | 2022-11-19 01:19:55 +0000 |
commit | f4f8397fed02b612c36c425fc9c5dce32408e21b (patch) | |
tree | 3eab46722211be405f8a9e4ccc4e8e565a5550cb /src/net/http/transport_test.go | |
parent | c6cdfd88c762ee746cd9579ae57e528f56f5dd00 (diff) | |
download | go-f4f8397fed02b612c36c425fc9c5dce32408e21b.tar.gz go-f4f8397fed02b612c36c425fc9c5dce32408e21b.zip |
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 <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
Diffstat (limited to 'src/net/http/transport_test.go')
-rw-r--r-- | src/net/http/transport_test.go | 48 |
1 files changed, 14 insertions, 34 deletions
diff --git a/src/net/http/transport_test.go b/src/net/http/transport_test.go index c0cabccab8..2bc83fd42b 100644 --- a/src/net/http/transport_test.go +++ b/src/net/http/transport_test.go @@ -2224,57 +2224,37 @@ func testTransportConcurrency(t *testing.T, mode testMode) { func TestIssue4191_InfiniteGetTimeout(t *testing.T) { run(t, testIssue4191_InfiniteGetTimeout) } func testIssue4191_InfiniteGetTimeout(t *testing.T, mode testMode) { - const debug = false mux := NewServeMux() mux.HandleFunc("/get", func(w ResponseWriter, r *Request) { io.Copy(w, neverEnding('a')) }) ts := newClientServerTest(t, mode, mux).ts - timeout := 100 * time.Millisecond + connc := make(chan net.Conn, 1) c := ts.Client() c.Transport.(*Transport).Dial = func(n, addr string) (net.Conn, error) { conn, err := net.Dial(n, addr) if err != nil { return nil, err } - conn.SetDeadline(time.Now().Add(timeout)) - if debug { - conn = NewLoggingConn("client", conn) + select { + case connc <- conn: + default: } return conn, nil } - getFailed := false - nRuns := 5 - if testing.Short() { - nRuns = 1 - } - for i := 0; i < nRuns; i++ { - if debug { - println("run", i+1, "of", nRuns) - } - sres, err := c.Get(ts.URL + "/get") - if err != nil { - if !getFailed { - // Make the timeout longer, once. - getFailed = true - t.Logf("increasing timeout") - i-- - timeout *= 10 - continue - } - t.Errorf("Error issuing GET: %v", err) - break - } - _, err = io.Copy(io.Discard, sres.Body) - if err == nil { - t.Errorf("Unexpected successful copy") - break - } + res, err := c.Get(ts.URL + "/get") + if err != nil { + t.Fatalf("Error issuing GET: %v", err) } - if debug { - println("tests complete; waiting for handlers to finish") + defer res.Body.Close() + + conn := <-connc + conn.SetDeadline(time.Now().Add(1 * time.Millisecond)) + _, err = io.Copy(io.Discard, res.Body) + if err == nil { + t.Errorf("Unexpected successful copy") } } |