github.com/sandwich-go/boost@v1.3.29/xpool/buffer_test.go (about)

     1  package xpool
     2  
     3  import (
     4  	. "github.com/smartystreets/goconvey/convey"
     5  	"testing"
     6  )
     7  
     8  func TestBufferPool(t *testing.T) {
     9  	Convey("bytes pool", t, func() {
    10  		debug = true
    11  		So(func() { NewSyncBytesPool(0, 100, 2) }, ShouldPanic)
    12  		p := NewSyncBytesPool(1, 100, 2)
    13  		buff0 := p.Alloc(6)
    14  		So(p.(*SyncBytesPool).allocTimesFromPool, ShouldEqual, 1)
    15  		buff1 := p.Alloc(101)
    16  		So(p.(*SyncBytesPool).allocTimesFromPool, ShouldEqual, 1)
    17  		p.Free(buff0)
    18  		So(p.(*SyncBytesPool).freeTimesToPool, ShouldEqual, 1)
    19  		p.Free(buff1)
    20  		So(p.(*SyncBytesPool).freeTimesToPool, ShouldEqual, 1)
    21  		var frame = make([]byte, 7)
    22  		p.Free(frame)
    23  		So(func() { _ = p.Alloc(8) }, ShouldNotPanic)
    24  
    25  		frame = make([]byte, 2, 2)
    26  		p = NewSyncBytesPool(4, 100, 2)
    27  		p.Free(frame)
    28  		So(func() { _ = p.Alloc(4) }, ShouldNotPanic)
    29  	})
    30  }