vitess.io/vitess@v0.16.2/go/stats/counter_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 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestCounter(t *testing.T) { 27 var gotname string 28 var gotv *Counter 29 clear() 30 Register(func(name string, v expvar.Var) { 31 gotname = name 32 gotv = v.(*Counter) 33 }) 34 v := NewCounter("Int", "help") 35 if gotname != "Int" { 36 t.Errorf("want Int, got %s", gotname) 37 } 38 if gotv != v { 39 t.Errorf("want %#v, got %#v", v, gotv) 40 } 41 v.Add(1) 42 if v.Get() != 1 { 43 t.Errorf("want 1, got %v", v.Get()) 44 } 45 if v.String() != "1" { 46 t.Errorf("want 1, got %v", v.Get()) 47 } 48 v.Reset() 49 if v.Get() != 0 { 50 t.Errorf("want 0, got %v", v.Get()) 51 } 52 } 53 54 func TestGaugeFunc(t *testing.T) { 55 var gotname string 56 var gotv *GaugeFunc 57 clear() 58 Register(func(name string, v expvar.Var) { 59 gotname = name 60 gotv = v.(*GaugeFunc) 61 }) 62 63 v := NewGaugeFunc("name", "help", func() int64 { 64 return 1 65 }) 66 if gotname != "name" { 67 t.Errorf("want name, got %s", gotname) 68 } 69 if gotv != v { 70 t.Errorf("want %#v, got %#v", v, gotv) 71 } 72 if v.String() != "1" { 73 t.Errorf("want 1, got %v", v.String()) 74 } 75 } 76 77 func TestGaugeFloat64(t *testing.T) { 78 var gotname string 79 var gotv *GaugeFloat64 80 clear() 81 Register(func(name string, v expvar.Var) { 82 gotname = name 83 gotv = v.(*GaugeFloat64) 84 }) 85 v := NewGaugeFloat64("f", "help") 86 assert.Equal(t, "f", gotname) 87 assert.Equal(t, v, gotv) 88 v.Set(3.14) 89 assert.Equal(t, 3.14, v.Get()) 90 assert.Equal(t, "3.14", v.String()) 91 v.Reset() 92 assert.Equal(t, float64(0), v.Get()) 93 }