counter_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2019 The Vitess Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package stats
  14. import (
  15. "expvar"
  16. "testing"
  17. )
  18. func TestCounter(t *testing.T) {
  19. var gotname string
  20. var gotv *Counter
  21. clear()
  22. Register(func(name string, v expvar.Var) {
  23. gotname = name
  24. gotv = v.(*Counter)
  25. })
  26. v := NewCounter("Int", "help")
  27. if gotname != "Int" {
  28. t.Errorf("want Int, got %s", gotname)
  29. }
  30. if gotv != v {
  31. t.Errorf("want %#v, got %#v", v, gotv)
  32. }
  33. v.Add(1)
  34. if v.Get() != 1 {
  35. t.Errorf("want 1, got %v", v.Get())
  36. }
  37. if v.String() != "1" {
  38. t.Errorf("want 1, got %v", v.Get())
  39. }
  40. v.Reset()
  41. if v.Get() != 0 {
  42. t.Errorf("want 0, got %v", v.Get())
  43. }
  44. }
  45. func TestGaugeFunc(t *testing.T) {
  46. var gotname string
  47. var gotv *GaugeFunc
  48. clear()
  49. Register(func(name string, v expvar.Var) {
  50. gotname = name
  51. gotv = v.(*GaugeFunc)
  52. })
  53. v := NewGaugeFunc("name", "help", func() int64 {
  54. return 1
  55. })
  56. if gotname != "name" {
  57. t.Errorf("want name, got %s", gotname)
  58. }
  59. if gotv != v {
  60. t.Errorf("want %#v, got %#v", v, gotv)
  61. }
  62. if v.String() != "1" {
  63. t.Errorf("want 1, got %v", v.String())
  64. }
  65. }