1
0
mirror of https://github.com/golang/go synced 2024-11-07 17:56:21 -07:00

net/http: deflake TestClientTimeoutKillsConn_AfterHeaders

It was flaky on slower machines.

Per report at https://github.com/golang/go/issues/23399#issuecomment-405792381

Change-Id: I7cab02821f78b5ce02ea51089d7eb51723f9705f
Reviewed-on: https://go-review.googlesource.com/124835
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Brad Fitzpatrick 2018-07-18 21:37:56 +00:00
parent 3bb4be79ba
commit c54bc34483

View File

@ -4764,7 +4764,7 @@ func TestClientTimeoutKillsConn_AfterHeaders(t *testing.T) {
setParallel(t) setParallel(t)
defer afterTest(t) defer afterTest(t)
inHandler := make(chan net.Conn, 1) inHandler := make(chan net.Conn, 1)
handlerReadReturned := make(chan bool, 1) handlerResult := make(chan error, 1)
cst := newClientServerTest(t, h1Mode, HandlerFunc(func(w ResponseWriter, r *Request) { cst := newClientServerTest(t, h1Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
w.Header().Set("Content-Length", "100") w.Header().Set("Content-Length", "100")
w.(Flusher).Flush() w.(Flusher).Flush()
@ -4776,17 +4776,29 @@ func TestClientTimeoutKillsConn_AfterHeaders(t *testing.T) {
conn.Write([]byte("foo")) conn.Write([]byte("foo"))
inHandler <- conn inHandler <- conn
n, err := conn.Read([]byte{0}) n, err := conn.Read([]byte{0})
if n != 0 || err != io.EOF { // The error should be io.EOF or "read tcp
t.Errorf("unexpected Read result: %v, %v", n, err) // 127.0.0.1:35827->127.0.0.1:40290: read: connection
// reset by peer" depending on timing. Really we just
// care that it returns at all. But if it returns with
// data, that's weird.
if n != 0 || err == nil {
handlerResult <- fmt.Errorf("unexpected Read result: %v, %v", n, err)
return
} }
handlerReadReturned <- true handlerResult <- nil
})) }))
defer cst.close() defer cst.close()
const timeout = 50 * time.Millisecond // Set Timeout to something very long but non-zero to exercise
cst.c.Timeout = timeout // the codepaths that check for it. But rather than wait for it to fire
// (which would make the test slow), we send on the req.Cancel channel instead,
// which happens to exercise the same code paths.
cst.c.Timeout = time.Minute // just to be non-zero, not to hit it.
req, _ := NewRequest("GET", cst.ts.URL, nil)
cancel := make(chan struct{})
req.Cancel = cancel
res, err := cst.c.Get(cst.ts.URL) res, err := cst.c.Do(req)
if err != nil { if err != nil {
select { select {
case <-inHandler: case <-inHandler:
@ -4797,27 +4809,25 @@ func TestClientTimeoutKillsConn_AfterHeaders(t *testing.T) {
} }
} }
close(cancel)
got, err := ioutil.ReadAll(res.Body) got, err := ioutil.ReadAll(res.Body)
if err == nil { if err == nil {
t.Fatal("unexpected result") t.Fatalf("unexpected success; read %q, nil", got)
} }
t.Logf("Read %q, %v", got, err)
select { select {
case c := <-inHandler: case c := <-inHandler:
select { select {
case <-handlerReadReturned: case err := <-handlerResult:
// Success. if err != nil {
t.Errorf("handler: %v", err)
}
return return
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
t.Error("Handler's conn.Read seems to be stuck in Read") t.Error("Handler's conn.Read seems to be stuck in Read")
c.Close() // close it to unblock Handler c.Close() // close it to unblock Handler
} }
case <-time.After(timeout * 10): case <-time.After(5 * time.Second):
// If we didn't get into the Handler in 50ms, that probably means t.Fatal("timeout")
// the builder was just slow and the the Get failed in that time
// but never made it to the server. That's fine. We'll usually
// test the past above on faster machines.
t.Skip("skipping test on slow builder")
} }
} }