Completed tests

This commit is contained in:
beorn7 2016-11-14 20:53:23 +01:00
parent 120be69578
commit 8aba21aba3
1 changed files with 80 additions and 14 deletions

View File

@ -20,22 +20,88 @@ import (
) )
func TestTimerObserve(t *testing.T) { func TestTimerObserve(t *testing.T) {
his := NewHistogram( his := NewHistogram(HistogramOpts{
HistogramOpts{
Name: "test_histogram", Name: "test_histogram",
}, })
) sum := NewSummary(SummaryOpts{
Name: "test_summary",
})
gauge := NewGauge(GaugeOpts{
Name: "test_gauge",
})
func() {
hisTimer := NewTimer().With(his)
sumTimer := NewTimer().With(sum)
gaugeTimer := NewTimer().WithGauge(gauge)
defer hisTimer.ObserveDuration()
defer sumTimer.ObserveDuration()
defer gaugeTimer.ObserveDuration()
}()
timer := NewTimer().With(his)
timer.ObserveDuration()
m := &dto.Metric{} m := &dto.Metric{}
his.Write(m) his.Write(m)
if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
for _, b := range m.Histogram.Bucket { t.Errorf("want %d observations for histogram, got %d", want, got)
if int(b.GetCumulativeCount()) > 0 {
return
} }
m.Reset()
sum.Write(m)
if want, got := uint64(1), m.GetSummary().GetSampleCount(); want != got {
t.Errorf("want %d observations for summary, got %d", want, got)
}
m.Reset()
gauge.Write(m)
if got := m.GetGauge().GetValue(); got <= 0 {
t.Errorf("want value > 0 for gauge, got %f", got)
}
}
func TestTimerEmpty(t *testing.T) {
emptyTimer := NewTimer()
emptyTimer.ObserveDuration()
// Do nothing, just demonstrate it works without panic.
}
func TestTimerUnset(t *testing.T) {
his := NewHistogram(HistogramOpts{
Name: "test_histogram",
})
func() {
timer := NewTimer().With(his)
defer timer.ObserveDuration()
timer.With(nil)
}()
m := &dto.Metric{}
his.Write(m)
if want, got := uint64(0), m.GetHistogram().GetSampleCount(); want != got {
t.Errorf("want %d observations for histogram, got %d", want, got)
}
}
func TestTimerChange(t *testing.T) {
his := NewHistogram(HistogramOpts{
Name: "test_histogram",
})
sum := NewSummary(SummaryOpts{
Name: "test_summary",
})
func() {
timer := NewTimer().With(his)
defer timer.ObserveDuration()
timer.With(sum)
}()
m := &dto.Metric{}
his.Write(m)
if want, got := uint64(0), m.GetHistogram().GetSampleCount(); want != got {
t.Errorf("want %d observations for histogram, got %d", want, got)
}
m.Reset()
sum.Write(m)
if want, got := uint64(1), m.GetSummary().GetSampleCount(); want != got {
t.Errorf("want %d observations for summary, got %d", want, got)
} }
t.Fatalf("no counts recorded")
} }