github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/metric/types_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 metric 22 23 import ( 24 "testing" 25 26 "github.com/m3db/m3/src/metrics/generated/proto/metricpb" 27 28 "github.com/stretchr/testify/require" 29 yaml "gopkg.in/yaml.v2" 30 ) 31 32 func TestTypeUnmarshalYAML(t *testing.T) { 33 inputs := []struct { 34 str string 35 expected Type 36 }{ 37 {str: "counter", expected: CounterType}, 38 {str: "timer", expected: TimerType}, 39 {str: "gauge", expected: GaugeType}, 40 } 41 for _, input := range inputs { 42 var typ Type 43 require.NoError(t, yaml.Unmarshal([]byte(input.str), &typ)) 44 require.Equal(t, input.expected, typ) 45 } 46 } 47 48 func TestTypeUnmarshalYAMLErrors(t *testing.T) { 49 inputs := []string{ 50 "huh", 51 "blah", 52 } 53 for _, input := range inputs { 54 var typ Type 55 err := yaml.Unmarshal([]byte(input), &typ) 56 require.Error(t, err) 57 require.Equal(t, "invalid metric type '"+input+"', valid types are: counter, timer, gauge", err.Error()) 58 } 59 } 60 61 func TestMustParseType(t *testing.T) { 62 require.Equal(t, CounterType, MustParseType("counter")) 63 require.Panics(t, func() { MustParseType("foo") }) 64 } 65 66 func TestTypeToProto(t *testing.T) { 67 inputs := []struct { 68 metricType Type 69 expected metricpb.MetricType 70 }{ 71 { 72 metricType: UnknownType, 73 expected: metricpb.MetricType_UNKNOWN, 74 }, 75 { 76 metricType: CounterType, 77 expected: metricpb.MetricType_COUNTER, 78 }, 79 { 80 metricType: TimerType, 81 expected: metricpb.MetricType_TIMER, 82 }, 83 { 84 metricType: GaugeType, 85 expected: metricpb.MetricType_GAUGE, 86 }, 87 } 88 89 for _, input := range inputs { 90 var mt metricpb.MetricType 91 require.NoError(t, input.metricType.ToProto(&mt)) 92 require.Equal(t, input.expected, mt) 93 } 94 } 95 96 func TestTypeToProtoBadType(t *testing.T) { 97 inputs := []Type{ 98 Type(1000), 99 } 100 101 for _, input := range inputs { 102 var mt metricpb.MetricType 103 require.Error(t, input.ToProto(&mt)) 104 } 105 } 106 107 func TestTypeFromProto(t *testing.T) { 108 inputs := []struct { 109 metricType metricpb.MetricType 110 expected Type 111 }{ 112 { 113 metricType: metricpb.MetricType_UNKNOWN, 114 expected: UnknownType, 115 }, 116 { 117 metricType: metricpb.MetricType_COUNTER, 118 expected: CounterType, 119 }, 120 { 121 metricType: metricpb.MetricType_TIMER, 122 expected: TimerType, 123 }, 124 { 125 metricType: metricpb.MetricType_GAUGE, 126 expected: GaugeType, 127 }, 128 } 129 130 var mt Type 131 for _, input := range inputs { 132 require.NoError(t, mt.FromProto(input.metricType)) 133 require.Equal(t, input.expected, mt) 134 } 135 } 136 137 func TestTypeFromProtoBadTypeProto(t *testing.T) { 138 inputs := []metricpb.MetricType{ 139 metricpb.MetricType(1000), 140 } 141 142 for _, input := range inputs { 143 var mt Type 144 require.Error(t, mt.FromProto(input)) 145 } 146 }