github.com/XiaoMi/Gaea@v1.2.5/stats/export_test.go (about) 1 /* 2 Copyright 2017 Google Inc. 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 "testing" 22 ) 23 24 func clear() { 25 defaultVarGroup.vars = make(map[string]expvar.Var) 26 defaultVarGroup.newVarHook = nil 27 } 28 29 func TestNoHook(t *testing.T) { 30 clear() 31 v := NewCounter("plainint", "help") 32 v.Add(1) 33 if v.String() != "1" { 34 t.Errorf("want 1, got %s", v.String()) 35 } 36 } 37 38 func TestFloat(t *testing.T) { 39 var gotname string 40 var gotv *Float 41 clear() 42 Register(func(name string, v expvar.Var) { 43 gotname = name 44 gotv = v.(*Float) 45 }) 46 v := NewFloat("Float") 47 if gotname != "Float" { 48 t.Errorf("want Float, got %s", gotname) 49 } 50 if gotv != v { 51 t.Errorf("want %#v, got %#v", v, gotv) 52 } 53 v.Set(5.1) 54 if v.Get() != 5.1 { 55 t.Errorf("want 5.1, got %v", v.Get()) 56 } 57 v.Add(1.0) 58 if v.Get() != 6.1 { 59 t.Errorf("want 6.1, got %v", v.Get()) 60 } 61 if v.String() != "6.1" { 62 t.Errorf("want 6.1, got %v", v.Get()) 63 } 64 65 f := FloatFunc(func() float64 { 66 return 1.234 67 }) 68 if f.String() != "1.234" { 69 t.Errorf("want 1.234, got %v", f.String()) 70 } 71 } 72 73 func TestString(t *testing.T) { 74 var gotname string 75 var gotv *String 76 clear() 77 Register(func(name string, v expvar.Var) { 78 gotname = name 79 gotv = v.(*String) 80 }) 81 v := NewString("String") 82 if gotname != "String" { 83 t.Errorf("want String, got %s", gotname) 84 } 85 if gotv != v { 86 t.Errorf("want %#v, got %#v", v, gotv) 87 } 88 v.Set("a\"b") 89 if v.Get() != "a\"b" { 90 t.Errorf("want \"a\"b\", got %#v", gotv) 91 } 92 if v.String() != "\"a\\\"b\"" { 93 t.Errorf("want \"\"a\\\"b\"\", got %#v", gotv) 94 } 95 96 f := StringFunc(func() string { 97 return "a" 98 }) 99 if f.String() != "\"a\"" { 100 t.Errorf("want \"a\", got %v", f.String()) 101 } 102 } 103 104 type Mystr string 105 106 func (m *Mystr) String() string { 107 return string(*m) 108 } 109 110 func TestPublish(t *testing.T) { 111 var gotname string 112 var gotv expvar.Var 113 clear() 114 Register(func(name string, v expvar.Var) { 115 gotname = name 116 gotv = v.(*Mystr) 117 }) 118 v := Mystr("abcd") 119 Publish("Mystr", &v) 120 if gotname != "Mystr" { 121 t.Errorf("want Mystr, got %s", gotname) 122 } 123 if gotv != &v { 124 t.Errorf("want %#v, got %#v", &v, gotv) 125 } 126 } 127 128 func f() string { 129 return "abcd" 130 } 131 132 func TestPublishFunc(t *testing.T) { 133 var gotname string 134 var gotv JSONFunc 135 clear() 136 Register(func(name string, v expvar.Var) { 137 gotname = name 138 gotv = v.(JSONFunc) 139 }) 140 PublishJSONFunc("Myfunc", f) 141 if gotname != "Myfunc" { 142 t.Errorf("want Myfunc, got %s", gotname) 143 } 144 if gotv.String() != f() { 145 t.Errorf("want %v, got %#v", f(), gotv()) 146 } 147 } 148 149 func TestStringMap(t *testing.T) { 150 clear() 151 c := NewStringMap("stringmap1") 152 c.Set("c1", "val1") 153 c.Set("c2", "val2") 154 c.Set("c2", "val3") 155 want1 := `{"c1": "val1", "c2": "val3"}` 156 want2 := `{"c2": "val3", "c1": "val1"}` 157 if s := c.String(); s != want1 && s != want2 { 158 t.Errorf("want %s or %s, got %s", want1, want2, s) 159 } 160 161 f := StringMapFunc(func() map[string]string { 162 return map[string]string{ 163 "c1": "val1", 164 "c2": "val3", 165 } 166 }) 167 if s := f.String(); s != want1 && s != want2 { 168 t.Errorf("want %s or %s, got %s", want1, want2, s) 169 } 170 }