github.com/grafana/pyroscope@v1.18.0/pkg/util/bufferpool/pool_test.go (about)

     1  package bufferpool
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func Test_returnPool(t *testing.T) {
    10  	assert.EqualValues(t, 0, returnPool(512, 0)) // Buffers can be added to the pool.
    11  	assert.EqualValues(t, 1, returnPool(513, 0))
    12  	assert.EqualValues(t, 1, returnPool(1<<10, 0))
    13  	assert.EqualValues(t, -1, returnPool(0, 0))    // Empty buffers are ignored.
    14  	assert.EqualValues(t, -1, returnPool(0, 10))   //
    15  	assert.EqualValues(t, 5, returnPool(1<<14, 0)) // New buffers are added to the appropriate pool.
    16  	assert.EqualValues(t, 5, returnPool(1<<14, 3)) // Buffer of a capacity exceeding the next power of two are relocated.
    17  	assert.EqualValues(t, 4, returnPool(1<<14, 4)) // Buffer of a capacity not exceeding the next power of two are retained.
    18  	assert.EqualValues(t, 5, returnPool(1<<14, 5)) // Buffer of the nominal capacity.
    19  	assert.EqualValues(t, 5, returnPool(1<<14, 6)) // Buffer of a smaller capacity must be relocated.
    20  	assert.EqualValues(t, 21, returnPool(1<<30, 13))
    21  	assert.EqualValues(t, -1, returnPool(1<<30+1, 13)) // No pools for buffers larger than 4MB.
    22  }