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"},
45 func TestMetricCounterVector(t *testing.T) {
48 c_grp1 := Metric.GetCounterGroupFromVects([]string{"name1", "event1"}, mCVect)
49 if _, ok := c_grp1["counter1"]; ok == false {
50 t.Errorf("c_grp1 counter1 not exists")
52 c_grp1["counter1"].Inc()
56 c_grp2 := Metric.GetCounterGroupFromVects([]string{"name1", "event2"}, mCVect)
57 if _, ok := c_grp2["counter1"]; ok == false {
58 t.Errorf("c_grp2 counter1 not exists")
60 c_grp2["counter1"].Inc()
63 func TestMetricGaugeVector(t *testing.T) {
66 g_grp1 := Metric.GetGaugeGroupFromVects([]string{"name1", "event1"}, mGVect)
67 if _, ok := g_grp1["counter2"]; ok == false {
68 t.Errorf("g_grp1 counter2 not exists")
70 g_grp1["counter2"].Inc()
74 g_grp2 := Metric.GetGaugeGroupFromVects([]string{"name1", "event2"}, mGVect)
75 if _, ok := g_grp2["counter2"]; ok == false {
76 t.Errorf("g_grp2 counter2 not exists")
78 g_grp2["counter2"].Inc()
81 func TestMetricCounterVectorPrefix(t *testing.T) {
84 c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
85 if _, ok := c_grp1["event1_counter1"]; ok == false {
86 t.Errorf("c_grp1 event1_counter1 not exists")
88 c_grp1["event1_counter1"].Inc()
92 c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
93 if _, ok := c_grp2["event2_counter1"]; ok == false {
94 t.Errorf("c_grp2 event2_counter1 not exists")
96 c_grp2["event2_counter1"].Inc()
100 c_grp := Metric.CombineCounterGroups(c_grp1, c_grp2)
104 if _, ok := c_grp["event1_counter1"]; ok == false {
105 t.Errorf("c_grp event1_counter1 not exists")
107 c_grp["event1_counter1"].Inc()
111 if _, ok := c_grp["event2_counter1"]; ok == false {
112 t.Errorf("c_grp event2_counter1 not exists")
114 c_grp["event2_counter1"].Inc()
117 func TestMetricGaugeVectorPrefix(t *testing.T) {
120 g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
121 if _, ok := g_grp1["event1_counter2"]; ok == false {
122 t.Errorf("g_grp1 event1_counter2 not exists")
124 g_grp1["event1_counter2"].Inc()
128 g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
129 if _, ok := g_grp2["event2_counter2"]; ok == false {
130 t.Errorf("g_grp2 event2_counter2 not exists")
132 g_grp2["event2_counter2"].Inc()
136 g_grp := Metric.CombineGaugeGroups(g_grp1, g_grp2)
140 if _, ok := g_grp["event1_counter2"]; ok == false {
141 t.Errorf("g_grp event1_counter2 not exists")
143 g_grp["event1_counter2"].Inc()
147 if _, ok := g_grp["event2_counter2"]; ok == false {
148 t.Errorf("g_grp event2_counter2 not exists")
150 g_grp["event2_counter2"].Inc()
153 func TestMetricGroupCache(t *testing.T) {
156 c_grp1 := Metric.GetCounterGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mCVect)
157 if _, ok := c_grp1["event1_counter1"]; ok == false {
158 t.Errorf("c_grp1 event1_counter1 not exists")
160 c_grp1["event1_counter1"].Inc()
164 c_grp2 := Metric.GetCounterGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mCVect)
165 if _, ok := c_grp2["event2_counter1"]; ok == false {
166 t.Errorf("c_grp2 event2_counter1 not exists")
168 c_grp2["event2_counter1"].Inc()
172 g_grp1 := Metric.GetGaugeGroupFromVectsWithPrefix("event1_", []string{"name1", "event1"}, mGVect)
173 if _, ok := g_grp1["event1_counter2"]; ok == false {
174 t.Errorf("g_grp1 event1_counter2 not exists")
176 g_grp1["event1_counter2"].Inc()
180 g_grp2 := Metric.GetGaugeGroupFromVectsWithPrefix("event2_", []string{"name1", "event2"}, mGVect)
181 if _, ok := g_grp2["event2_counter2"]; ok == false {
182 t.Errorf("g_grp2 event2_counter2 not exists")
184 g_grp2["event2_counter2"].Inc()
189 entry := Metric.GroupCacheGet(cacheid)
191 Metric.GroupCacheAddCounters(cacheid, c_grp1)
192 Metric.GroupCacheAddCounters(cacheid, c_grp2)
193 Metric.GroupCacheAddGauges(cacheid, g_grp1)
194 Metric.GroupCacheAddGauges(cacheid, g_grp2)
195 entry = Metric.GroupCacheGet(cacheid)
199 t.Errorf("Cache failed")
202 if _, ok := entry.Counters["event1_counter1"]; ok == false {
203 t.Errorf("entry.Counters event1_counter1 not exists")
205 entry.Counters["event1_counter1"].Inc()
207 if _, ok := entry.Counters["event2_counter1"]; ok == false {
208 t.Errorf("entry.Counters event2_counter1 not exists")
210 entry.Counters["event2_counter1"].Inc()
212 if _, ok := entry.Gauges["event1_counter2"]; ok == false {
213 t.Errorf("entry.Gauges event1_counter2 not exists")
215 entry.Gauges["event1_counter2"].Inc()
217 if _, ok := entry.Gauges["event2_counter2"]; ok == false {
218 t.Errorf("entry.Gauges event2_counter2 not exists")
220 entry.Gauges["event2_counter2"].Inc()