duration_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "time"
  18. )
  19. func TestCounterDuration(t *testing.T) {
  20. var gotname string
  21. var gotv *CounterDuration
  22. clear()
  23. Register(func(name string, v expvar.Var) {
  24. gotname = name
  25. gotv = v.(*CounterDuration)
  26. })
  27. v := NewCounterDuration("CounterDuration", "help")
  28. if gotname != "CounterDuration" {
  29. t.Errorf("want CounterDuration, got %s", gotname)
  30. }
  31. if gotv != v {
  32. t.Errorf("want %#v, got %#v", v, gotv)
  33. }
  34. if v.Get() != 0 {
  35. t.Errorf("want 0, got %v", v.Get())
  36. }
  37. v.Add(time.Duration(1))
  38. if v.Get() != 1 {
  39. t.Errorf("want 1, got %v", v.Get())
  40. }
  41. if v.String() != "1" {
  42. t.Errorf("want 1, got %v", v.Get())
  43. }
  44. }
  45. func TestCounterDurationFunc(t *testing.T) {
  46. var gotname string
  47. var gotv *CounterDurationFunc
  48. clear()
  49. Register(func(name string, v expvar.Var) {
  50. gotname = name
  51. gotv = v.(*CounterDurationFunc)
  52. })
  53. v := NewCounterDurationFunc("CounterDurationFunc", "help", func() time.Duration {
  54. return time.Duration(1)
  55. })
  56. if gotname != "CounterDurationFunc" {
  57. t.Errorf("want CounterDurationFunc, 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. }
  66. func TestGaugeDuration(t *testing.T) {
  67. var gotname string
  68. var gotv *GaugeDuration
  69. clear()
  70. Register(func(name string, v expvar.Var) {
  71. gotname = name
  72. gotv = v.(*GaugeDuration)
  73. })
  74. v := NewGaugeDuration("GaugeDuration", "help")
  75. if gotname != "GaugeDuration" {
  76. t.Errorf("want GaugeDuration, got %s", gotname)
  77. }
  78. if gotv != v {
  79. t.Errorf("want %#v, got %#v", v, gotv)
  80. }
  81. v.Set(time.Duration(5))
  82. if v.Get() != 5 {
  83. t.Errorf("want 5, got %v", v.Get())
  84. }
  85. v.Add(time.Duration(1))
  86. if v.Get() != 6 {
  87. t.Errorf("want 6, got %v", v.Get())
  88. }
  89. if v.String() != "6" {
  90. t.Errorf("want 6, got %v", v.Get())
  91. }
  92. }
  93. func TestGaugeDurationFunc(t *testing.T) {
  94. var gotname string
  95. var gotv *GaugeDurationFunc
  96. clear()
  97. Register(func(name string, v expvar.Var) {
  98. gotname = name
  99. gotv = v.(*GaugeDurationFunc)
  100. })
  101. v := NewGaugeDurationFunc("GaugeDurationFunc", "help", func() time.Duration {
  102. return time.Duration(1)
  103. })
  104. if gotname != "GaugeDurationFunc" {
  105. t.Errorf("want GaugeDurationFunc, got %s", gotname)
  106. }
  107. if gotv != v {
  108. t.Errorf("want %#v, got %#v", v, gotv)
  109. }
  110. if v.String() != "1" {
  111. t.Errorf("want 1, got %v", v.String())
  112. }
  113. }