github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/pool/config_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package pool
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  	"gopkg.in/yaml.v2"
    28  
    29  	"github.com/m3db/m3/src/x/instrument"
    30  )
    31  
    32  func TestObjectPoolConfiguration(t *testing.T) {
    33  	cfg := ObjectPoolConfiguration{
    34  		Size: 1,
    35  		Watermark: WatermarkConfiguration{
    36  			RefillLowWatermark:  0.1,
    37  			RefillHighWatermark: 0.5,
    38  		},
    39  	}
    40  	opts := cfg.NewObjectPoolOptions(instrument.NewOptions()).(*objectPoolOptions)
    41  	require.Equal(t, 1, opts.size)
    42  	require.False(t, opts.Dynamic())
    43  	require.Equal(t, 0.1, opts.refillLowWatermark)
    44  	require.Equal(t, 0.5, opts.refillHighWatermark)
    45  
    46  	dynamicOpts := cfg.NewObjectPoolOptions(instrument.NewOptions())
    47  	require.False(t, dynamicOpts.Dynamic())
    48  	dynamicOpts = dynamicOpts.SetSize(-1)
    49  	require.True(t, dynamicOpts.Dynamic())
    50  }
    51  
    52  func TestDynamicObjectPoolConfiguration(t *testing.T) {
    53  	cfg := ObjectPoolConfiguration{
    54  		Size: DynamicPoolSize,
    55  	}
    56  	opts := cfg.NewObjectPoolOptions(instrument.NewOptions()).(*objectPoolOptions)
    57  	require.Equal(t, DynamicPoolSize, opts.Size())
    58  	require.True(t, opts.Dynamic())
    59  
    60  	cfg, err := cfgFromStr(`
    61  size: dynamic
    62  `)
    63  	require.NoError(t, err)
    64  
    65  	opts = cfg.NewObjectPoolOptions(instrument.NewOptions()).(*objectPoolOptions)
    66  	require.Equal(t, DynamicPoolSize, opts.Size())
    67  	require.True(t, opts.Dynamic())
    68  
    69  	_, err = cfgFromStr(`
    70  size: invalid
    71  `)
    72  	require.Error(t, err)
    73  
    74  	_, err = cfgFromStr(`
    75  size: -10
    76  `)
    77  	require.Error(t, err)
    78  }
    79  
    80  func TestBucketizedPoolConfiguration(t *testing.T) {
    81  	cfg := BucketizedPoolConfiguration{
    82  		Buckets: []BucketConfiguration{
    83  			{Count: 1, Capacity: 10},
    84  			{Count: 2, Capacity: 20},
    85  		},
    86  		Watermark: WatermarkConfiguration{
    87  			RefillLowWatermark:  0.1,
    88  			RefillHighWatermark: 0.5,
    89  		},
    90  	}
    91  	expectedBuckets := []Bucket{
    92  		{Count: 1, Capacity: 10},
    93  		{Count: 2, Capacity: 20},
    94  	}
    95  	require.Equal(t, expectedBuckets, cfg.NewBuckets())
    96  	opts := cfg.NewObjectPoolOptions(instrument.NewOptions()).(*objectPoolOptions)
    97  	require.Equal(t, 0.1, opts.refillLowWatermark)
    98  	require.Equal(t, 0.5, opts.refillHighWatermark)
    99  }
   100  
   101  func cfgFromStr(config string) (ObjectPoolConfiguration, error) {
   102  	cfg := ObjectPoolConfiguration{}
   103  	err := yaml.Unmarshal([]byte(config), &cfg)
   104  	return cfg, err
   105  }