Merge pull request #80 from go-kit/improve-coverage
Improve test coverage
Peter Bourgon
8 years ago
28 | 28 | if want, have := "level=error msg=\"© violation\"\n", buf.String(); want != have { |
29 | 29 | t.Errorf("want %#v, have %#v", want, have) |
30 | 30 | } |
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 | } | |
31 | 37 | } |
32 | 38 | |
33 | 39 | func TestModifiedLevels(t *testing.T) { |
36 | 42 | log.NewJSONLogger(&buf), |
37 | 43 | levels.Key("l"), |
38 | 44 | levels.DebugValue("dbg"), |
45 | levels.InfoValue("nfo"), | |
46 | levels.WarnValue("wrn"), | |
47 | levels.ErrorValue("err"), | |
48 | levels.CritValue("crt"), | |
39 | 49 | ) |
40 | 50 | logger.With("easter_island", "176°").Debug("msg", "moai") |
41 | 51 | if want, have := `{"easter_island":"176°","l":"dbg","msg":"moai"}`+"\n", buf.String(); want != have { |
117 | 117 | } |
118 | 118 | |
119 | 119 | func (h *histogram) rotateLoop(d time.Duration) { |
120 | for _ = range time.Tick(d) { | |
120 | for range time.Tick(d) { | |
121 | 121 | h.mu.Lock() |
122 | 122 | h.hist.Rotate() |
123 | 123 | h.mu.Unlock() |
4 | 4 | "fmt" |
5 | 5 | "testing" |
6 | 6 | |
7 | "github.com/go-kit/kit/metrics" | |
7 | 8 | "github.com/go-kit/kit/metrics/expvar" |
8 | 9 | "github.com/go-kit/kit/metrics/teststat" |
9 | 10 | ) |
10 | 11 | |
11 | 12 | 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 | ) | |
16 | 18 | const seed, mean, stdev int64 = 424242, 50, 10 |
17 | 19 | teststat.PopulateNormalHistogram(t, h, seed, mean, stdev) |
18 | teststat.AssertExpvarNormalHistogram(t, metricName, mean, stdev, quantiles) | |
20 | teststat.AssertExpvarNormalHistogram(t, name, mean, stdev, quantiles) | |
19 | 21 | } |
20 | 22 | |
21 | 23 | 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 { | |
26 | 30 | t.Errorf("want %q, have %q", want, have) |
27 | 31 | } |
28 | 32 | } |
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 | } |
52 | 52 | Subsystem: "multi_counter", |
53 | 53 | Name: "beta", |
54 | 54 | Help: "Beta counter.", |
55 | }, []string{}), | |
56 | ).Add(123) | |
55 | }, []string{"a"}), | |
56 | ).With(metrics.Field{Key: "a", Value: "b"}).Add(123) | |
57 | 57 | |
58 | 58 | if want, have := "123", stdexpvar.Get("alpha").String(); want != have { |
59 | 59 | t.Errorf("expvar: want %q, have %q", want, have) |
62 | 62 | if want, have := strings.Join([]string{ |
63 | 63 | `# HELP test_multi_counter_beta Beta counter.`, |
64 | 64 | `# TYPE test_multi_counter_beta counter`, |
65 | `test_multi_counter_beta 123`, | |
65 | `test_multi_counter_beta{a="b"} 123`, | |
66 | 66 | }, "\n"), scrapePrometheus(t); !strings.Contains(have, want) { |
67 | 67 | t.Errorf("Prometheus metric stanza not found or incorrect\n%s", have) |
68 | 68 | } |
76 | 76 | Subsystem: "multi_gauge", |
77 | 77 | Name: "kappa", |
78 | 78 | 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) | |
83 | 84 | |
84 | 85 | if want, have := "34", stdexpvar.Get("delta").String(); want != have { |
85 | 86 | t.Errorf("expvar: want %q, have %q", want, have) |
87 | 88 | if want, have := strings.Join([]string{ |
88 | 89 | `# HELP test_multi_gauge_kappa Kappa gauge.`, |
89 | 90 | `# 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) | |
96 | 97 | |
97 | 98 | if want, have := "-6", stdexpvar.Get("delta").String(); want != have { |
98 | 99 | t.Errorf("expvar: want %q, have %q", want, have) |
100 | 101 | if want, have := strings.Join([]string{ |
101 | 102 | `# HELP test_multi_gauge_kappa Kappa gauge.`, |
102 | 103 | `# TYPE test_multi_gauge_kappa gauge`, |
103 | `test_multi_gauge_kappa -6`, | |
104 | `test_multi_gauge_kappa{a="aaa"} -6`, | |
104 | 105 | }, "\n"), scrapePrometheus(t); !strings.Contains(have, want) { |
105 | 106 | t.Errorf("Prometheus metric stanza not found or incorrect\n%s", have) |
106 | 107 | } |
121 | 122 | const seed, mean, stdev int64 = 123, 50, 10 |
122 | 123 | populateNormalHistogram(t, h, seed, mean, stdev) |
123 | 124 | assertExpvarNormalHistogram(t, "omicron", mean, stdev, quantiles) |
124 | assertPrometheusNormalHistogram(t, "test_multi_histogram_nu", mean, stdev) | |
125 | assertPrometheusNormalHistogram(t, `test_multi_histogram_nu`, mean, stdev) | |
125 | 126 | } |
126 | 127 | |
127 | 128 | func populateNormalHistogram(t *testing.T, h metrics.Histogram, seed int64, mean, stdev int64) { |
219 | 220 | } |
220 | 221 | |
221 | 222 | 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) | |
223 | 225 | 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) | |
225 | 227 | } |
226 | 228 | 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) | |
228 | 230 | } |
229 | 231 | i, err := strconv.Atoi(matches[0][1]) |
230 | 232 | if err != nil { |
7 | 7 | ) |
8 | 8 | |
9 | 9 | 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 | ) | |
13 | 15 | |
14 | 16 | var h metrics.Histogram |
15 | 17 | h = expvar.NewHistogram(metricName, 0, 1000, 3, quantiles...) |
16 | 18 | h = metrics.NewScaledHistogram(h, scale) |
19 | h = h.With(metrics.Field{Key: "a", Value: "b"}) | |
17 | 20 | |
18 | 21 | const seed, mean, stdev = 333, 500, 100 // input values |
19 | 22 | populateNormalHistogram(t, h, seed, mean, stdev) // will be scaled down |
9 | 9 | ) |
10 | 10 | |
11 | 11 | 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 | ||
16 | 22 | const seed, mean, stdev int64 = 321, 100, 20 |
17 | 23 | |
18 | 24 | for i := 0; i < 4321; i++ { |
19 | 25 | sample := time.Duration(rand.NormFloat64()*float64(stdev)+float64(mean)) * time.Millisecond |
20 | h.Observe(sample) | |
26 | th.Observe(sample) | |
21 | 27 | } |
22 | 28 | |
23 | 29 | assertExpvarNormalHistogram(t, metricName, mean, stdev, quantiles) |