1
0
mirror of https://github.com/golang/go synced 2024-11-23 15:20:03 -07:00

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 <alex.brainman@gmail.com>
This commit is contained in:
Brad Fitzpatrick 2015-01-12 17:23:18 -08:00
parent 1a27c07c6f
commit 3ab4b68bc1

View 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")
}
}