gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/pool/default-buffer-pool_test.go (about)

     1  package pool
     2  
     3  import (
     4  	"crypto/md5"
     5  	"gitee.com/sy_183/go-common/unit"
     6  	"math/rand"
     7  	"testing"
     8  )
     9  
    10  func TestDefaultBufferPool(t *testing.T) {
    11  	fill := func(buf []byte, ch byte, len int) {
    12  		for i := 0; i < len; i++ {
    13  			buf[i] = ch
    14  		}
    15  	}
    16  	pool := NewDefaultBufferPool(256*unit.KiBiByte, 2048, ProvideSlicePool[*Buffer])
    17  	var chunks []*Data
    18  	var md5s [][16]byte
    19  	ch := byte('0')
    20  	for i := 0; i < 1000000; i++ {
    21  		buf := pool.Get()
    22  		l := rand.Intn(800) + 600
    23  		fill(buf, ch, l)
    24  		md5s = append(md5s, md5.Sum(buf[:l]))
    25  		data := pool.Alloc(uint(l))
    26  		chunks = append(chunks, data)
    27  		if len(chunks) > 1000 {
    28  			for i, chunk := range chunks {
    29  				if md5.Sum(chunk.Data) != md5s[i] {
    30  					panic("")
    31  				}
    32  				chunk.Release()
    33  			}
    34  			chunks = chunks[:0]
    35  			md5s = md5s[:0]
    36  		}
    37  		if ch++; ch > 'z' {
    38  			ch = '0'
    39  		}
    40  	}
    41  }