...
1 package multi
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/go-kit/kit/metrics"
8 )
9
10 func TestMultiCounter(t *testing.T) {
11 c1 := &mockCounter{}
12 c2 := &mockCounter{}
13 c3 := &mockCounter{}
14 mc := NewCounter(c1, c2, c3)
15
16 mc.Add(123)
17 mc.Add(456)
18
19 want := "[123 456]"
20 for i, m := range []fmt.Stringer{c1, c2, c3} {
21 if have := m.String(); want != have {
22 t.Errorf("c%d: want %q, have %q", i+1, want, have)
23 }
24 }
25 }
26
27 func TestMultiGauge(t *testing.T) {
28 g1 := &mockGauge{}
29 g2 := &mockGauge{}
30 g3 := &mockGauge{}
31 mg := NewGauge(g1, g2, g3)
32
33 mg.Set(9)
34 mg.Set(8)
35 mg.Set(7)
36 mg.Add(3)
37
38 want := "[9 8 7 10]"
39 for i, m := range []fmt.Stringer{g1, g2, g3} {
40 if have := m.String(); want != have {
41 t.Errorf("g%d: want %q, have %q", i+1, want, have)
42 }
43 }
44 }
45
46 func TestMultiHistogram(t *testing.T) {
47 h1 := &mockHistogram{}
48 h2 := &mockHistogram{}
49 h3 := &mockHistogram{}
50 mh := NewHistogram(h1, h2, h3)
51
52 mh.Observe(1)
53 mh.Observe(2)
54 mh.Observe(4)
55 mh.Observe(8)
56
57 want := "[1 2 4 8]"
58 for i, m := range []fmt.Stringer{h1, h2, h3} {
59 if have := m.String(); want != have {
60 t.Errorf("g%d: want %q, have %q", i+1, want, have)
61 }
62 }
63 }
64
65 type mockCounter struct {
66 obs []float64
67 }
68
69 func (c *mockCounter) Add(delta float64) { c.obs = append(c.obs, delta) }
70 func (c *mockCounter) With(...string) metrics.Counter { return c }
71 func (c *mockCounter) String() string { return fmt.Sprintf("%v", c.obs) }
72
73 type mockGauge struct {
74 obs []float64
75 }
76
77 func (g *mockGauge) Set(value float64) { g.obs = append(g.obs, value) }
78 func (g *mockGauge) With(...string) metrics.Gauge { return g }
79 func (g *mockGauge) String() string { return fmt.Sprintf("%v", g.obs) }
80 func (g *mockGauge) Add(delta float64) {
81 var value float64
82 if len(g.obs) > 0 {
83 value = g.obs[len(g.obs)-1] + delta
84 } else {
85 value = delta
86 }
87 g.obs = append(g.obs, value)
88 }
89
90 type mockHistogram struct {
91 obs []float64
92 }
93
94 func (h *mockHistogram) Observe(value float64) { h.obs = append(h.obs, value) }
95 func (h *mockHistogram) With(...string) metrics.Histogram { return h }
96 func (h *mockHistogram) String() string { return fmt.Sprintf("%v", h.obs) }
97
View as plain text