Package list golang-github-go-kit-kit / a1c440f
Merge pull request #80 from go-kit/improve-coverage Improve test coverage Peter Bourgon 6 years ago
6 changed file(s) with 97 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
2828 if want, have := "level=error msg=\"© violation\"\n", buf.String(); want != have {
2929 t.Errorf("want %#v, have %#v", want, have)
3030 }
31
32 buf.Reset()
33 logger.Crit("msg", " ")
34 if want, have := "level=crit msg=\"\\t\"\n", buf.String(); want != have {
35 t.Errorf("want %#v, have %#v", want, have)
36 }
3137 }
3238
3339 func TestModifiedLevels(t *testing.T) {
3642 log.NewJSONLogger(&buf),
3743 levels.Key("l"),
3844 levels.DebugValue("dbg"),
45 levels.InfoValue("nfo"),
46 levels.WarnValue("wrn"),
47 levels.ErrorValue("err"),
48 levels.CritValue("crt"),
3949 )
4050 logger.With("easter_island", "176°").Debug("msg", "moai")
4151 if want, have := `{"easter_island":"176°","l":"dbg","msg":"moai"}`+"\n", buf.String(); want != have {
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 }
5252 Subsystem: "multi_counter",
5353 Name: "beta",
5454 Help: "Beta counter.",
55 }, []string{}),
56 ).Add(123)
55 }, []string{"a"}),
56 ).With(metrics.Field{Key: "a", Value: "b"}).Add(123)
5757
5858 if want, have := "123", stdexpvar.Get("alpha").String(); want != have {
5959 t.Errorf("expvar: want %q, have %q", want, have)
6262 if want, have := strings.Join([]string{
6363 `# HELP test_multi_counter_beta Beta counter.`,
6464 `# TYPE test_multi_counter_beta counter`,
65 `test_multi_counter_beta 123`,
65 `test_multi_counter_beta{a="b"} 123`,
6666 }, "\n"), scrapePrometheus(t); !strings.Contains(have, want) {
6767 t.Errorf("Prometheus metric stanza not found or incorrect\n%s", have)
6868 }
7676 Subsystem: "multi_gauge",
7777 Name: "kappa",
7878 Help: "Kappa gauge.",
79 }, []string{}),
80 )
81
82 g.Set(34)
79 }, []string{"a"}),
80 )
81
82 f := metrics.Field{Key: "a", Value: "aaa"}
83 g.With(f).Set(34)
8384
8485 if want, have := "34", stdexpvar.Get("delta").String(); want != have {
8586 t.Errorf("expvar: want %q, have %q", want, have)
8788 if want, have := strings.Join([]string{
8889 `# HELP test_multi_gauge_kappa Kappa gauge.`,
8990 `# TYPE test_multi_gauge_kappa gauge`,
90 `test_multi_gauge_kappa 34`,
91 }, "\n"), scrapePrometheus(t); !strings.Contains(have, want) {
92 t.Errorf("Prometheus metric stanza not found or incorrect\n%s", have)
93 }
94
95 g.Add(-40)
91 `test_multi_gauge_kappa{a="aaa"} 34`,
92 }, "\n"), scrapePrometheus(t); !strings.Contains(have, want) {
93 t.Errorf("Prometheus metric stanza not found or incorrect\n%s", have)
94 }
95
96 g.With(f).Add(-40)
9697
9798 if want, have := "-6", stdexpvar.Get("delta").String(); want != have {
9899 t.Errorf("expvar: want %q, have %q", want, have)
100101 if want, have := strings.Join([]string{
101102 `# HELP test_multi_gauge_kappa Kappa gauge.`,
102103 `# TYPE test_multi_gauge_kappa gauge`,
103 `test_multi_gauge_kappa -6`,
104 `test_multi_gauge_kappa{a="aaa"} -6`,
104105 }, "\n"), scrapePrometheus(t); !strings.Contains(have, want) {
105106 t.Errorf("Prometheus metric stanza not found or incorrect\n%s", have)
106107 }
121122 const seed, mean, stdev int64 = 123, 50, 10
122123 populateNormalHistogram(t, h, seed, mean, stdev)
123124 assertExpvarNormalHistogram(t, "omicron", mean, stdev, quantiles)
124 assertPrometheusNormalHistogram(t, "test_multi_histogram_nu", mean, stdev)
125 assertPrometheusNormalHistogram(t, `test_multi_histogram_nu`, mean, stdev)
125126 }
126127
127128 func populateNormalHistogram(t *testing.T, h metrics.Histogram, seed int64, mean, stdev int64) {
219220 }
220221
221222 func getPrometheusQuantile(t *testing.T, scrape, name, quantileStr string) int {
222 matches := regexp.MustCompile(name+`{quantile="`+quantileStr+`"} ([0-9]+)`).FindAllStringSubmatch(scrape, -1)
223 re := name + `{quantile="` + quantileStr + `"} ([0-9]+)`
224 matches := regexp.MustCompile(re).FindAllStringSubmatch(scrape, -1)
223225 if len(matches) < 1 {
224 t.Fatalf("%q: quantile %q not found in scrape", name, quantileStr)
226 t.Fatalf("%q: quantile %q not found in scrape (%s)", name, quantileStr, re)
225227 }
226228 if len(matches[0]) < 2 {
227 t.Fatalf("%q: quantile %q not found in scrape", name, quantileStr)
229 t.Fatalf("%q: quantile %q not found in scrape (%s)", name, quantileStr, re)
228230 }
229231 i, err := strconv.Atoi(matches[0][1])
230232 if err != nil {
77 )
88
99 func TestScaledHistogram(t *testing.T) {
10 quantiles := []int{50, 90, 99}
11 scale := int64(10)
12 metricName := "test_scaled_histogram"
10 var (
11 quantiles = []int{50, 90, 99}
12 scale = int64(10)
13 metricName = "test_scaled_histogram"
14 )
1315
1416 var h metrics.Histogram
1517 h = expvar.NewHistogram(metricName, 0, 1000, 3, quantiles...)
1618 h = metrics.NewScaledHistogram(h, scale)
19 h = h.With(metrics.Field{Key: "a", Value: "b"})
1720
1821 const seed, mean, stdev = 333, 500, 100 // input values
1922 populateNormalHistogram(t, h, seed, mean, stdev) // will be scaled down
99 )
1010
1111 func TestTimeHistogram(t *testing.T) {
12 const metricName string = "test_time_histogram"
13 quantiles := []int{50, 90, 99}
14 h0 := expvar.NewHistogram(metricName, 0, 200, 3, quantiles...)
15 h := metrics.NewTimeHistogram(time.Millisecond, h0)
12 var (
13 metricName = "test_time_histogram"
14 minValue = int64(0)
15 maxValue = int64(200)
16 sigfigs = 3
17 quantiles = []int{50, 90, 99}
18 h = expvar.NewHistogram(metricName, minValue, maxValue, sigfigs, quantiles...)
19 th = metrics.NewTimeHistogram(time.Millisecond, h).With(metrics.Field{Key: "a", Value: "b"})
20 )
21
1622 const seed, mean, stdev int64 = 321, 100, 20
1723
1824 for i := 0; i < 4321; i++ {
1925 sample := time.Duration(rand.NormFloat64()*float64(stdev)+float64(mean)) * time.Millisecond
20 h.Observe(sample)
26 th.Observe(sample)
2127 }
2228
2329 assertExpvarNormalHistogram(t, metricName, mean, stdev, quantiles)