]> Cypherpunks repositories - gostls13.git/commitdiff
net/http, net/http/httputil: make chunked reader alloc test more robust
authorBrad Fitzpatrick <bradfitz@golang.org>
Sun, 19 Jan 2014 18:02:10 +0000 (10:02 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sun, 19 Jan 2014 18:02:10 +0000 (10:02 -0800)
Use testing.AllocsPerRun now that it exists, instead of doing it by hand.

Fixes #6076

R=golang-codereviews, alex.brainman
CC=golang-codereviews
https://golang.org/cl/53810043

src/pkg/net/http/chunked_test.go
src/pkg/net/http/httputil/chunked_test.go

index 0b18c7b55ec23da26dee90affa869da71449dbae..ae32a69ea7783f8ba25ea4ea72b21b07a13158b4 100644 (file)
@@ -8,11 +8,11 @@
 package http
 
 import (
+       "bufio"
        "bytes"
        "fmt"
        "io"
        "io/ioutil"
-       "runtime"
        "testing"
 )
 
@@ -42,8 +42,6 @@ func TestChunk(t *testing.T) {
 }
 
 func TestChunkReaderAllocs(t *testing.T) {
-       // temporarily set GOMAXPROCS to 1 as we are testing memory allocations
-       defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
        var buf bytes.Buffer
        w := newChunkedWriter(&buf)
        a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
@@ -52,26 +50,23 @@ func TestChunkReaderAllocs(t *testing.T) {
        w.Write(c)
        w.Close()
 
-       r := newChunkedReader(&buf)
        readBuf := make([]byte, len(a)+len(b)+len(c)+1)
-
-       var ms runtime.MemStats
-       runtime.ReadMemStats(&ms)
-       m0 := ms.Mallocs
-
-       n, err := io.ReadFull(r, readBuf)
-
-       runtime.ReadMemStats(&ms)
-       mallocs := ms.Mallocs - m0
-       if mallocs > 1 {
-               t.Errorf("%d mallocs; want <= 1", mallocs)
-       }
-
-       if n != len(readBuf)-1 {
-               t.Errorf("read %d bytes; want %d", n, len(readBuf)-1)
-       }
-       if err != io.ErrUnexpectedEOF {
-               t.Errorf("read error = %v; want ErrUnexpectedEOF", err)
+       byter := bytes.NewReader(buf.Bytes())
+       bufr := bufio.NewReader(byter)
+       mallocs := testing.AllocsPerRun(10, func() {
+               byter.Seek(0, 0)
+               bufr.Reset(byter)
+               r := newChunkedReader(bufr)
+               n, err := io.ReadFull(r, readBuf)
+               if n != len(readBuf)-1 {
+                       t.Fatalf("read %d bytes; want %d", n, len(readBuf)-1)
+               }
+               if err != io.ErrUnexpectedEOF {
+                       t.Fatalf("read error = %v; want ErrUnexpectedEOF", err)
+               }
+       })
+       if mallocs > 1.5 {
+               t.Logf("mallocs = %v; want 1", mallocs)
        }
 }
 
index a06bffad5b30f7032cc7bd24a52ed537dbd40f29..3fb5fa52655826b634c910be06fd436786a590de 100644 (file)
 package httputil
 
 import (
+       "bufio"
        "bytes"
        "fmt"
        "io"
        "io/ioutil"
-       "runtime"
        "testing"
 )
 
@@ -44,8 +44,6 @@ func TestChunk(t *testing.T) {
 }
 
 func TestChunkReaderAllocs(t *testing.T) {
-       // temporarily set GOMAXPROCS to 1 as we are testing memory allocations
-       defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
        var buf bytes.Buffer
        w := NewChunkedWriter(&buf)
        a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
@@ -54,26 +52,23 @@ func TestChunkReaderAllocs(t *testing.T) {
        w.Write(c)
        w.Close()
 
-       r := NewChunkedReader(&buf)
        readBuf := make([]byte, len(a)+len(b)+len(c)+1)
-
-       var ms runtime.MemStats
-       runtime.ReadMemStats(&ms)
-       m0 := ms.Mallocs
-
-       n, err := io.ReadFull(r, readBuf)
-
-       runtime.ReadMemStats(&ms)
-       mallocs := ms.Mallocs - m0
-       if mallocs > 1 {
-               t.Errorf("%d mallocs; want <= 1", mallocs)
-       }
-
-       if n != len(readBuf)-1 {
-               t.Errorf("read %d bytes; want %d", n, len(readBuf)-1)
-       }
-       if err != io.ErrUnexpectedEOF {
-               t.Errorf("read error = %v; want ErrUnexpectedEOF", err)
+       byter := bytes.NewReader(buf.Bytes())
+       bufr := bufio.NewReader(byter)
+       mallocs := testing.AllocsPerRun(10, func() {
+               byter.Seek(0, 0)
+               bufr.Reset(byter)
+               r := NewChunkedReader(bufr)
+               n, err := io.ReadFull(r, readBuf)
+               if n != len(readBuf)-1 {
+                       t.Fatalf("read %d bytes; want %d", n, len(readBuf)-1)
+               }
+               if err != io.ErrUnexpectedEOF {
+                       t.Fatalf("read error = %v; want ErrUnexpectedEOF", err)
+               }
+       })
+       if mallocs > 1.5 {
+               t.Logf("mallocs = %v; want 1", mallocs)
        }
 }