-
Notifications
You must be signed in to change notification settings - Fork 1
/
summary_test.go
178 lines (149 loc) · 3.41 KB
/
summary_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"reflect"
"testing"
)
func checkEqual(t *testing.T, expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Expected %v but got %v", expected, actual)
}
}
func TestStatTracker_NilGet(t *testing.T) {
// Test that get() works on a nil StatTracker
var st *StatTracker
expected := &StatResponse{}
checkEqual(t, expected, st.get())
}
func TestStatTracker_AddGet(t *testing.T) {
st := NewStatTracker()
stat1 := &Stat{
Tags: []string{"foo:bar", "baz:blarg"},
}
stat2 := &Stat{
Tags: []string{"foo:bar", "baz:bang"},
}
// Add a single stat
st.add(stat1)
// Check the output
expected := &StatResponse{
Count: 1,
Tags: map[string][]string{
"foo": {"bar"},
"baz": {"blarg"},
},
TagSets: map[string]int{
"baz:blarg,foo:bar": 1,
},
}
checkEqual(t, expected, st.get())
// Add another stat, plus the first one again
st.add(stat2)
st.add(stat1)
// Check the output
expected = &StatResponse{
Count: 3,
Tags: map[string][]string{
"foo": {"bar"},
"baz": {"bang", "blarg"},
},
TagSets: map[string]int{
"baz:blarg,foo:bar": 2,
"baz:bang,foo:bar": 1,
},
}
checkEqual(t, expected, st.get())
}
func TestSummary_Empty(t *testing.T) {
s := NewSummary()
// Try to get a metric that doesn't exist
expected1 := &StatResponse{}
r := s.get("non.existent.metric")
checkEqual(t, expected1, r)
expected2 := map[string]int{}
count := s.getAllCount()
checkEqual(t, expected2, count)
expected3 := map[string]*StatResponse{}
details := s.getAllDetails()
checkEqual(t, expected3, details)
}
func TestSummary_WithMetrics(t *testing.T) {
s := NewSummary()
stat1 := &Stat{
Name: "first.metric",
Tags: []string{"foo:bar", "baz:blarg"},
}
stat2 := &Stat{
Name: "second.metric",
Tags: []string{"foo:bar", "baz:bang"},
}
// Add stat1 once, stat2 twice
s.add(stat1)
s.add(stat2)
s.add(stat2)
// Get one of the metrics
expectedFirst := &StatResponse{
Count: 1,
Tags: map[string][]string{
"foo": {"bar"},
"baz": {"blarg"},
},
TagSets: map[string]int{
"baz:blarg,foo:bar": 1,
},
}
r := s.get(stat1.Name)
checkEqual(t, expectedFirst, r)
// Get a count of all metrics
expected2 := map[string]int{
"first.metric": 1,
"second.metric": 2,
}
count := s.getAllCount()
checkEqual(t, expected2, count)
// Get details about all metrics
expectedSecond := &StatResponse{
Count: 2,
Tags: map[string][]string{
"foo": {"bar"},
"baz": {"bang"},
},
TagSets: map[string]int{
"baz:bang,foo:bar": 2,
},
}
details := s.getAllDetails()
checkEqual(t, 2, len(details))
checkEqual(t, expectedFirst, details["first.metric"])
checkEqual(t, expectedSecond, details["second.metric"])
}
func TestSummary_Reset(t *testing.T) {
s := NewSummary()
stat1 := &Stat{
Name: "first.metric",
Tags: []string{"foo:bar", "baz:blarg"},
}
stat2 := &Stat{
Name: "second.metric",
Tags: []string{"foo:bar", "baz:bang"},
}
// Add a few metrics
s.add(stat1)
s.add(stat2)
// Check that it worked
expected1 := map[string]int{
"first.metric": 1,
"second.metric": 1,
}
count1 := s.getAllCount()
checkEqual(t, expected1, count1)
// Reset the summary
s.reset()
// Make sure the count was reset
expected2 := map[string]int{}
count2 := s.getAllCount()
checkEqual(t, expected2, count2)
// Details should also be reset
expected3 := map[string]*StatResponse{}
details := s.getAllDetails()
checkEqual(t, expected3, details)
}