-
Notifications
You must be signed in to change notification settings - Fork 451
/
tallytest.go
114 lines (100 loc) · 3.61 KB
/
tallytest.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
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tallytest
import (
"fmt"
"sort"
"testing"
"github.com/stretchr/testify/assert"
"github.com/uber-go/tally"
)
// AssertCounterValue asserts that the given counter has the expected value.
func AssertCounterValue(t *testing.T, expected int64, s tally.Snapshot, name string, tags map[string]string) bool {
index := flattenMetricIndex(name, tags)
counter := s.Counters()[index]
notFound := fmt.Sprintf("not found: key=%s, actual=%v", index, counterKeys(s.Counters()))
if !assert.NotNil(t, counter, notFound) {
return false
}
mismatch := fmt.Sprintf("current values: %v", counterMap(s.Counters()))
return assert.Equal(t, expected, counter.Value(), mismatch)
}
// AssertGaugeValue asserts that the given gauge has the expected value.
func AssertGaugeValue(t *testing.T, expected float64, s tally.Snapshot, name string, tags map[string]string) bool {
index := flattenMetricIndex(name, tags)
gauge := s.Gauges()[index]
notFound := fmt.Sprintf("not found: key=%s, actual=%v", index, gaugeKeys(s.Gauges()))
if !assert.NotNil(t, gauge, notFound) {
return false
}
mismatch := fmt.Sprintf("current values: %v", gaugeMap(s.Gauges()))
return assert.InDelta(t, expected, gauge.Value(), 0.0001, mismatch)
}
// AssertGaugeNil asserts that the given gauge does not exist.
func AssertGaugeNil(t *testing.T, s tally.Snapshot, name string, tags map[string]string) bool {
index := flattenMetricIndex(name, tags)
gauge := s.Gauges()[index]
found := fmt.Sprintf("found: key=%s, actual=%v", index, gaugeKeys(s.Gauges()))
return assert.Nil(t, gauge, found)
}
func flattenMetricIndex(name string, tags map[string]string) string {
keys := make([]string, 0, len(tags))
for k := range tags {
keys = append(keys, k)
}
sort.Strings(keys)
index := name + "+"
for i, k := range keys {
sep := ""
if i != 0 {
sep = ","
}
index += fmt.Sprintf("%s%s=%s", sep, k, tags[k])
}
return index
}
func counterMap(m map[string]tally.CounterSnapshot) map[string]int64 {
result := make(map[string]int64, len(m))
for k, v := range m {
result[k] = v.Value()
}
return result
}
func gaugeMap(m map[string]tally.GaugeSnapshot) map[string]float64 {
result := make(map[string]float64, len(m))
for k, v := range m {
result[k] = v.Value()
}
return result
}
func counterKeys(m map[string]tally.CounterSnapshot) []string {
r := make([]string, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
func gaugeKeys(m map[string]tally.GaugeSnapshot) []string {
r := make([]string, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}