1package multi 2 3import ( 4 "fmt" 5 "testing" 6 7 "github.com/go-kit/kit/metrics" 8) 9 10func 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 27func 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 46func 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 65type mockCounter struct { 66 obs []float64 67} 68 69func (c *mockCounter) Add(delta float64) { c.obs = append(c.obs, delta) } 70func (c *mockCounter) With(...string) metrics.Counter { return c } 71func (c *mockCounter) String() string { return fmt.Sprintf("%v", c.obs) } 72 73type mockGauge struct { 74 obs []float64 75} 76 77func (g *mockGauge) Set(value float64) { g.obs = append(g.obs, value) } 78func (g *mockGauge) With(...string) metrics.Gauge { return g } 79func (g *mockGauge) String() string { return fmt.Sprintf("%v", g.obs) } 80func (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 90type mockHistogram struct { 91 obs []float64 92} 93 94func (h *mockHistogram) Observe(value float64) { h.obs = append(h.obs, value) } 95func (h *mockHistogram) With(...string) metrics.Histogram { return h } 96func (h *mockHistogram) String() string { return fmt.Sprintf("%v", h.obs) } 97