2019-06-01 23:18:41 +03:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewSet(t *testing.T) {
|
|
|
|
var ss []*Set
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
s := NewSet()
|
|
|
|
ss = append(ss, s)
|
|
|
|
}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
s := ss[i]
|
|
|
|
for j := 0; j < 10; j++ {
|
|
|
|
c := s.NewCounter(fmt.Sprintf("counter_%d", j))
|
|
|
|
c.Inc()
|
|
|
|
if n := c.Get(); n != 1 {
|
|
|
|
t.Fatalf("unexpected counter value; got %d; want %d", n, 1)
|
|
|
|
}
|
|
|
|
g := s.NewGauge(fmt.Sprintf("gauge_%d", j), func() float64 { return 123 })
|
|
|
|
if v := g.Get(); v != 123 {
|
|
|
|
t.Fatalf("unexpected gauge value; got %v; want %v", v, 123)
|
|
|
|
}
|
|
|
|
sm := s.NewSummary(fmt.Sprintf("summary_%d", j))
|
|
|
|
if sm == nil {
|
|
|
|
t.Fatalf("NewSummary returned nil")
|
|
|
|
}
|
2019-11-23 01:16:17 +03:00
|
|
|
h := s.NewHistogram(fmt.Sprintf("histogram_%d", j))
|
|
|
|
if h == nil {
|
|
|
|
t.Fatalf("NewHistogram returned nil")
|
|
|
|
}
|
2019-06-01 23:18:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-26 20:52:38 +03:00
|
|
|
|
2020-02-26 21:37:47 +03:00
|
|
|
func TestSetListMetricNames(t *testing.T) {
|
2020-02-26 20:52:38 +03:00
|
|
|
s := NewSet()
|
|
|
|
expect := []string{"cnt1", "cnt2", "cnt3"}
|
|
|
|
// Initialize a few counters
|
|
|
|
for _, n := range expect {
|
|
|
|
c := s.NewCounter(n)
|
|
|
|
c.Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
list := s.ListMetricNames()
|
|
|
|
|
|
|
|
if len(list) != len(expect) {
|
|
|
|
t.Fatalf("Metrics count is wrong for listing")
|
|
|
|
}
|
|
|
|
for _, e := range expect {
|
|
|
|
found := false
|
|
|
|
for _, n := range list {
|
|
|
|
if e == n {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("Metric %s not found in listing", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 21:37:47 +03:00
|
|
|
func TestSetUnregisterMetric(t *testing.T) {
|
2020-02-26 20:52:38 +03:00
|
|
|
s := NewSet()
|
2020-02-26 21:37:47 +03:00
|
|
|
// Initialize a few metrics
|
2020-02-26 20:52:38 +03:00
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
c := s.NewCounter(fmt.Sprintf("counter_%d", i))
|
|
|
|
c.Inc()
|
2020-02-26 21:37:47 +03:00
|
|
|
sm := s.NewSummary(fmt.Sprintf("summary_%d", i))
|
|
|
|
sm.Update(float64(i))
|
|
|
|
}
|
|
|
|
// Unregister existing metrics
|
|
|
|
if !s.UnregisterMetric("counter_1") {
|
|
|
|
t.Fatalf("UnregisterMetric(counter_1) must return true")
|
2020-02-26 20:52:38 +03:00
|
|
|
}
|
2020-02-26 21:37:47 +03:00
|
|
|
if !s.UnregisterMetric("summary_1") {
|
|
|
|
t.Fatalf("UnregisterMetric(summary_1) must return true")
|
2020-02-26 20:52:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unregister twice must return false
|
2020-02-26 21:37:47 +03:00
|
|
|
if s.UnregisterMetric("counter_1") {
|
|
|
|
t.Fatalf("UnregisterMetric(counter_1) must return false on unregistered metric")
|
|
|
|
}
|
|
|
|
if s.UnregisterMetric("summary_1") {
|
|
|
|
t.Fatalf("UnregisterMetric(summary_1) must return false on unregistered metric")
|
2020-02-26 20:52:38 +03:00
|
|
|
}
|
|
|
|
|
2020-02-26 21:37:47 +03:00
|
|
|
// Validate metrics are removed
|
2020-07-28 21:58:28 +03:00
|
|
|
const cName, smName = "counter_1", "summary_1"
|
2020-02-26 21:37:47 +03:00
|
|
|
ok := false
|
2020-02-26 20:52:38 +03:00
|
|
|
for _, n := range s.ListMetricNames() {
|
2020-07-28 21:58:28 +03:00
|
|
|
if n == cName || n == smName {
|
2020-02-26 20:52:38 +03:00
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ok {
|
2020-02-26 21:37:47 +03:00
|
|
|
t.Fatalf("Metric counter_1 and summary_1 must not be listed anymore after unregister")
|
2020-02-26 20:52:38 +03:00
|
|
|
}
|
2020-07-28 21:58:28 +03:00
|
|
|
|
|
|
|
// re-register with the same names supposed
|
|
|
|
// to be successful
|
|
|
|
s.NewCounter(cName).Inc()
|
|
|
|
s.NewSummary(smName).Update(float64(1))
|
2020-02-26 20:52:38 +03:00
|
|
|
}
|