File: timer_test.go

package info (click to toggle)
golang-github-go-kit-kit 0.13.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,784 kB
  • sloc: sh: 22; makefile: 11
file content (60 lines) | stat: -rw-r--r-- 1,457 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package metrics_test

import (
	"math"
	"testing"

	"time"

	"github.com/go-kit/kit/metrics"
	"github.com/go-kit/kit/metrics/generic"
)

func TestTimerFast(t *testing.T) {
	h := generic.NewSimpleHistogram()
	metrics.NewTimer(h).ObserveDuration()

	tolerance := 0.050
	if want, have := 0.000, h.ApproximateMovingAverage(); math.Abs(want-have) > tolerance {
		t.Errorf("want %.3f, have %.3f", want, have)
	}
}

func TestTimerSlow(t *testing.T) {
	t.Skip("Skipping flaky test")
	h := generic.NewSimpleHistogram()
	timer := metrics.NewTimer(h)
	time.Sleep(250 * time.Millisecond)
	timer.ObserveDuration()

	tolerance := 0.050
	if want, have := 0.250, h.ApproximateMovingAverage(); math.Abs(want-have) > tolerance {
		t.Errorf("want %.3f, have %.3f", want, have)
	}
}

func TestTimerUnit(t *testing.T) {
	t.Skip("Skipping flaky test")
	for _, tc := range []struct {
		name      string
		unit      time.Duration
		tolerance float64
		want      float64
	}{
		{"Seconds", time.Second, 0.010, 0.100},
		{"Milliseconds", time.Millisecond, 10, 100},
		{"Nanoseconds", time.Nanosecond, 10000000, 100000000},
	} {
		t.Run(tc.name, func(t *testing.T) {
			h := generic.NewSimpleHistogram()
			timer := metrics.NewTimer(h)
			time.Sleep(100 * time.Millisecond)
			timer.Unit(tc.unit)
			timer.ObserveDuration()

			if want, have := tc.want, h.ApproximateMovingAverage(); math.Abs(want-have) > tc.tolerance {
				t.Errorf("want %.3f, have %.3f", want, have)
			}
		})
	}
}