mirror of
https://github.com/golang/go
synced 2024-11-17 15:34:42 -07:00
src/bytes_test: fix flaky test in TestGrow
Use testing.AllocsPerRun to measure allocations in TestGrow. With testing.AllocsPerRun, the influence made by garbage collection would play a minor role for running test for multiple times. Fixes #36695
This commit is contained in:
parent
71239b4f49
commit
c2fb1f7ddb
@ -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 _, growLen := range []int{0, 100, 1000, 10000, 100000} {
|
||||
for _, startLen := range []int{0, 100, 1000, 10000, 100000} {
|
||||
xBytes := Repeat(x, startLen)
|
||||
for _, growLen := range []int{0, 100, 1000, 10000, 100000} {
|
||||
|
||||
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)
|
||||
// Check no allocation occurs in write, as long as we're single-threaded.
|
||||
var m1, m2 runtime.MemStats
|
||||
runtime.ReadMemStats(&m1)
|
||||
allocs := testing.AllocsPerRun(100, func() {
|
||||
buf.Grow(growLen)
|
||||
buf.Write(yBytes)
|
||||
runtime.ReadMemStats(&m2)
|
||||
if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs {
|
||||
})
|
||||
// Check no allocation occurs in write, as long as we're single-threaded.
|
||||
if allocs != 0 {
|
||||
t.Errorf("allocation occurred during write")
|
||||
}
|
||||
// Check that buffer has correct data.
|
||||
|
Loading…
Reference in New Issue
Block a user