Package list golang-github-go-kit-kit / 55ab68a
metrics/expvar: improve test coverage Peter Bourgon 6 years ago
2 changed file(s) with 50 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
117117 }
118118
119119 func (h *histogram) rotateLoop(d time.Duration) {
120 for _ = range time.Tick(d) {
120 for range time.Tick(d) {
121121 h.mu.Lock()
122122 h.hist.Rotate()
123123 h.mu.Unlock()
44 "fmt"
55 "testing"
66
7 "github.com/go-kit/kit/metrics"
78 "github.com/go-kit/kit/metrics/expvar"
89 "github.com/go-kit/kit/metrics/teststat"
910 )
1011
1112 func TestHistogramQuantiles(t *testing.T) {
12 metricName := "test_histogram"
13 quantiles := []int{50, 90, 95, 99}
14 h := expvar.NewHistogram(metricName, 0, 100, 3, quantiles...)
15
13 var (
14 name = "test_histogram"
15 quantiles = []int{50, 90, 95, 99}
16 h = expvar.NewHistogram(name, 0, 100, 3, quantiles...).With(metrics.Field{Key: "ignored", Value: "field"})
17 )
1618 const seed, mean, stdev int64 = 424242, 50, 10
1719 teststat.PopulateNormalHistogram(t, h, seed, mean, stdev)
18 teststat.AssertExpvarNormalHistogram(t, metricName, mean, stdev, quantiles)
20 teststat.AssertExpvarNormalHistogram(t, name, mean, stdev, quantiles)
1921 }
2022
2123 func TestCallbackGauge(t *testing.T) {
22 value := 42.43
23 metricName := "foo"
24 expvar.PublishCallbackGauge(metricName, func() float64 { return value })
25 if want, have := fmt.Sprint(value), stdexpvar.Get(metricName).String(); want != have {
24 var (
25 name = "foo"
26 value = 42.43
27 )
28 expvar.PublishCallbackGauge(name, func() float64 { return value })
29 if want, have := fmt.Sprint(value), stdexpvar.Get(name).String(); want != have {
2630 t.Errorf("want %q, have %q", want, have)
2731 }
2832 }
33
34 func TestCounter(t *testing.T) {
35 var (
36 name = "m"
37 value = 123
38 )
39 expvar.NewCounter(name).With(metrics.Field{Key: "ignored", Value: "field"}).Add(uint64(value))
40 if want, have := fmt.Sprint(value), stdexpvar.Get(name).String(); want != have {
41 t.Errorf("want %q, have %q", want, have)
42 }
43 }
44
45 func TestGauge(t *testing.T) {
46 var (
47 name = "xyz"
48 value = 54321
49 delta = 12345
50 g = expvar.NewGauge(name).With(metrics.Field{Key: "ignored", Value: "field"})
51 )
52 g.Set(float64(value))
53 g.Add(float64(delta))
54 if want, have := fmt.Sprint(value+delta), stdexpvar.Get(name).String(); want != have {
55 t.Errorf("want %q, have %q", want, have)
56 }
57 }
58
59 func TestInvalidQuantile(t *testing.T) {
60 defer func() {
61 if err := recover(); err == nil {
62 t.Errorf("expected panic, got none")
63 } else {
64 t.Logf("got expected panic: %v", err)
65 }
66 }()
67 expvar.NewHistogram("foo", 0.0, 100.0, 3, 50, 90, 95, 99, 101)
68 }