export_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "reflect"
  17. "testing"
  18. )
  19. func clear() {
  20. defaultVarGroup.vars = make(map[string]expvar.Var)
  21. defaultVarGroup.newVarHook = nil
  22. *combineDimensions = ""
  23. *dropVariables = ""
  24. combinedDimensions = nil
  25. droppedVars = nil
  26. }
  27. func TestNoHook(t *testing.T) {
  28. clear()
  29. v := NewCounter("plainint", "help")
  30. v.Add(1)
  31. if v.String() != "1" {
  32. t.Errorf("want 1, got %s", v.String())
  33. }
  34. }
  35. func TestString(t *testing.T) {
  36. var gotname string
  37. var gotv *String
  38. clear()
  39. Register(func(name string, v expvar.Var) {
  40. gotname = name
  41. gotv = v.(*String)
  42. })
  43. v := NewString("String")
  44. if gotname != "String" {
  45. t.Errorf("want String, got %s", gotname)
  46. }
  47. if gotv != v {
  48. t.Errorf("want %#v, got %#v", v, gotv)
  49. }
  50. v.Set("a\"b")
  51. if v.Get() != "a\"b" {
  52. t.Errorf("want \"a\"b\", got %#v", gotv)
  53. }
  54. if v.String() != "\"a\\\"b\"" {
  55. t.Errorf("want \"\"a\\\"b\"\", got %#v", gotv)
  56. }
  57. f := StringFunc(func() string {
  58. return "a"
  59. })
  60. if f.String() != "\"a\"" {
  61. t.Errorf("want \"a\", got %v", f.String())
  62. }
  63. }
  64. type Mystr string
  65. func (m *Mystr) String() string {
  66. return string(*m)
  67. }
  68. func TestPublish(t *testing.T) {
  69. var gotname string
  70. var gotv expvar.Var
  71. clear()
  72. Register(func(name string, v expvar.Var) {
  73. gotname = name
  74. gotv = v.(*Mystr)
  75. })
  76. v := Mystr("abcd")
  77. Publish("Mystr", &v)
  78. if gotname != "Mystr" {
  79. t.Errorf("want Mystr, got %s", gotname)
  80. }
  81. if gotv != &v {
  82. t.Errorf("want %#v, got %#v", &v, gotv)
  83. }
  84. }
  85. func f() string {
  86. return "abcd"
  87. }
  88. type expvarFunc func() string
  89. func (f expvarFunc) String() string {
  90. return f()
  91. }
  92. func TestPublishFunc(t *testing.T) {
  93. var gotname string
  94. var gotv expvarFunc
  95. clear()
  96. Register(func(name string, v expvar.Var) {
  97. gotname = name
  98. gotv = v.(expvarFunc)
  99. })
  100. publish("Myfunc", expvarFunc(f))
  101. if gotname != "Myfunc" {
  102. t.Errorf("want Myfunc, got %s", gotname)
  103. }
  104. if gotv.String() != f() {
  105. t.Errorf("want %v, got %#v", f(), gotv())
  106. }
  107. }
  108. func TestDropVariable(t *testing.T) {
  109. clear()
  110. *dropVariables = "dropTest"
  111. // This should not panic.
  112. _ = NewGaugesWithSingleLabel("dropTest", "help", "label")
  113. _ = NewGaugesWithSingleLabel("dropTest", "help", "label")
  114. }
  115. func TestStringMapToString(t *testing.T) {
  116. expected1 := "{\"aaa\": \"111\", \"bbb\": \"222\"}"
  117. expected2 := "{\"bbb\": \"222\", \"aaa\": \"111\"}"
  118. got := stringMapToString(map[string]string{"aaa": "111", "bbb": "222"})
  119. if got != expected1 && got != expected2 {
  120. t.Errorf("expected %v or %v, got %v", expected1, expected2, got)
  121. }
  122. }
  123. func TestParseCommonTags(t *testing.T) {
  124. res := ParseCommonTags("")
  125. if len(res) != 0 {
  126. t.Errorf("expected empty result, got %v", res)
  127. }
  128. res = ParseCommonTags("s,a:b ")
  129. expected1 := map[string]string{"a": "b"}
  130. if !reflect.DeepEqual(expected1, res) {
  131. t.Errorf("expected %v, got %v", expected1, res)
  132. }
  133. res = ParseCommonTags("a:b, c:d")
  134. expected2 := map[string]string{"a": "b", "c": "d"}
  135. if !reflect.DeepEqual(expected2, res) {
  136. t.Errorf("expected %v, got %v", expected2, res)
  137. }
  138. }