github.com/XiaoMi/Gaea@v1.2.5/stats/duration_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  	"time"
    23  )
    24  
    25  func TestCounterDuration(t *testing.T) {
    26  	var gotname string
    27  	var gotv *CounterDuration
    28  	clear()
    29  	Register(func(name string, v expvar.Var) {
    30  		gotname = name
    31  		gotv = v.(*CounterDuration)
    32  	})
    33  	v := NewCounterDuration("CounterDuration", "help")
    34  	if gotname != "CounterDuration" {
    35  		t.Errorf("want CounterDuration, got %s", gotname)
    36  	}
    37  	if gotv != v {
    38  		t.Errorf("want %#v, got %#v", v, gotv)
    39  	}
    40  	if v.Get() != 0 {
    41  		t.Errorf("want 0, got %v", v.Get())
    42  	}
    43  	v.Add(time.Duration(1))
    44  	if v.Get() != 1 {
    45  		t.Errorf("want 1, got %v", v.Get())
    46  	}
    47  	if v.String() != "1" {
    48  		t.Errorf("want 1, got %v", v.Get())
    49  	}
    50  }
    51  
    52  func TestCounterDurationFunc(t *testing.T) {
    53  	var gotname string
    54  	var gotv *CounterDurationFunc
    55  	clear()
    56  	Register(func(name string, v expvar.Var) {
    57  		gotname = name
    58  		gotv = v.(*CounterDurationFunc)
    59  	})
    60  
    61  	v := NewCounterDurationFunc("CounterDurationFunc", "help", func() time.Duration {
    62  		return time.Duration(1)
    63  	})
    64  	if gotname != "CounterDurationFunc" {
    65  		t.Errorf("want CounterDurationFunc, 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  }
    74  
    75  func TestGaugeDuration(t *testing.T) {
    76  	var gotname string
    77  	var gotv *GaugeDuration
    78  	clear()
    79  	Register(func(name string, v expvar.Var) {
    80  		gotname = name
    81  		gotv = v.(*GaugeDuration)
    82  	})
    83  	v := NewGaugeDuration("GaugeDuration", "help")
    84  	if gotname != "GaugeDuration" {
    85  		t.Errorf("want GaugeDuration, got %s", gotname)
    86  	}
    87  	if gotv != v {
    88  		t.Errorf("want %#v, got %#v", v, gotv)
    89  	}
    90  	v.Set(time.Duration(5))
    91  	if v.Get() != 5 {
    92  		t.Errorf("want 5, got %v", v.Get())
    93  	}
    94  	v.Add(time.Duration(1))
    95  	if v.Get() != 6 {
    96  		t.Errorf("want 6, got %v", v.Get())
    97  	}
    98  	if v.String() != "6" {
    99  		t.Errorf("want 6, got %v", v.Get())
   100  	}
   101  }
   102  
   103  func TestGaugeDurationFunc(t *testing.T) {
   104  	var gotname string
   105  	var gotv *GaugeDurationFunc
   106  	clear()
   107  	Register(func(name string, v expvar.Var) {
   108  		gotname = name
   109  		gotv = v.(*GaugeDurationFunc)
   110  	})
   111  
   112  	v := NewGaugeDurationFunc("GaugeDurationFunc", "help", func() time.Duration {
   113  		return time.Duration(1)
   114  	})
   115  
   116  	if gotname != "GaugeDurationFunc" {
   117  		t.Errorf("want GaugeDurationFunc, got %s", gotname)
   118  	}
   119  	if gotv != v {
   120  		t.Errorf("want %#v, got %#v", v, gotv)
   121  	}
   122  	if v.String() != "1" {
   123  		t.Errorf("want 1, got %v", v.String())
   124  	}
   125  }