]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: deflake TestResponseWriterWriteStringAllocs, test interface instead
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 13 Jan 2015 01:23:18 +0000 (17:23 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 13 Jan 2015 01:38:10 +0000 (01:38 +0000)
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 <alex.brainman@gmail.com>
src/net/http/serve_test.go

index 5e0a0053c01d842cc24039f51586941d677a889a..eb695e2549dd35f8b6c7fcb7d6de4171e5dc1c0e 100644 (file)
@@ -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")
        }
 }