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
28 var mGGroup map[string]Gauge
30 func TestMetricSetup(t *testing.T) {
31 mCVect = Metric.RegisterCounterVecGroup(
33 {Name: "counter1", Help: "counter1"},
35 []string{"name", "event"},
38 mGVect = Metric.RegisterGaugeVecGroup(
40 {Name: "counter2", Help: "counter2"},
42 []string{"name", "event"},
45 mGGroup = Metric.RegisterGaugeGroup(
47 {Name: "counter3", Help: "counter3"},
52 func TestMetricCounter(t *testing.T) {
53 var TestCounterOpts = []CounterOpts{
54 {Name: "Blaah1", Help: "Blaah1"},
55 {Name: "Blaah2", Help: "Blaah2"},
56 {Name: "Blaah3", Help: "Blaah3"},
57 {Name: "Blaah4", Help: "Blaah4"},
60 ret1 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounter")
63 t.Errorf("ret1 counter group is empty")
66 ret2 := Metric.RegisterCounterGroup(TestCounterOpts, "TestMetricCounter")
69 t.Errorf("ret2 counter group is empty")
72 if len(ret1) != len(ret2) {
73 t.Errorf("ret1 len %d differs from ret2 len %d", len(ret1), len(ret2))
77 func TestMetricCounterVector(t *testing.T) {
80 c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCVect)
81 if _, ok := c_grp1["counter1"]; ok == false {
82 t.Errorf("c_grp1 counter1 not exists")
84 c_grp1["counter1"].Inc()
88 c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCVect)
89 if _, ok := c_grp2["counter1"]; ok == false {
90 t.Errorf("c_grp2 counter1 not exists")
92 c_grp2["counter1"].Inc()
95 func TestMetricGaugeVector(t *testing.T) {
98 g_grp1 := Metric.GetGaugeGroupFromVects([]string{"name1", "event1"}, mGVect)
99 if _, ok := g_grp1["counter2"]; ok == false {
100 t.Errorf("g_grp1 counter2 not exists")
102 g_grp1["counter2"].Inc()
106 g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGVect)
107 if _, ok := g_grp2["counter2"]; ok == false {
108 t.Errorf("g_grp2 counter2 not exists")
110 g_grp2["counter2"].Inc()
113 func TestMetricCounterVectorPrefix(t *testing.T) {
116 c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
117 if _, ok := c_grp1["event1_counter1"]; ok == false {
118 t.Errorf("c_grp1 event1_counter1 not exists")
120 c_grp1["event1_counter1"].Inc()
124 c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
125 if _, ok := c_grp2["event2_counter1"]; ok == false {
126 t.Errorf("c_grp2 event2_counter1 not exists")
128 c_grp2["event2_counter1"].Inc()
132 m_grp := NewMetricGroupsCache()
133 m_grp.CombineCounterGroups(c_grp1, c_grp2)
137 if m_grp.CIs("event1_counter1") == false {
138 t.Errorf("m_grp event1_counter1 not exists")
140 m_grp.CInc("event1_counter1")
144 if m_grp.CIs("event2_counter1") == false {
145 t.Errorf("m_grp event2_counter1 not exists")
148 m_grp.CAdd("event2_counter1", 1)
149 m_grp.CGet("event2_counter1")
152 func TestMetricGaugeVectorPrefix(t *testing.T) {
155 g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
156 if _, ok := g_grp1["event1_counter2"]; ok == false {
157 t.Errorf("g_grp1 event1_counter2 not exists")
159 g_grp1["event1_counter2"].Inc()
163 g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
164 if _, ok := g_grp2["event2_counter2"]; ok == false {
165 t.Errorf("g_grp2 event2_counter2 not exists")
167 g_grp2["event2_counter2"].Inc()
169 m_grp := NewMetricGroupsCache()
170 m_grp.CombineGaugeGroups(g_grp1, g_grp2)
174 if m_grp.GIs("event1_counter2") == false {
175 t.Errorf("m_grp event1_counter2 not exists")
177 m_grp.GInc("event1_counter2")
181 if m_grp.GIs("event2_counter2") == false {
182 t.Errorf("m_grp event2_counter2 not exists")
184 m_grp.GInc("event2_counter2")
186 m_grp.GGet("event2_counter2")
187 m_grp.GDec("event2_counter2")
188 m_grp.GSet("event2_counter2", 1)
191 func TestMetricGroupCache(t *testing.T) {
194 c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
195 if _, ok := c_grp1["event1_counter1"]; ok == false {
196 t.Errorf("c_grp1 event1_counter1 not exists")
198 c_grp1["event1_counter1"].Inc()
202 c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
203 if _, ok := c_grp2["event2_counter1"]; ok == false {
204 t.Errorf("c_grp2 event2_counter1 not exists")
206 c_grp2["event2_counter1"].Inc()
210 g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
211 if _, ok := g_grp1["event1_counter2"]; ok == false {
212 t.Errorf("g_grp1 event1_counter2 not exists")
214 g_grp1["event1_counter2"].Inc()
218 g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
219 if _, ok := g_grp2["event2_counter2"]; ok == false {
220 t.Errorf("g_grp2 event2_counter2 not exists")
222 g_grp2["event2_counter2"].Inc()
226 m_grp := NewMetricGroupsCache()
227 m_grp.CombineCounterGroups(c_grp1)
228 m_grp.CombineCounterGroups(c_grp2)
229 m_grp.CombineGaugeGroups(g_grp1)
230 m_grp.CombineGaugeGroups(g_grp2)
233 t.Errorf("Cache failed")
236 if m_grp.CIs("event1_counter1") == false {
237 t.Errorf("m_grp.Counters event1_counter1 not exists")
239 m_grp.CInc("event1_counter1")
241 if m_grp.CIs("event2_counter1") == false {
242 t.Errorf("m_grp.Counters event2_counter1 not exists")
244 m_grp.CInc("event2_counter1")
246 if m_grp.GIs("event1_counter2") == false {
247 t.Errorf("m_grp.Gauges event1_counter2 not exists")
249 m_grp.GInc("event1_counter2")
251 if m_grp.GIs("event2_counter2") == false {
252 t.Errorf("m_grp.Gauges event2_counter2 not exists")
254 m_grp.GInc("event2_counter2")
256 m_grp.CAdd("event2_counter1", 1)
257 m_grp.CGet("event2_counter1")
258 m_grp.GGet("event2_counter2")
259 m_grp.GDec("event2_counter2")
260 m_grp.GSet("event2_counter2", 1)