github.com/XiaoMi/Gaea@v1.2.5/stats/counter_test.go (about)

     1  /*
     2  Copyright 2018 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  
    24  func TestCounter(t *testing.T) {
    25  	var gotname string
    26  	var gotv *Counter
    27  	clear()
    28  	Register(func(name string, v expvar.Var) {
    29  		gotname = name
    30  		gotv = v.(*Counter)
    31  	})
    32  	v := NewCounter("Int", "help")
    33  	if gotname != "Int" {
    34  		t.Errorf("want Int, got %s", gotname)
    35  	}
    36  	if gotv != v {
    37  		t.Errorf("want %#v, got %#v", v, gotv)
    38  	}
    39  	v.Add(1)
    40  	if v.Get() != 1 {
    41  		t.Errorf("want 1, got %v", v.Get())
    42  	}
    43  	if v.String() != "1" {
    44  		t.Errorf("want 1, got %v", v.Get())
    45  	}
    46  	v.Reset()
    47  	if v.Get() != 0 {
    48  		t.Errorf("want 0, got %v", v.Get())
    49  	}
    50  }
    51  
    52  func TestGaugeFunc(t *testing.T) {
    53  	var gotname string
    54  	var gotv *GaugeFunc
    55  	clear()
    56  	Register(func(name string, v expvar.Var) {
    57  		gotname = name
    58  		gotv = v.(*GaugeFunc)
    59  	})
    60  
    61  	v := NewGaugeFunc("name", "help", func() int64 {
    62  		return 1
    63  	})
    64  	if gotname != "name" {
    65  		t.Errorf("want name, got %s", gotname)
    66  	}
    67  	if gotv != v {
    68  		t.Errorf("want %#v, got %#v", v, gotv)
    69  	}
    70  	if v.String() != "1" {
    71  		t.Errorf("want 1, got %v", v.String())
    72  	}
    73  }