github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/sampler/sampler_test.go (about) 1 // Copyright (c) 2018 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 sampler 22 23 import ( 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 "gopkg.in/yaml.v2" 29 ) 30 31 func TestInvalidSampleRate(t *testing.T) { 32 _, err := NewSampler(-1.0) 33 require.Error(t, err) 34 35 _, err = NewSampler(0.0) 36 require.NoError(t, err) 37 38 _, err = NewSampler(1.0) 39 require.NoError(t, err) 40 41 _, err = NewSampler(2.0) 42 require.Error(t, err) 43 } 44 45 func TestSampler(t *testing.T) { 46 s, err := NewSampler(0.5) 47 require.NoError(t, err) 48 require.True(t, s.Sample()) 49 require.False(t, s.Sample()) 50 require.True(t, s.Sample()) 51 require.False(t, s.Sample()) 52 require.True(t, s.Sample()) 53 54 s, err = NewSampler(0.25) 55 require.NoError(t, err) 56 require.True(t, s.Sample()) 57 require.False(t, s.Sample()) 58 require.False(t, s.Sample()) 59 require.False(t, s.Sample()) 60 require.True(t, s.Sample()) 61 require.False(t, s.Sample()) 62 require.False(t, s.Sample()) 63 require.False(t, s.Sample()) 64 require.True(t, s.Sample()) 65 } 66 67 func TestRateUnmarshalYAML(t *testing.T) { 68 type config struct { 69 Rate Rate `yaml:"rate"` 70 } 71 72 tests := []struct { 73 input []byte 74 expectValue float64 75 expectErr bool 76 }{ 77 { 78 input: []byte("rate: foo\n"), 79 expectValue: 0, 80 expectErr: true, 81 }, 82 { 83 input: []byte("rate: -1\n"), 84 expectValue: 0, 85 expectErr: true, 86 }, 87 { 88 input: []byte("\n"), 89 expectValue: 0, 90 expectErr: false, 91 }, 92 { 93 input: []byte("rate: 0\n"), 94 expectValue: 0, 95 expectErr: false, 96 }, 97 { 98 input: []byte("rate: 1\n"), 99 expectValue: 1, 100 expectErr: false, 101 }, 102 { 103 input: []byte("rate: 1.01\n"), 104 expectValue: 0, 105 expectErr: true, 106 }, 107 } 108 109 for _, test := range tests { 110 t.Run(string(test.input), func(t *testing.T) { 111 var c config 112 err := yaml.Unmarshal(test.input, &c) 113 if test.expectErr { 114 require.Error(t, err) 115 } else { 116 require.NoError(t, err) 117 } 118 119 assert.Equal(t, test.expectValue, c.Rate.Value()) 120 }) 121 } 122 }