forked from libp2p/go-flow-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry_test.go
124 lines (103 loc) · 2.49 KB
/
registry_test.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package flow
import (
"testing"
"time"
)
func TestRegistry(t *testing.T) {
r := new(MeterRegistry)
m1 := r.Get("first")
m2 := r.Get("second")
m1Update := m1.Snapshot().LastUpdate
m1.Mark(10)
m2.Mark(30)
time.Sleep(2*time.Second + time.Millisecond)
if total := r.Get("first").Snapshot().Total; total != 10 {
t.Errorf("expected first total to be 10, got %d", total)
}
if total := r.Get("second").Snapshot().Total; total != 30 {
t.Errorf("expected second total to be 30, got %d", total)
}
if !m1.Snapshot().LastUpdate.After(m1Update) {
t.Error("expected the last update to have been updated")
}
expectedMeters := map[string]*Meter{
"first": m1,
"second": m2,
}
r.ForEach(func(n string, m *Meter) {
if expectedMeters[n] != m {
t.Errorf("wrong meter '%s'", n)
}
delete(expectedMeters, n)
})
if len(expectedMeters) != 0 {
t.Errorf("missing meters: '%v'", expectedMeters)
}
r.Remove("first")
found := false
r.ForEach(func(n string, m *Meter) {
if n != "second" {
t.Errorf("found unexpected meter: %s", n)
return
}
if found {
t.Error("found meter twice")
}
found = true
})
if !found {
t.Errorf("didn't find second meter")
}
m3 := r.Get("first")
if m3 == m1 {
t.Error("should have gotten a new meter")
}
if total := m3.Snapshot().Total; total != 0 {
t.Errorf("expected first total to now be 0, got %d", total)
}
expectedMeters = map[string]*Meter{
"first": m3,
"second": m2,
}
r.ForEach(func(n string, m *Meter) {
if expectedMeters[n] != m {
t.Errorf("wrong meter '%s'", n)
}
delete(expectedMeters, n)
})
if len(expectedMeters) != 0 {
t.Errorf("missing meters: '%v'", expectedMeters)
}
before := time.Now()
m3.Mark(1)
time.Sleep(2 * time.Second)
after := time.Now()
if len(r.FindIdle(before)) != 1 {
t.Error("expected 1 idle timer")
}
if len(r.FindIdle(after)) != 2 {
t.Error("expected 2 idle timers")
}
count := r.TrimIdle(after)
if count != 2 {
t.Error("expected to trim 2 idle timers")
}
}
func TestClearRegistry(t *testing.T) {
r := new(MeterRegistry)
m1 := r.Get("first")
m2 := r.Get("second")
m1.Mark(10)
m2.Mark(30)
time.Sleep(2 * time.Second)
r.Clear()
r.ForEach(func(n string, _m *Meter) {
t.Errorf("expected no meters at all, found a meter %s", n)
})
if total := r.Get("first").Snapshot().Total; total != 0 {
t.Errorf("expected first total to be 0, got %d", total)
}
if total := r.Get("second").Snapshot().Total; total != 0 {
t.Errorf("expected second total to be 0, got %d", total)
}
}