github.com/core-coin/go-core/v2@v2.1.9/metrics/registry_test.go (about) 1 package metrics 2 3 import ( 4 "testing" 5 ) 6 7 func BenchmarkRegistry(b *testing.B) { 8 r := NewRegistry() 9 r.Register("foo", NewCounter()) 10 b.ResetTimer() 11 for i := 0; i < b.N; i++ { 12 r.Each(func(string, interface{}) {}) 13 } 14 } 15 16 func TestRegistry(t *testing.T) { 17 r := NewRegistry() 18 r.Register("foo", NewCounter()) 19 i := 0 20 r.Each(func(name string, iface interface{}) { 21 i++ 22 if name != "foo" { 23 t.Fatal(name) 24 } 25 if _, ok := iface.(Counter); !ok { 26 t.Fatal(iface) 27 } 28 }) 29 if i != 1 { 30 t.Fatal(i) 31 } 32 r.Unregister("foo") 33 i = 0 34 r.Each(func(string, interface{}) { i++ }) 35 if i != 0 { 36 t.Fatal(i) 37 } 38 } 39 40 func TestRegistryDuplicate(t *testing.T) { 41 r := NewRegistry() 42 if err := r.Register("foo", NewCounter()); nil != err { 43 t.Fatal(err) 44 } 45 if err := r.Register("foo", NewGauge()); nil == err { 46 t.Fatal(err) 47 } 48 i := 0 49 r.Each(func(name string, iface interface{}) { 50 i++ 51 if _, ok := iface.(Counter); !ok { 52 t.Fatal(iface) 53 } 54 }) 55 if i != 1 { 56 t.Fatal(i) 57 } 58 } 59 60 func TestRegistryGet(t *testing.T) { 61 r := NewRegistry() 62 r.Register("foo", NewCounter()) 63 if count := r.Get("foo").(Counter).Count(); count != 0 { 64 t.Fatal(count) 65 } 66 r.Get("foo").(Counter).Inc(1) 67 if count := r.Get("foo").(Counter).Count(); count != 1 { 68 t.Fatal(count) 69 } 70 } 71 72 func TestRegistryGetOrRegister(t *testing.T) { 73 r := NewRegistry() 74 75 // First metric wins with GetOrRegister 76 _ = r.GetOrRegister("foo", NewCounter()) 77 m := r.GetOrRegister("foo", NewGauge()) 78 if _, ok := m.(Counter); !ok { 79 t.Fatal(m) 80 } 81 82 i := 0 83 r.Each(func(name string, iface interface{}) { 84 i++ 85 if name != "foo" { 86 t.Fatal(name) 87 } 88 if _, ok := iface.(Counter); !ok { 89 t.Fatal(iface) 90 } 91 }) 92 if i != 1 { 93 t.Fatal(i) 94 } 95 } 96 97 func TestRegistryGetOrRegisterWithLazyInstantiation(t *testing.T) { 98 r := NewRegistry() 99 100 // First metric wins with GetOrRegister 101 _ = r.GetOrRegister("foo", NewCounter) 102 m := r.GetOrRegister("foo", NewGauge) 103 if _, ok := m.(Counter); !ok { 104 t.Fatal(m) 105 } 106 107 i := 0 108 r.Each(func(name string, iface interface{}) { 109 i++ 110 if name != "foo" { 111 t.Fatal(name) 112 } 113 if _, ok := iface.(Counter); !ok { 114 t.Fatal(iface) 115 } 116 }) 117 if i != 1 { 118 t.Fatal(i) 119 } 120 } 121 122 func TestRegistryUnregister(t *testing.T) { 123 l := len(arbiter.meters) 124 r := NewRegistry() 125 r.Register("foo", NewCounter()) 126 r.Register("bar", NewMeter()) 127 r.Register("baz", NewTimer()) 128 if len(arbiter.meters) != l+2 { 129 t.Errorf("arbiter.meters: %d != %d\n", l+2, len(arbiter.meters)) 130 } 131 r.Unregister("foo") 132 r.Unregister("bar") 133 r.Unregister("baz") 134 if len(arbiter.meters) != l { 135 t.Errorf("arbiter.meters: %d != %d\n", l+2, len(arbiter.meters)) 136 } 137 } 138 139 func TestPrefixedChildRegistryGetOrRegister(t *testing.T) { 140 r := NewRegistry() 141 pr := NewPrefixedChildRegistry(r, "prefix.") 142 143 _ = pr.GetOrRegister("foo", NewCounter()) 144 145 i := 0 146 r.Each(func(name string, m interface{}) { 147 i++ 148 if name != "prefix.foo" { 149 t.Fatal(name) 150 } 151 }) 152 if i != 1 { 153 t.Fatal(i) 154 } 155 } 156 157 func TestPrefixedRegistryGetOrRegister(t *testing.T) { 158 r := NewPrefixedRegistry("prefix.") 159 160 _ = r.GetOrRegister("foo", NewCounter()) 161 162 i := 0 163 r.Each(func(name string, m interface{}) { 164 i++ 165 if name != "prefix.foo" { 166 t.Fatal(name) 167 } 168 }) 169 if i != 1 { 170 t.Fatal(i) 171 } 172 } 173 174 func TestPrefixedRegistryRegister(t *testing.T) { 175 r := NewPrefixedRegistry("prefix.") 176 err := r.Register("foo", NewCounter()) 177 c := NewCounter() 178 Register("bar", c) 179 if err != nil { 180 t.Fatal(err.Error()) 181 } 182 183 i := 0 184 r.Each(func(name string, m interface{}) { 185 i++ 186 if name != "prefix.foo" { 187 t.Fatal(name) 188 } 189 }) 190 if i != 1 { 191 t.Fatal(i) 192 } 193 } 194 195 func TestPrefixedRegistryUnregister(t *testing.T) { 196 r := NewPrefixedRegistry("prefix.") 197 198 _ = r.Register("foo", NewCounter()) 199 200 i := 0 201 r.Each(func(name string, m interface{}) { 202 i++ 203 if name != "prefix.foo" { 204 t.Fatal(name) 205 } 206 }) 207 if i != 1 { 208 t.Fatal(i) 209 } 210 211 r.Unregister("foo") 212 213 i = 0 214 r.Each(func(name string, m interface{}) { 215 i++ 216 }) 217 218 if i != 0 { 219 t.Fatal(i) 220 } 221 } 222 223 func TestPrefixedRegistryGet(t *testing.T) { 224 pr := NewPrefixedRegistry("prefix.") 225 name := "foo" 226 pr.Register(name, NewCounter()) 227 228 fooCounter := pr.Get(name) 229 if fooCounter == nil { 230 t.Fatal(name) 231 } 232 } 233 234 func TestPrefixedChildRegistryGet(t *testing.T) { 235 r := NewRegistry() 236 pr := NewPrefixedChildRegistry(r, "prefix.") 237 name := "foo" 238 pr.Register(name, NewCounter()) 239 fooCounter := pr.Get(name) 240 if fooCounter == nil { 241 t.Fatal(name) 242 } 243 } 244 245 func TestChildPrefixedRegistryRegister(t *testing.T) { 246 r := NewPrefixedChildRegistry(DefaultRegistry, "prefix.") 247 err := r.Register("foo", NewCounter()) 248 c := NewCounter() 249 Register("bar", c) 250 if err != nil { 251 t.Fatal(err.Error()) 252 } 253 254 i := 0 255 r.Each(func(name string, m interface{}) { 256 i++ 257 if name != "prefix.foo" { 258 t.Fatal(name) 259 } 260 }) 261 if i != 1 { 262 t.Fatal(i) 263 } 264 } 265 266 func TestChildPrefixedRegistryOfChildRegister(t *testing.T) { 267 r := NewPrefixedChildRegistry(NewRegistry(), "prefix.") 268 r2 := NewPrefixedChildRegistry(r, "prefix2.") 269 err := r.Register("foo2", NewCounter()) 270 if err != nil { 271 t.Fatal(err.Error()) 272 } 273 err = r2.Register("baz", NewCounter()) 274 if err != nil { 275 t.Fatal(err.Error()) 276 } 277 c := NewCounter() 278 Register("bars", c) 279 280 i := 0 281 r2.Each(func(name string, m interface{}) { 282 i++ 283 if name != "prefix.prefix2.baz" { 284 t.Fatal(name) 285 } 286 }) 287 if i != 1 { 288 t.Fatal(i) 289 } 290 } 291 292 func TestWalkRegistries(t *testing.T) { 293 r := NewPrefixedChildRegistry(NewRegistry(), "prefix.") 294 r2 := NewPrefixedChildRegistry(r, "prefix2.") 295 err := r.Register("foo2", NewCounter()) 296 if err != nil { 297 t.Fatal(err.Error()) 298 } 299 err = r2.Register("baz", NewCounter()) 300 if err != nil { 301 t.Fatal(err.Error()) 302 } 303 c := NewCounter() 304 Register("bars", c) 305 306 _, prefix := findPrefix(r2, "") 307 if prefix != "prefix.prefix2." { 308 t.Fatal(prefix) 309 } 310 311 }