registry_test.go 5.5 KB

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