github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/selfmonitor/metrics_imp_v2_test.go (about) 1 // Copyright 2024 iLogtail Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package selfmonitor 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 22 "github.com/alibaba/ilogtail/pkg/protocol" 23 ) 24 25 func TestStrMetricV2_Name(t *testing.T) { 26 type fields struct { 27 name string 28 value string 29 labels []*protocol.Log_Content 30 } 31 tests := []struct { 32 name string 33 fields fields 34 want string 35 }{ 36 { 37 name: "test_name", 38 fields: fields{ 39 name: "field", 40 value: "v", 41 }, 42 want: "field", 43 }, 44 { 45 name: "test_name", 46 fields: fields{ 47 name: "field", 48 value: "v", 49 labels: []*protocol.Log_Content{ 50 { 51 Key: "key", 52 Value: "value", 53 }, 54 }, 55 }, 56 want: "field", 57 }, 58 } 59 for _, tt := range tests { 60 t.Run(tt.name, func(t *testing.T) { 61 constLabels := map[string]string{} 62 for _, v := range tt.fields.labels { 63 constLabels[v.Key] = v.Value 64 } 65 metric := NewStringMetricVector(tt.fields.name, constLabels, nil).WithLabels() 66 metric.Set(tt.fields.value) 67 68 if got := metric.(*strMetricImp).Name(); got != tt.want { 69 t.Errorf("StrMetric.Name() = %v, want %v", got, tt.want) 70 } 71 }) 72 } 73 } 74 75 func TestStrMetricV2_Set(t *testing.T) { 76 type fields struct { 77 name string 78 value string 79 } 80 type args struct { 81 v string 82 } 83 tests := []struct { 84 name string 85 fields fields 86 args args 87 }{ 88 { 89 name: "1", 90 fields: fields{ 91 name: "n", 92 value: "v", 93 }, 94 args: args{ 95 "x", 96 }, 97 }, 98 } 99 for _, tt := range tests { 100 t.Run(tt.name, func(t *testing.T) { 101 constLabels := map[string]string{} 102 s := NewStringMetricVector(tt.fields.name, constLabels, nil).WithLabels() 103 s.Set(tt.args.v) 104 if s.Collect().Value != tt.args.v { 105 t.Errorf("fail %s != %s\n", s.Collect(), tt.args.v) 106 } 107 }) 108 } 109 } 110 111 func TestDelta(t *testing.T) { 112 ms := newMetricVector("test", CounterType, nil, nil) 113 delta := newDeltaCounter(ms, nil) 114 115 for i := 0; i < 1000; i++ { 116 delta.Add(int64(1)) 117 } 118 119 assert.Equal(t, float64(1000), delta.Collect().Value) 120 assert.Equal(t, float64(0), delta.Collect().Value) 121 122 for i := 0; i < 100000; i++ { 123 delta.Add(int64(1)) 124 } 125 126 assert.Equal(t, float64(100000), delta.Collect().Value) 127 assert.Equal(t, float64(0), delta.Collect().Value) 128 }