|
21 | 21 | package metrics |
22 | 22 |
|
23 | 23 | import ( |
| 24 | + "io" |
| 25 | + "sync" |
24 | 26 | "testing" |
25 | 27 | "time" |
26 | 28 |
|
27 | 29 | "github.com/stretchr/testify/require" |
28 | 30 | "github.com/uber-go/tally" |
29 | | - "io" |
30 | | - "sync" |
31 | 31 | ) |
32 | 32 |
|
33 | 33 | func Test_Counter(t *testing.T) { |
34 | | - isReplay := true |
35 | | - scope, closer, reporter := NewMetricsScope(&isReplay) |
36 | | - scope.Counter("test-name").Inc(1) |
37 | | - closer.Close() |
38 | | - require.Equal(t, 0, len(reporter.Counts())) |
39 | | - |
40 | | - isReplay = false |
41 | | - scope, closer, reporter = NewMetricsScope(&isReplay) |
42 | | - scope.Counter("test-name").Inc(3) |
43 | | - closer.Close() |
44 | | - require.Equal(t, 1, len(reporter.Counts())) |
45 | | - require.Equal(t, int64(3), reporter.Counts()[0].Value()) |
| 34 | + t.Parallel() |
| 35 | + replayed, executed := withScope(t, func(scope tally.Scope) { |
| 36 | + scope.Counter("test-name").Inc(3) |
| 37 | + }) |
| 38 | + require.Equal(t, 0, len(replayed.Counts())) |
| 39 | + require.Equal(t, 1, len(executed.Counts())) |
| 40 | + require.Equal(t, int64(3), executed.Counts()[0].Value()) |
46 | 41 | } |
47 | 42 |
|
48 | 43 | func Test_Gauge(t *testing.T) { |
49 | | - isReplay := true |
50 | | - scope, closer, reporter := NewMetricsScope(&isReplay) |
51 | | - scope.Gauge("test-name").Update(1) |
52 | | - closer.Close() |
53 | | - require.Equal(t, 0, len(reporter.Gauges())) |
54 | | - |
55 | | - isReplay = false |
56 | | - scope, closer, reporter = NewMetricsScope(&isReplay) |
57 | | - scope.Gauge("test-name").Update(3) |
58 | | - closer.Close() |
59 | | - require.Equal(t, 1, len(reporter.Gauges())) |
60 | | - require.Equal(t, float64(3), reporter.Gauges()[0].Value()) |
| 44 | + t.Parallel() |
| 45 | + replayed, executed := withScope(t, func(scope tally.Scope) { |
| 46 | + scope.Gauge("test-name").Update(3) |
| 47 | + }) |
| 48 | + require.Equal(t, 0, len(replayed.Gauges())) |
| 49 | + require.Equal(t, 1, len(executed.Gauges())) |
| 50 | + require.Equal(t, float64(3), executed.Gauges()[0].Value()) |
61 | 51 | } |
62 | 52 |
|
63 | 53 | func Test_Timer(t *testing.T) { |
64 | | - isReplay := true |
65 | | - scope, closer, reporter := NewMetricsScope(&isReplay) |
66 | | - scope.Timer("test-name").Record(time.Second) |
67 | | - sw := scope.Timer("test-stopwatch").Start() |
68 | | - sw.Stop() |
69 | | - closer.Close() |
70 | | - require.Equal(t, 0, len(reporter.Timers())) |
71 | | - |
72 | | - isReplay = false |
73 | | - scope, closer, reporter = NewMetricsScope(&isReplay) |
74 | | - scope.Timer("test-name").Record(time.Second) |
75 | | - sw = scope.Timer("test-stopwatch").Start() |
76 | | - sw.Stop() |
77 | | - closer.Close() |
78 | | - require.Equal(t, 2, len(reporter.Timers())) |
79 | | - require.Equal(t, time.Second, reporter.Timers()[0].Value()) |
| 54 | + t.Parallel() |
| 55 | + replayed, executed := withScope(t, func(scope tally.Scope) { |
| 56 | + scope.Timer("test-name").Record(time.Second) |
| 57 | + scope.Timer("test-stopwatch").Start().Stop() |
| 58 | + }) |
| 59 | + require.Equal(t, 0, len(replayed.Timers())) |
| 60 | + require.Equal(t, 2, len(executed.Timers())) |
| 61 | + require.Equal(t, time.Second, executed.Timers()[0].Value()) |
80 | 62 | } |
81 | 63 |
|
82 | 64 | func Test_Histogram(t *testing.T) { |
83 | | - isReplay := true |
84 | | - scope, closer, reporter := NewMetricsScope(&isReplay) |
85 | | - valueBuckets := tally.MustMakeLinearValueBuckets(0, 10, 10) |
86 | | - scope.Histogram("test-hist-1", valueBuckets).RecordValue(5) |
87 | | - scope.Histogram("test-hist-2", valueBuckets).RecordValue(15) |
88 | | - closer.Close() |
89 | | - require.Equal(t, 0, len(reporter.HistogramValueSamples())) |
90 | | - scope, closer, reporter = NewMetricsScope(&isReplay) |
91 | | - durationBuckets := tally.MustMakeLinearDurationBuckets(0, time.Hour, 10) |
92 | | - scope.Histogram("test-hist-1", durationBuckets).RecordDuration(time.Minute) |
93 | | - scope.Histogram("test-hist-2", durationBuckets).RecordDuration(time.Minute * 61) |
94 | | - sw := scope.Histogram("test-hist-3", durationBuckets).Start() |
95 | | - sw.Stop() |
96 | | - closer.Close() |
97 | | - require.Equal(t, 0, len(reporter.HistogramDurationSamples())) |
98 | | - |
99 | | - isReplay = false |
100 | | - scope, closer, reporter = NewMetricsScope(&isReplay) |
101 | | - valueBuckets = tally.MustMakeLinearValueBuckets(0, 10, 10) |
102 | | - scope.Histogram("test-hist-1", valueBuckets).RecordValue(5) |
103 | | - scope.Histogram("test-hist-2", valueBuckets).RecordValue(15) |
104 | | - closer.Close() |
105 | | - require.Equal(t, 2, len(reporter.HistogramValueSamples())) |
106 | | - |
107 | | - scope, closer, reporter = NewMetricsScope(&isReplay) |
108 | | - durationBuckets = tally.MustMakeLinearDurationBuckets(0, time.Hour, 10) |
109 | | - scope.Histogram("test-hist-1", durationBuckets).RecordDuration(time.Minute) |
110 | | - scope.Histogram("test-hist-2", durationBuckets).RecordDuration(time.Minute * 61) |
111 | | - sw = scope.Histogram("test-hist-3", durationBuckets).Start() |
112 | | - sw.Stop() |
113 | | - closer.Close() |
114 | | - require.Equal(t, 3, len(reporter.HistogramDurationSamples())) |
| 65 | + t.Parallel() |
| 66 | + t.Run("values", func(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + replayed, executed := withScope(t, func(scope tally.Scope) { |
| 69 | + valueBuckets := tally.MustMakeLinearValueBuckets(0, 10, 10) |
| 70 | + scope.Histogram("test-hist-1", valueBuckets).RecordValue(5) |
| 71 | + scope.Histogram("test-hist-2", valueBuckets).RecordValue(15) |
| 72 | + }) |
| 73 | + require.Equal(t, 0, len(replayed.HistogramValueSamples())) |
| 74 | + require.Equal(t, 2, len(executed.HistogramValueSamples())) |
| 75 | + }) |
| 76 | + t.Run("durations", func(t *testing.T) { |
| 77 | + t.Parallel() |
| 78 | + replayed, executed := withScope(t, func(scope tally.Scope) { |
| 79 | + durationBuckets := tally.MustMakeLinearDurationBuckets(0, time.Hour, 10) |
| 80 | + scope.Histogram("test-hist-1", durationBuckets).RecordDuration(time.Minute) |
| 81 | + scope.Histogram("test-hist-2", durationBuckets).RecordDuration(time.Minute * 61) |
| 82 | + scope.Histogram("test-hist-3", durationBuckets).Start().Stop() |
| 83 | + }) |
| 84 | + require.Equal(t, 0, len(replayed.HistogramDurationSamples())) |
| 85 | + require.Equal(t, 3, len(executed.HistogramDurationSamples())) |
| 86 | + }) |
115 | 87 | } |
116 | 88 |
|
117 | 89 | func Test_ScopeCoverage(t *testing.T) { |
@@ -190,6 +162,25 @@ func newTaggedMetricsScope() (*TaggedScope, io.Closer, *capturingStatsReporter) |
190 | 162 | return &TaggedScope{Scope: scope}, closer, reporter |
191 | 163 | } |
192 | 164 |
|
| 165 | +// withScope runs your callback twice, once for "during replay" and once for "after replay" / "executing". |
| 166 | +// stats are captured, and the results are returned for your validation. |
| 167 | +func withScope(t *testing.T, cb func(scope tally.Scope)) (replayed *CapturingStatsReporter, executed *CapturingStatsReporter) { |
| 168 | + replaying, executing := true, false |
| 169 | + |
| 170 | + replayingScope, replayingCloser, replayed := NewMetricsScope(&replaying) |
| 171 | + executingScope, executingCloser, executed := NewMetricsScope(&executing) |
| 172 | + |
| 173 | + defer func() { |
| 174 | + require.NoError(t, replayingCloser.Close()) |
| 175 | + require.NoError(t, executingCloser.Close()) |
| 176 | + }() |
| 177 | + |
| 178 | + cb(replayingScope) |
| 179 | + cb(executingScope) |
| 180 | + |
| 181 | + return replayed, executed |
| 182 | +} |
| 183 | + |
193 | 184 | // capturingStatsReporter is a reporter used by tests to capture the metric so we can verify our tests. |
194 | 185 | type capturingStatsReporter struct { |
195 | 186 | counts []capturedCount |
|
0 commit comments