github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/metrics/counter_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:39</date>
    10  //</624450098037788672>
    11  
    12  package metrics
    13  
    14  import "testing"
    15  
    16  func BenchmarkCounter(b *testing.B) {
    17  	c := NewCounter()
    18  	b.ResetTimer()
    19  	for i := 0; i < b.N; i++ {
    20  		c.Inc(1)
    21  	}
    22  }
    23  
    24  func TestCounterClear(t *testing.T) {
    25  	c := NewCounter()
    26  	c.Inc(1)
    27  	c.Clear()
    28  	if count := c.Count(); 0 != count {
    29  		t.Errorf("c.Count(): 0 != %v\n", count)
    30  	}
    31  }
    32  
    33  func TestCounterDec1(t *testing.T) {
    34  	c := NewCounter()
    35  	c.Dec(1)
    36  	if count := c.Count(); -1 != count {
    37  		t.Errorf("c.Count(): -1 != %v\n", count)
    38  	}
    39  }
    40  
    41  func TestCounterDec2(t *testing.T) {
    42  	c := NewCounter()
    43  	c.Dec(2)
    44  	if count := c.Count(); -2 != count {
    45  		t.Errorf("c.Count(): -2 != %v\n", count)
    46  	}
    47  }
    48  
    49  func TestCounterInc1(t *testing.T) {
    50  	c := NewCounter()
    51  	c.Inc(1)
    52  	if count := c.Count(); 1 != count {
    53  		t.Errorf("c.Count(): 1 != %v\n", count)
    54  	}
    55  }
    56  
    57  func TestCounterInc2(t *testing.T) {
    58  	c := NewCounter()
    59  	c.Inc(2)
    60  	if count := c.Count(); 2 != count {
    61  		t.Errorf("c.Count(): 2 != %v\n", count)
    62  	}
    63  }
    64  
    65  func TestCounterSnapshot(t *testing.T) {
    66  	c := NewCounter()
    67  	c.Inc(1)
    68  	snapshot := c.Snapshot()
    69  	c.Inc(1)
    70  	if count := snapshot.Count(); 1 != count {
    71  		t.Errorf("c.Count(): 1 != %v\n", count)
    72  	}
    73  }
    74  
    75  func TestCounterZero(t *testing.T) {
    76  	c := NewCounter()
    77  	if count := c.Count(); 0 != count {
    78  		t.Errorf("c.Count(): 0 != %v\n", count)
    79  	}
    80  }
    81  
    82  func TestGetOrRegisterCounter(t *testing.T) {
    83  	r := NewRegistry()
    84  	NewRegisteredCounter("foo", r).Inc(47)
    85  	if c := GetOrRegisterCounter("foo", r); 47 != c.Count() {
    86  		t.Fatal(c)
    87  	}
    88  }
    89