2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
26 var mCVect map[string]CounterVec
27 var mGVect map[string]GaugeVec
29 func TestMetricSetup(t *testing.T) {
30 mCVect = Metric.RegisterCounterVecGroup(
32 {Name: "counter1", Help: "counter1"},
34 []string{"name", "event"},
37 mGVect = Metric.RegisterGaugeVecGroup(
39 {Name: "counter2", Help: "counter2"},
41 []string{"name", "event"},
46 func TestMetricCounter(t *testing.T) {
47 var TestCounterOpts = []CounterOpts{
48 {Name: "Blaah1", Help: "Blaah1"},
49 {Name: "Blaah2", Help: "Blaah2"},
50 {Name: "Blaah3", Help: "Blaah3"},
51 {Name: "Blaah4", Help: "Blaah4"},
54 ret1 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounter")
57 t.Errorf("ret1 counter group is empty")
60 ret2 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounter")
63 t.Errorf("ret2 counter group is empty")
66 if len(ret1) != len(ret2) {
67 t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2))
71 func TestMetricCounterVector(t *testing.T) {
74 c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCVect)
75 if _, ok := c_grp1["counter1"]; ok == false {
76 t.Errorf("c_grp1 counter1 not exists")
78 c_grp1["counter1"].Inc()
82 c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCVect)
83 if _, ok := c_grp2["counter1"]; ok == false {
84 t.Errorf("c_grp2 counter1 not exists")
86 c_grp2["counter1"].Inc()
89 func TestMetricGaugeVector(t *testing.T) {
92 g_grp1 := Metric.GetGaugeGroupFromVects([]string{"name1", "event1"}, mGVect)
93 if _, ok := g_grp1["counter2"]; ok == false {
94 t.Errorf("g_grp1 counter2 not exists")
96 g_grp1["counter2"].Inc()
100 g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGVect)
101 if _, ok := g_grp2["counter2"]; ok == false {
102 t.Errorf("g_grp2 counter2 not exists")
104 g_grp2["counter2"].Inc()
107 func TestMetricCounterVectorPrefix(t *testing.T) {
110 c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
111 if _, ok := c_grp1["event1_counter1"]; ok == false {
112 t.Errorf("c_grp1 event1_counter1 not exists")
114 c_grp1["event1_counter1"].Inc()
118 c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
119 if _, ok := c_grp2["event2_counter1"]; ok == false {
120 t.Errorf("c_grp2 event2_counter1 not exists")
122 c_grp2["event2_counter1"].Inc()
126 m_grp := NewMetricGroupsCache()
127 m_grp.CombineCounterGroups(c_grp1, c_grp2)
131 if m_grp.CIs("event1_counter1") == false {
132 t.Errorf("m_grp event1_counter1 not exists")
134 m_grp.CInc("event1_counter1")
138 if m_grp.CIs("event2_counter1") == false {
139 t.Errorf("m_grp event2_counter1 not exists")
141 m_grp.CInc("event2_counter1")
144 func TestMetricGaugeVectorPrefix(t *testing.T) {
147 g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
148 if _, ok := g_grp1["event1_counter2"]; ok == false {
149 t.Errorf("g_grp1 event1_counter2 not exists")
151 g_grp1["event1_counter2"].Inc()
155 g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
156 if _, ok := g_grp2["event2_counter2"]; ok == false {
157 t.Errorf("g_grp2 event2_counter2 not exists")
159 g_grp2["event2_counter2"].Inc()
161 m_grp := NewMetricGroupsCache()
162 m_grp.CombineGaugeGroups(g_grp1, g_grp2)
166 if m_grp.GIs("event1_counter2") == false {
167 t.Errorf("m_grp event1_counter2 not exists")
169 m_grp.GInc("event1_counter2")
173 if m_grp.GIs("event2_counter2") == false {
174 t.Errorf("m_grp event2_counter2 not exists")
176 m_grp.GInc("event2_counter2")
179 func TestMetricGroupCache(t *testing.T) {
182 c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
183 if _, ok := c_grp1["event1_counter1"]; ok == false {
184 t.Errorf("c_grp1 event1_counter1 not exists")
186 c_grp1["event1_counter1"].Inc()
190 c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
191 if _, ok := c_grp2["event2_counter1"]; ok == false {
192 t.Errorf("c_grp2 event2_counter1 not exists")
194 c_grp2["event2_counter1"].Inc()
198 g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
199 if _, ok := g_grp1["event1_counter2"]; ok == false {
200 t.Errorf("g_grp1 event1_counter2 not exists")
202 g_grp1["event1_counter2"].Inc()
206 g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
207 if _, ok := g_grp2["event2_counter2"]; ok == false {
208 t.Errorf("g_grp2 event2_counter2 not exists")
210 g_grp2["event2_counter2"].Inc()
214 m_grp := NewMetricGroupsCache()
215 m_grp.CombineCounterGroups(c_grp1)
216 m_grp.CombineCounterGroups(c_grp2)
217 m_grp.CombineGaugeGroups(g_grp1)
218 m_grp.CombineGaugeGroups(g_grp2)
221 t.Errorf("Cache failed")
224 if m_grp.CIs("event1_counter1") == false {
225 t.Errorf("m_grp.Counters event1_counter1 not exists")
227 m_grp.CInc("event1_counter1")
229 if m_grp.CIs("event2_counter1") == false {
230 t.Errorf("m_grp.Counters event2_counter1 not exists")
232 m_grp.CInc("event2_counter1")
234 if m_grp.GIs("event1_counter2") == false {
235 t.Errorf("m_grp.Gauges event1_counter2 not exists")
237 m_grp.GInc("event1_counter2")
239 if m_grp.GIs("event2_counter2") == false {
240 t.Errorf("m_grp.Gauges event2_counter2 not exists")
242 m_grp.GInc("event2_counter2")