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

     1  package pool
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/sy_183/go-common/unit"
     6  	"testing"
     7  )
     8  
     9  func TestRingBufferPool(t *testing.T) {
    10  	p := NewRingBufferPool(2, unit.MeBiByte, 2048)
    11  	for i := 0; i < 10; i++ {
    12  		var blocks []*Data
    13  		for j := 0; ; j++ {
    14  			buf := p.Get()
    15  			if buf == nil {
    16  				fmt.Println("alloc full")
    17  				break
    18  			}
    19  			size := len(buf)
    20  			if size > 1400 {
    21  				size = 1400
    22  			}
    23  			data := p.Alloc(uint(size))
    24  			fmt.Printf("alloc data(seq:%d,size:%d)\n", j, size)
    25  			blocks = append(blocks, data)
    26  		}
    27  		for _, block := range blocks {
    28  			block.Release()
    29  		}
    30  		blocks = blocks[:0]
    31  	}
    32  }