From a9ca741d311772efb828c4b4f45e737783a89ca3 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 14 Sep 2022 14:21:23 -0400 Subject: [PATCH] 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 Run-TryBot: Bryan Mills Reviewed-by: Damien Neil Auto-Submit: Bryan Mills TryBot-Result: Gopher Robot --- src/net/http/serve_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index 21f23c652f7..dd1ae55b8af 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -5872,23 +5872,23 @@ func TestServerCancelsReadHeaderTimeoutWhenIdle(t *testing.T) { defer conn.Close() 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 { - 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 // 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 { - t.Fatalf("writing second request failed: %v", err) + return fmt.Errorf("writing second request failed: %v", err) } 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