1
0
mirror of https://github.com/golang/go synced 2024-11-26 15:36:59 -07:00

net/http: deflake TestServerCancelsReadHeaderTimeoutWhenIdle

Return errors instead of calling t.Fatalf for timing-sensitive failure
modes. Scale the Sleep call to the timeout (so that it isn't too short
for the longer durations).

Fixes #54891.

Change-Id: I574e85e121becdda9ab8ee6bfd37c18a549d366d
Reviewed-on: https://go-review.googlesource.com/c/go/+/430955
Reviewed-by: Joel Sing <joel@sing.id.au>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Bryan C. Mills 2022-09-14 14:21:23 -04:00 committed by Gopher Robot
parent 2addbf3f2a
commit a9ca741d31

View File

@ -5872,23 +5872,23 @@ func TestServerCancelsReadHeaderTimeoutWhenIdle(t *testing.T) {
defer conn.Close() defer conn.Close()
if _, err := conn.Write([]byte("GET / HTTP/1.1\r\nHost: e.com\r\n\r\n")); err != nil { if _, err := conn.Write([]byte("GET / HTTP/1.1\r\nHost: e.com\r\n\r\n")); err != nil {
t.Fatalf("writing first request failed: %v", err) return fmt.Errorf("writing first request failed: %v", err)
} }
if _, err := ReadResponse(br, nil); err != nil { if _, err := ReadResponse(br, nil); err != nil {
t.Fatalf("first response (before timeout) failed: %v", err) return fmt.Errorf("first response (before timeout) failed: %v", err)
} }
// wait for longer than the server's ReadHeaderTimeout, and then send // wait for longer than the server's ReadHeaderTimeout, and then send
// another request // another request
time.Sleep(timeout + 10*time.Millisecond) time.Sleep(timeout * 3 / 2)
if _, err := conn.Write([]byte("GET / HTTP/1.1\r\nHost: e.com\r\n\r\n")); err != nil { if _, err := conn.Write([]byte("GET / HTTP/1.1\r\nHost: e.com\r\n\r\n")); err != nil {
t.Fatalf("writing second request failed: %v", err) return fmt.Errorf("writing second request failed: %v", err)
} }
if _, err := ReadResponse(br, nil); err != nil { if _, err := ReadResponse(br, nil); err != nil {
t.Fatalf("second response (after timeout) failed: %v", err) return fmt.Errorf("second response (after timeout) failed: %v", err)
} }
return nil return nil