github.com/kiali/kiali@v1.84.0/tracing/tempo/tempopb/pool/pool_test.go (about)

     1  // Forked with love from: https://github.com/prometheus/prometheus/tree/c954cd9d1d4e3530be2939d39d8633c38b70913f/util/pool
     2  
     3  package pool
     4  
     5  import (
     6  	"math/rand"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func makeFunc(size int) []byte {
    13  	return make([]byte, 0, size)
    14  }
    15  
    16  func TestPool(t *testing.T) {
    17  	testPool := New(1, 8, 2, makeFunc)
    18  	cases := []struct {
    19  		size        int
    20  		expectedCap int
    21  	}{
    22  		{
    23  			size:        -1,
    24  			expectedCap: 1,
    25  		},
    26  		{
    27  			size:        3,
    28  			expectedCap: 4,
    29  		},
    30  		{
    31  			size:        10,
    32  			expectedCap: 10,
    33  		},
    34  	}
    35  	for _, c := range cases {
    36  		ret := testPool.Get(c.size)
    37  		require.Equal(t, c.expectedCap, cap(ret))
    38  		testPool.Put(ret)
    39  	}
    40  }
    41  
    42  func TestPoolSlicesAreAlwaysLargeEnough(t *testing.T) {
    43  	testPool := New(1, 1024, 2, makeFunc)
    44  
    45  	for i := 0; i < 10000; i++ {
    46  		size := rand.Intn(1000)
    47  		externalSlice := make([]byte, 0, size)
    48  		testPool.Put(externalSlice)
    49  
    50  		size = rand.Intn(1000)
    51  		ret := testPool.Get(size)
    52  
    53  		require.True(t, cap(ret) >= size)
    54  	}
    55  }