vitess.io/vitess@v0.16.2/go/stats/export_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package stats 18 19 import ( 20 "expvar" 21 "reflect" 22 "testing" 23 ) 24 25 func clear() { 26 defaultVarGroup.vars = make(map[string]expvar.Var) 27 defaultVarGroup.newVarHook = nil 28 combineDimensions = "" 29 dropVariables = "" 30 combinedDimensions = nil 31 droppedVars = nil 32 } 33 34 func TestNoHook(t *testing.T) { 35 clear() 36 v := NewCounter("plainint", "help") 37 v.Add(1) 38 if v.String() != "1" { 39 t.Errorf("want 1, got %s", v.String()) 40 } 41 } 42 43 func TestString(t *testing.T) { 44 var gotname string 45 var gotv *String 46 clear() 47 Register(func(name string, v expvar.Var) { 48 gotname = name 49 gotv = v.(*String) 50 }) 51 v := NewString("String") 52 if gotname != "String" { 53 t.Errorf("want String, got %s", gotname) 54 } 55 if gotv != v { 56 t.Errorf("want %#v, got %#v", v, gotv) 57 } 58 v.Set("a\"b") 59 if v.Get() != "a\"b" { 60 t.Errorf("want \"a\"b\", got %#v", gotv) 61 } 62 if v.String() != "\"a\\\"b\"" { 63 t.Errorf("want \"\"a\\\"b\"\", got %#v", gotv) 64 } 65 66 f := StringFunc(func() string { 67 return "a" 68 }) 69 if f.String() != "\"a\"" { 70 t.Errorf("want \"a\", got %v", f.String()) 71 } 72 } 73 74 type Mystr string 75 76 func (m *Mystr) String() string { 77 return string(*m) 78 } 79 80 func TestPublish(t *testing.T) { 81 var gotname string 82 var gotv expvar.Var 83 clear() 84 Register(func(name string, v expvar.Var) { 85 gotname = name 86 gotv = v.(*Mystr) 87 }) 88 v := Mystr("abcd") 89 Publish("Mystr", &v) 90 if gotname != "Mystr" { 91 t.Errorf("want Mystr, got %s", gotname) 92 } 93 if gotv != &v { 94 t.Errorf("want %#v, got %#v", &v, gotv) 95 } 96 } 97 98 func f() string { 99 return "abcd" 100 } 101 102 type expvarFunc func() string 103 104 func (f expvarFunc) String() string { 105 return f() 106 } 107 108 func TestPublishFunc(t *testing.T) { 109 var gotname string 110 var gotv expvarFunc 111 clear() 112 Register(func(name string, v expvar.Var) { 113 gotname = name 114 gotv = v.(expvarFunc) 115 }) 116 publish("Myfunc", expvarFunc(f)) 117 if gotname != "Myfunc" { 118 t.Errorf("want Myfunc, got %s", gotname) 119 } 120 if gotv.String() != f() { 121 t.Errorf("want %v, got %#v", f(), gotv()) 122 } 123 } 124 125 func TestDropVariable(t *testing.T) { 126 clear() 127 dropVariables = "dropTest" 128 129 // This should not panic. 130 _ = NewGaugesWithSingleLabel("dropTest", "help", "label") 131 _ = NewGaugesWithSingleLabel("dropTest", "help", "label") 132 } 133 134 func TestStringMapToString(t *testing.T) { 135 expected1 := "{\"aaa\": \"111\", \"bbb\": \"222\"}" 136 expected2 := "{\"bbb\": \"222\", \"aaa\": \"111\"}" 137 got := stringMapToString(map[string]string{"aaa": "111", "bbb": "222"}) 138 139 if got != expected1 && got != expected2 { 140 t.Errorf("expected %v or %v, got %v", expected1, expected2, got) 141 } 142 } 143 144 func TestParseCommonTags(t *testing.T) { 145 res := ParseCommonTags([]string{""}) 146 if len(res) != 0 { 147 t.Errorf("expected empty result, got %v", res) 148 } 149 res = ParseCommonTags([]string{"s", "a:b"}) 150 expected1 := map[string]string{"a": "b"} 151 if !reflect.DeepEqual(expected1, res) { 152 t.Errorf("expected %v, got %v", expected1, res) 153 } 154 res = ParseCommonTags([]string{"a:b", "c:d"}) 155 expected2 := map[string]string{"a": "b", "c": "d"} 156 if !reflect.DeepEqual(expected2, res) { 157 t.Errorf("expected %v, got %v", expected2, res) 158 } 159 }