From 3ab4b68bc18927607d893bf747f8381c5902eb4a Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 12 Jan 2015 17:23:18 -0800 Subject: [PATCH] net/http: deflake TestResponseWriterWriteStringAllocs, test interface instead Skip the allocation testing (which was only used as a signal for whether the interface was implemented by ResponseWriter), and just test for it directly. Fixes #9575 Change-Id: Ie230f1d21b104537d5647e9c900a81509d692469 Reviewed-on: https://go-review.googlesource.com/2720 Reviewed-by: Alex Brainman --- src/net/http/serve_test.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index 5e0a0053c01..eb695e2549d 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -2384,18 +2384,24 @@ func TestRequestBodyCloseDoesntBlock(t *testing.T) { } } -func TestResponseWriterWriteStringAllocs(t *testing.T) { +// test that ResponseWriter implements io.stringWriter. +func TestResponseWriterWriteString(t *testing.T) { + okc := make(chan bool, 1) ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) { - if r.URL.Path == "/s" { - io.WriteString(w, "Hello world") - } else { - w.Write([]byte("Hello world")) + type stringWriter interface { + WriteString(s string) (n int, err error) } + _, ok := w.(stringWriter) + okc <- ok })) - before := testing.AllocsPerRun(50, func() { ht.rawResponse("GET / HTTP/1.0") }) - after := testing.AllocsPerRun(50, func() { ht.rawResponse("GET /s HTTP/1.0") }) - if int(after) >= int(before) { - t.Errorf("WriteString allocs of %v >= Write allocs of %v", after, before) + ht.rawResponse("GET / HTTP/1.0") + select { + case ok := <-okc: + if !ok { + t.Error("ResponseWriter did not implement io.stringWriter") + } + default: + t.Error("handler was never called") } }