Skip to content

Commit 4914507

Browse files
author
Dmitry Verkhoturov
committed
add helpers tests
1 parent 03b49f3 commit 4914507

File tree

5 files changed

+52
-13
lines changed

5 files changed

+52
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func main() {
8080
os.Exit(1)
8181
}
8282
// collect data using provided functions with provided arguments once a minute
83-
go metrics.Scheduler(time.Minute, metrics.GoRuntimeStats, []string{""})
83+
go metrics.Scheduler(time.Minute, metrics.GoRuntimeStats, []string{})
8484
go metrics.Scheduler(time.Minute, metrics.MeasureRuntime, []string{"uptime"}, time.Now())
8585

8686
//<...>

helpers.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,10 @@ func GoRuntimeStats(prefix []string) {
108108
// freed.
109109
UpdateGauge(append(prefix, "heap_objects"), int64(s.HeapReleased))
110110
}
111+
112+
// MeasureRuntime measures time elapsed since invocation
113+
func MeasureRuntime(key []string, start time.Time) {
114+
//convert time.Duration to milliseconds
115+
elapsed := int64(time.Since(start) / time.Millisecond)
116+
UpdateGauge(key, elapsed)
117+
}

helpers_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package metrics
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/rcrowley/go-metrics"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestHelpers(t *testing.T) {
12+
_ = Setup(
13+
"graphite",
14+
"localhost",
15+
2003,
16+
"",
17+
time.Hour, // don't flush metrics until Close call
18+
)
19+
20+
go Scheduler(time.Hour, MeasureRuntime, []string{"uptime"}, time.Time{})
21+
GoRuntimeStats([]string{"r"})
22+
23+
// wait for stats to be dumped
24+
Close(true)
25+
26+
// check GoRuntimeStats
27+
assert.Nil(t, emm.registry.Get("runtime.random_metric"), "random metric is not present")
28+
assert.NotNil(t, emm.registry.Get("r.runtime.num_goroutines"))
29+
assert.NotNil(t, emm.registry.Get("r.runtime.heap_alloc"))
30+
assert.NotNil(t, emm.registry.Get("r.runtime.sys"))
31+
assert.NotNil(t, emm.registry.Get("r.runtime.pause_total_ns"))
32+
assert.NotNil(t, emm.registry.Get("r.runtime.num_gc"))
33+
assert.NotNil(t, emm.registry.Get("r.runtime.heap_released"))
34+
assert.NotNil(t, emm.registry.Get("r.runtime.heap_objects"))
35+
36+
// check Scheduler and MeasureRuntime
37+
// Scheduler problem cases are not tested
38+
assert.Equal(t, (time.Now().Sub(time.Time{}) / time.Millisecond).Nanoseconds(), emm.registry.Get("uptime").(metrics.Gauge).Value())
39+
40+
// cleanup
41+
emm.registry.UnregisterAll()
42+
}

metrics.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,8 @@ func UpdateHistogram(key []string, value int64) {
199199
emm.metricsChan <- d
200200
}
201201

202-
// MeasureRuntime measures time elapsed since invocation
203-
func MeasureRuntime(key []string, start time.Time) {
204-
//convert time.Duration to milliseconds
205-
elapsed := int64(time.Since(start) / time.Millisecond)
206-
UpdateGauge(key, elapsed)
207-
}
208-
209202
// Close runs cleanup actions
210203
func Close(printStats bool) {
211-
close(emm.metricsChan)
212-
213204
if printStats {
214205
log.Info(emm.registry.GetAll())
215206
}

metrics_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ func TestMetricsStoreGraphite(t *testing.T) {
3030
UpdateTimer([]string{"happy_time"}, time.Minute)
3131
UpdateTimer([]string{"happy_time"}, time.Second)
3232

33-
MeasureRuntime([]string{"uptime"}, time.Time{})
34-
3533
// wait for stats to be dumped
3634
Close(true)
3735

@@ -41,7 +39,6 @@ func TestMetricsStoreGraphite(t *testing.T) {
4139
assert.Equal(t, int64(42), emm.registry.Get("happy_routine.happiness_hit").(metrics.Histogram).Sum())
4240
assert.Equal(t, int64(2), emm.registry.Get("happy_time").(metrics.Timer).Count())
4341
assert.Equal(t, (time.Minute + time.Second).Nanoseconds(), emm.registry.Get("happy_time").(metrics.Timer).Sum())
44-
assert.Equal(t, (time.Now().Sub(time.Time{}) / time.Millisecond).Nanoseconds(), emm.registry.Get("uptime").(metrics.Gauge).Value())
4542

4643
err = Setup(
4744
"graphite",
@@ -51,4 +48,6 @@ func TestMetricsStoreGraphite(t *testing.T) {
5148
time.Hour,
5249
)
5350
assert.Nil(t, err, "second call should do nothing and shouldn't return error")
51+
// cleanup
52+
emm.registry.UnregisterAll()
5453
}

0 commit comments

Comments
 (0)