-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter_test.go
161 lines (153 loc) · 3.93 KB
/
counter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package mockitmetrics
import (
"testing"
kit "github.com/go-kit/kit/metrics"
"github.com/stretchr/testify/assert"
)
func TestCounter(t *testing.T) {
tests := []struct {
description string
fn func(kit.Counter)
opt Option
opts []Option
expected map[string]float64
expectPanic bool
}{
{
description: "use a counter with no labels",
fn: func(c kit.Counter) {
c.Add(1)
},
expected: map[string]float64{
"": 1.0,
},
}, {
description: "use a counter with labels",
fn: func(c kit.Counter) {
c.With("label1", "value1", "label2", "value2").Add(1)
},
expected: map[string]float64{
"value1.value2": 1.0,
},
}, {
description: "use a counter with labels using 2 calls",
fn: func(c kit.Counter) {
c.With("label1", "value1").With("label2", "value2").Add(1)
},
expected: map[string]float64{
"value1.value2": 1.0,
},
}, {
description: "use a counter with labels using 3 calls",
fn: func(c kit.Counter) {
c.With("label1", "value1").With("label2", "value2").With("label3", "value3").Add(1)
},
expected: map[string]float64{
"value1.value2.value3": 1.0,
},
}, {
description: "use a different delimiter",
fn: func(c kit.Counter) {
c.With("label1", "value1").With("label2", "value2").With("label3", "value3").Add(1)
},
opt: Delimiter("-"),
expected: map[string]float64{
"value1-value2-value3": 1.0,
},
}, {
description: "check that custom panic is honored",
fn: func(c kit.Counter) {
c.With("label1", "value1").Add(-1)
},
opt: PanicFunc(func(any) {}),
}, {
description: "check that panic is honored",
fn: func(c kit.Counter) {
c.With("label1", "value1").Add(-1)
},
expectPanic: true,
}, {
description: "use a counter with no labels, expecting no labels",
fn: func(c kit.Counter) {
c.Add(1)
},
opt: ExpectLabels(),
expected: map[string]float64{
"": 1.0,
},
}, {
description: "error when an unexpected label is sent",
fn: func(c kit.Counter) {
c.With("invalid").Add(1)
},
opt: ExpectLabels(),
expectPanic: true,
}, {
description: "use a counter with labels, and require 2",
fn: func(c kit.Counter) {
c.With("one", "value1", "two", "value2").Add(1)
},
opt: ExpectLabels("one", "two"),
expected: map[string]float64{
"value1.value2": 1.0,
},
}, {
description: "error when a missing label is sent",
fn: func(c kit.Counter) {
c.With("one", "value1").Add(1)
},
opt: ExpectLabels("one", "two"),
expectPanic: true,
}, {
description: "error when an extra label is sent",
fn: func(c kit.Counter) {
c.With("one", "value1", "two", "value2", "label3", "value3")
},
opt: ExpectLabels("one", "two"),
expectPanic: true,
}, {
description: "use a gauge with the wrong label",
fn: func(c kit.Counter) {
c.With("label1", "value1", "label2", "value2")
},
opt: ExpectLabels("one", "two"),
expectPanic: true,
}, {
description: "use a gauge missing a value",
fn: func(c kit.Counter) {
c.With("one")
},
opt: ExpectLabels("one", "two"),
expectPanic: true,
}, {
description: "use a gauge with the label set to ''",
fn: func(c kit.Counter) {
c.With("", "value")
},
opt: ExpectLabels("one", "two"),
expectPanic: true,
}, {
description: "use a gauge with the value set to ''",
fn: func(c kit.Counter) {
c.With("one", "")
},
opt: ExpectLabels("one", "two"),
expectPanic: true,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
opts := append(tc.opts, tc.opt)
c := NewCounter(opts...)
if tc.expectPanic {
assert.Panics(func() { tc.fn(c) })
return
}
tc.fn(c)
assert.Equal(tc.expected, c.Value())
})
}
}