Commit 3ab4b68b authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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/2720Reviewed-by: default avatarAlex Brainman <alex.brainman@gmail.com>
parent 1a27c07c
...@@ -2384,18 +2384,24 @@ func TestRequestBodyCloseDoesntBlock(t *testing.T) { ...@@ -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) { ht := newHandlerTest(HandlerFunc(func(w ResponseWriter, r *Request) {
if r.URL.Path == "/s" { type stringWriter interface {
io.WriteString(w, "Hello world") WriteString(s string) (n int, err error)
} else {
w.Write([]byte("Hello world"))
} }
_, ok := w.(stringWriter)
okc <- ok
})) }))
before := testing.AllocsPerRun(50, func() { ht.rawResponse("GET / HTTP/1.0") }) ht.rawResponse("GET / HTTP/1.0")
after := testing.AllocsPerRun(50, func() { ht.rawResponse("GET /s HTTP/1.0") }) select {
if int(after) >= int(before) { case ok := <-okc:
t.Errorf("WriteString allocs of %v >= Write allocs of %v", after, before) if !ok {
t.Error("ResponseWriter did not implement io.stringWriter")
}
default:
t.Error("handler was never called")
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment