mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-24 11:55:40 +00:00
179 lines
3.1 KiB
Go
179 lines
3.1 KiB
Go
|
package metrics
|
||
|
|
||
|
import "testing"
|
||
|
|
||
|
func BenchmarkRegistry(b *testing.B) {
|
||
|
r := NewRegistry()
|
||
|
r.Register("foo", NewCounter())
|
||
|
b.ResetTimer()
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
r.Each(func(string, interface{}) {})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRegistry(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
r.Register("foo", NewCounter())
|
||
|
i := 0
|
||
|
r.Each(func(name string, iface interface{}) {
|
||
|
i++
|
||
|
if "foo" != name {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
if _, ok := iface.(Counter); !ok {
|
||
|
t.Fatal(iface)
|
||
|
}
|
||
|
})
|
||
|
if 1 != i {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
r.Unregister("foo")
|
||
|
i = 0
|
||
|
r.Each(func(string, interface{}) { i++ })
|
||
|
if 0 != i {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRegistryDuplicate(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
if err := r.Register("foo", NewCounter()); nil != err {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
if err := r.Register("foo", NewGauge()); nil == err {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
i := 0
|
||
|
r.Each(func(name string, iface interface{}) {
|
||
|
i++
|
||
|
if _, ok := iface.(Counter); !ok {
|
||
|
t.Fatal(iface)
|
||
|
}
|
||
|
})
|
||
|
if 1 != i {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRegistryGet(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
r.Register("foo", NewCounter())
|
||
|
if count := r.Get("foo").(Counter).Count(); 0 != count {
|
||
|
t.Fatal(count)
|
||
|
}
|
||
|
r.Get("foo").(Counter).Inc(1)
|
||
|
if count := r.Get("foo").(Counter).Count(); 1 != count {
|
||
|
t.Fatal(count)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRegistryGetOrRegister(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
|
||
|
// First metric wins with GetOrRegister
|
||
|
_ = r.GetOrRegister("foo", NewCounter())
|
||
|
m := r.GetOrRegister("foo", NewGauge())
|
||
|
if _, ok := m.(Counter); !ok {
|
||
|
t.Fatal(m)
|
||
|
}
|
||
|
|
||
|
i := 0
|
||
|
r.Each(func(name string, iface interface{}) {
|
||
|
i++
|
||
|
if name != "foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
if _, ok := iface.(Counter); !ok {
|
||
|
t.Fatal(iface)
|
||
|
}
|
||
|
})
|
||
|
if i != 1 {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRegistryGetOrRegisterWithLazyInstantiation(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
|
||
|
// First metric wins with GetOrRegister
|
||
|
_ = r.GetOrRegister("foo", NewCounter)
|
||
|
m := r.GetOrRegister("foo", NewGauge)
|
||
|
if _, ok := m.(Counter); !ok {
|
||
|
t.Fatal(m)
|
||
|
}
|
||
|
|
||
|
i := 0
|
||
|
r.Each(func(name string, iface interface{}) {
|
||
|
i++
|
||
|
if name != "foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
if _, ok := iface.(Counter); !ok {
|
||
|
t.Fatal(iface)
|
||
|
}
|
||
|
})
|
||
|
if i != 1 {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPrefixedChildRegistryGetOrRegister(t *testing.T) {
|
||
|
r := NewRegistry()
|
||
|
pr := NewPrefixedChildRegistry(r, "prefix.")
|
||
|
|
||
|
_ = pr.GetOrRegister("foo", NewCounter)
|
||
|
|
||
|
r.Each(func(name string, m interface{}) {
|
||
|
if name != "prefix.foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestPrefixedRegistryGetOrRegister(t *testing.T) {
|
||
|
r := NewPrefixedRegistry("prefix.")
|
||
|
|
||
|
_ = r.GetOrRegister("foo", NewCounter)
|
||
|
|
||
|
r.Each(func(name string, m interface{}) {
|
||
|
if name != "prefix.foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestPrefixedRegistryRegister(t *testing.T) {
|
||
|
r := NewPrefixedRegistry("prefix.")
|
||
|
|
||
|
_ = r.Register("foo", NewCounter)
|
||
|
|
||
|
r.Each(func(name string, m interface{}) {
|
||
|
if name != "prefix.foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestPrefixedRegistryUnregister(t *testing.T) {
|
||
|
r := NewPrefixedRegistry("prefix.")
|
||
|
|
||
|
_ = r.Register("foo", NewCounter)
|
||
|
|
||
|
r.Each(func(name string, m interface{}) {
|
||
|
if name != "prefix.foo" {
|
||
|
t.Fatal(name)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
r.Unregister("foo")
|
||
|
|
||
|
i := 0
|
||
|
r.Each(func(name string, m interface{}) {
|
||
|
i++
|
||
|
})
|
||
|
|
||
|
if i != 0 {
|
||
|
t.Fatal(i)
|
||
|
}
|
||
|
}
|