-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathregistry_test.go
More file actions
94 lines (71 loc) · 1.89 KB
/
registry_test.go
File metadata and controls
94 lines (71 loc) · 1.89 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package metrics
import (
"github.com/openziti/metrics/metrics_pb"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
type testData struct {
closeNotify chan struct{}
registry *usageRegistryImpl
events []*metrics_pb.MetricsMessage
}
func setUpTest(t *testing.T) *testData {
closeNotify := make(chan struct{})
config := DefaultUsageRegistryConfig(t.Name(), closeNotify)
td := &testData{
closeNotify: closeNotify,
registry: NewUsageRegistry(config).(*usageRegistryImpl),
}
td.registry.StartReporting(nil, time.Hour, 10)
return td
}
func (t *testData) Shutdown() {
close(t.closeNotify)
}
func (t *testData) AcceptMetrics(e *metrics_pb.MetricsMessage) {
t.events = append(t.events, e)
}
func TestEmpty(t *testing.T) {
td := setUpTest(t)
defer td.Shutdown()
td.registry.FlushToHandler(td)
assert.Len(t, td.events, 0)
}
func Test_Histogram(t *testing.T) {
td := setUpTest(t)
defer td.Shutdown()
hist := td.registry.Histogram("test.hist")
hist.Update(10)
td.registry.FlushToHandler(td)
assert.Len(t, td.events, 1)
ev := td.events[0]
assert.Nil(t, ev.FloatValues)
assert.Nil(t, ev.Meters)
assert.Nil(t, ev.IntValues)
assert.NotNil(t, ev.Histograms)
hm := ev.Histograms["test.hist"]
assert.NotNil(t, hm)
assert.Equal(t, int64(10), hm.Min)
assert.Equal(t, int64(10), hm.Max)
}
func Test_Timer(t *testing.T) {
td := setUpTest(t)
defer td.Shutdown()
timer := td.registry.Timer("test.timer")
timer.Update(3 * time.Second)
timer.Time(func() {
<-time.After(time.Second)
})
td.registry.FlushToHandler(td)
assert.Len(t, td.events, 1)
ev := td.events[0]
assert.Nil(t, ev.FloatValues)
assert.Nil(t, ev.Meters)
assert.Nil(t, ev.IntValues)
hm := ev.Timers["test.timer"]
assert.NotNil(t, hm)
assert.Equal(t, int64(2), hm.Count)
assert.Equal(t, 3*time.Second, time.Duration(hm.Max))
assert.InDelta(t, time.Second, time.Duration(hm.Min), float64(2*time.Millisecond))
}