Skip to content

Commit 75619d1

Browse files
howjmayBryan C. Mills
authored andcommitted
bytes: deflake TestGrow by using testing.AllocsPerRun
Fixes #36695 Change-Id: I4392246015252018b49f321a5a839cc68cc611d7 GitHub-Last-Rev: c2fb1f7 GitHub-Pull-Request: #36732 Reviewed-on: https://go-review.googlesource.com/c/go/+/216237 Reviewed-by: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 025a4fa commit 75619d1

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/bytes/buffer_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
. "bytes"
99
"io"
1010
"math/rand"
11-
"runtime"
1211
"testing"
1312
"unicode/utf8"
1413
)
@@ -495,20 +494,20 @@ func TestGrow(t *testing.T) {
495494
x := []byte{'x'}
496495
y := []byte{'y'}
497496
tmp := make([]byte, 72)
498-
for _, startLen := range []int{0, 100, 1000, 10000, 100000} {
499-
xBytes := Repeat(x, startLen)
500-
for _, growLen := range []int{0, 100, 1000, 10000, 100000} {
497+
for _, growLen := range []int{0, 100, 1000, 10000, 100000} {
498+
for _, startLen := range []int{0, 100, 1000, 10000, 100000} {
499+
xBytes := Repeat(x, startLen)
500+
501501
buf := NewBuffer(xBytes)
502502
// If we read, this affects buf.off, which is good to test.
503503
readBytes, _ := buf.Read(tmp)
504-
buf.Grow(growLen)
505504
yBytes := Repeat(y, growLen)
505+
allocs := testing.AllocsPerRun(100, func() {
506+
buf.Grow(growLen)
507+
buf.Write(yBytes)
508+
})
506509
// Check no allocation occurs in write, as long as we're single-threaded.
507-
var m1, m2 runtime.MemStats
508-
runtime.ReadMemStats(&m1)
509-
buf.Write(yBytes)
510-
runtime.ReadMemStats(&m2)
511-
if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs {
510+
if allocs != 0 {
512511
t.Errorf("allocation occurred during write")
513512
}
514513
// Check that buffer has correct data.

0 commit comments

Comments
 (0)