2021-01-22 18:21:40 +03:00
|
|
|
package meter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNoopMeter(t *testing.T) {
|
2021-02-18 15:57:42 +03:00
|
|
|
m := NewMeter(Path("/noop"))
|
|
|
|
if "/noop" != m.Options().Path {
|
|
|
|
t.Fatalf("invalid options parsing: %v", m.Options())
|
2021-02-13 15:35:56 +03:00
|
|
|
}
|
2021-01-27 00:54:19 +03:00
|
|
|
|
2021-04-26 23:13:36 +03:00
|
|
|
cnt := m.Counter("counter", "server", "noop")
|
2021-01-27 00:54:19 +03:00
|
|
|
cnt.Inc()
|
|
|
|
}
|
|
|
|
|
2021-07-21 14:10:20 +03:00
|
|
|
func testEq(a, b []string) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range a {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildLabels(t *testing.T) {
|
|
|
|
type testData struct {
|
|
|
|
src []string
|
|
|
|
dst []string
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []testData{
|
|
|
|
testData{
|
|
|
|
src: []string{"zerolabel", "value3", "firstlabel", "value2"},
|
|
|
|
dst: []string{"firstlabel", "value2", "zerolabel", "value3"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range data {
|
|
|
|
if !testEq(d.dst, BuildLabels(d.src...)) {
|
|
|
|
t.Fatalf("slices not properly sorted: %v %v", d.dst, d.src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 12:39:59 +03:00
|
|
|
func TestBuildName(t *testing.T) {
|
|
|
|
data := map[string][]string{
|
2021-07-21 14:10:20 +03:00
|
|
|
`my_metric{firstlabel="value2",zerolabel="value3"}`: []string{
|
|
|
|
"my_metric",
|
|
|
|
"zerolabel", "value3", "firstlabel", "value2",
|
|
|
|
},
|
2021-07-21 12:39:59 +03:00
|
|
|
`my_metric{broker="broker2",register="mdns",server="tcp"}`: []string{
|
|
|
|
"my_metric",
|
|
|
|
"broker", "broker1", "broker", "broker2", "server", "http", "server", "tcp", "register", "mdns",
|
|
|
|
},
|
|
|
|
}
|
2021-01-27 00:54:19 +03:00
|
|
|
|
2021-07-21 12:39:59 +03:00
|
|
|
for e, d := range data {
|
|
|
|
if x := BuildName(d[0], d[1:]...); x != e {
|
|
|
|
t.Fatalf("expect: %s, result: %s", e, x)
|
|
|
|
}
|
2021-01-27 00:54:19 +03:00
|
|
|
}
|
2021-01-22 18:21:40 +03:00
|
|
|
}
|