diff options
author | HowJMay <vulxj0j8j8@gmail.com> | 2020-02-26 18:19:18 +0000 |
---|---|---|
committer | Bryan C. Mills <bcmills@google.com> | 2020-02-26 18:40:56 +0000 |
commit | 75619d199f88bc4d411cefcf5735cbbff78d1ae2 (patch) | |
tree | 8366b541bed0b68b9aa8c6ff4f2b72c235b75508 /src/bytes | |
parent | 025a4faf5fd70b8be4a77d19762eb2b4da8754b0 (diff) | |
download | go-75619d199f88bc4d411cefcf5735cbbff78d1ae2.tar.gz go-75619d199f88bc4d411cefcf5735cbbff78d1ae2.zip |
bytes: deflake TestGrow by using testing.AllocsPerRun
Fixes #36695
Change-Id: I4392246015252018b49f321a5a839cc68cc611d7
GitHub-Last-Rev: c2fb1f7ddbe9b80059eed69f31781abe0a1db185
GitHub-Pull-Request: golang/go#36732
Reviewed-on: https://go-review.googlesource.com/c/go/+/216237
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/bytes')
-rw-r--r-- | src/bytes/buffer_test.go | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/src/bytes/buffer_test.go b/src/bytes/buffer_test.go index 7626d277d4..fec5ef8a35 100644 --- a/src/bytes/buffer_test.go +++ b/src/bytes/buffer_test.go @@ -8,7 +8,6 @@ import ( . "bytes" "io" "math/rand" - "runtime" "testing" "unicode/utf8" ) @@ -495,20 +494,20 @@ func TestGrow(t *testing.T) { x := []byte{'x'} y := []byte{'y'} tmp := make([]byte, 72) - for _, startLen := range []int{0, 100, 1000, 10000, 100000} { - xBytes := Repeat(x, startLen) - for _, growLen := range []int{0, 100, 1000, 10000, 100000} { + for _, growLen := range []int{0, 100, 1000, 10000, 100000} { + for _, startLen := range []int{0, 100, 1000, 10000, 100000} { + xBytes := Repeat(x, startLen) + buf := NewBuffer(xBytes) // If we read, this affects buf.off, which is good to test. readBytes, _ := buf.Read(tmp) - buf.Grow(growLen) yBytes := Repeat(y, growLen) + allocs := testing.AllocsPerRun(100, func() { + buf.Grow(growLen) + buf.Write(yBytes) + }) // Check no allocation occurs in write, as long as we're single-threaded. - var m1, m2 runtime.MemStats - runtime.ReadMemStats(&m1) - buf.Write(yBytes) - runtime.ReadMemStats(&m2) - if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs { + if allocs != 0 { t.Errorf("allocation occurred during write") } // Check that buffer has correct data. |