From c1d449c42c6ea3c61f4636d1e9412b660be6f6bd Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 15 Dec 2016 16:18:45 +0000 Subject: [PATCH] net/http: deflake TestServerTimeouts maybe I haven't been able to reproduce this one, but change a few suspect things in this test. Notably, using the global "Get" function and thus using the DefaultTransport was buggy in a parallel test. Then add some error checks and close a TCP connection. Hopefully the failure wasn't timing-related. Fixes #18036 (I hope) Change-Id: I4904e42e40b26d488cf82111424a1d4d46f42dae Reviewed-on: https://go-review.googlesource.com/34490 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/net/http/serve_test.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index 593b1f3cdd5..ab3c3461d7d 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -481,11 +481,11 @@ func TestServerTimeouts(t *testing.T) { if err != nil { t.Fatalf("http Get #1: %v", err) } - got, _ := ioutil.ReadAll(r.Body) + got, err := ioutil.ReadAll(r.Body) expected := "req=1" - if string(got) != expected { - t.Errorf("Unexpected response for request #1; got %q; expected %q", - string(got), expected) + if string(got) != expected || err != nil { + t.Errorf("Unexpected response for request #1; got %q ,%v; expected %q, nil", + string(got), err, expected) } // Slow client that should timeout. @@ -496,6 +496,7 @@ func TestServerTimeouts(t *testing.T) { } buf := make([]byte, 1) n, err := conn.Read(buf) + conn.Close() latency := time.Since(t1) if n != 0 || err != io.EOF { t.Errorf("Read = %v, %v, wanted %v, %v", n, err, 0, io.EOF) @@ -507,14 +508,14 @@ func TestServerTimeouts(t *testing.T) { // Hit the HTTP server successfully again, verifying that the // previous slow connection didn't run our handler. (that we // get "req=2", not "req=3") - r, err = Get(ts.URL) + r, err = c.Get(ts.URL) if err != nil { t.Fatalf("http Get #2: %v", err) } - got, _ = ioutil.ReadAll(r.Body) + got, err = ioutil.ReadAll(r.Body) expected = "req=2" - if string(got) != expected { - t.Errorf("Get #2 got %q, want %q", string(got), expected) + if string(got) != expected || err != nil { + t.Errorf("Get #2 got %q, %v, want %q, nil", string(got), err, expected) } if !testing.Short() {