github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/metrics/counter_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package metrics
    19  
    20  import "testing"
    21  
    22  func BenchmarkCounter(b *testing.B) {
    23  	c := NewCounter()
    24  	b.ResetTimer()
    25  	for i := 0; i < b.N; i++ {
    26  		c.Inc(1)
    27  	}
    28  }
    29  
    30  func TestCounterClear(t *testing.T) {
    31  	c := NewCounter()
    32  	c.Inc(1)
    33  	c.Clear()
    34  	if count := c.Count(); 0 != count {
    35  		t.Errorf("c.Count(): 0 != %v\n", count)
    36  	}
    37  }
    38  
    39  func TestCounterDec1(t *testing.T) {
    40  	c := NewCounter()
    41  	c.Dec(1)
    42  	if count := c.Count(); -1 != count {
    43  		t.Errorf("c.Count(): -1 != %v\n", count)
    44  	}
    45  }
    46  
    47  func TestCounterDec2(t *testing.T) {
    48  	c := NewCounter()
    49  	c.Dec(2)
    50  	if count := c.Count(); -2 != count {
    51  		t.Errorf("c.Count(): -2 != %v\n", count)
    52  	}
    53  }
    54  
    55  func TestCounterInc1(t *testing.T) {
    56  	c := NewCounter()
    57  	c.Inc(1)
    58  	if count := c.Count(); 1 != count {
    59  		t.Errorf("c.Count(): 1 != %v\n", count)
    60  	}
    61  }
    62  
    63  func TestCounterInc2(t *testing.T) {
    64  	c := NewCounter()
    65  	c.Inc(2)
    66  	if count := c.Count(); 2 != count {
    67  		t.Errorf("c.Count(): 2 != %v\n", count)
    68  	}
    69  }
    70  
    71  func TestCounterSnapshot(t *testing.T) {
    72  	c := NewCounter()
    73  	c.Inc(1)
    74  	snapshot := c.Snapshot()
    75  	c.Inc(1)
    76  	if count := snapshot.Count(); 1 != count {
    77  		t.Errorf("c.Count(): 1 != %v\n", count)
    78  	}
    79  }
    80  
    81  func TestCounterZero(t *testing.T) {
    82  	c := NewCounter()
    83  	if count := c.Count(); 0 != count {
    84  		t.Errorf("c.Count(): 0 != %v\n", count)
    85  	}
    86  }
    87  
    88  func TestGetOrRegisterCounter(t *testing.T) {
    89  	r := NewRegistry()
    90  	NewRegisteredCounter("foo", r).Inc(47)
    91  	if c := GetOrRegisterCounter("foo", r); 47 != c.Count() {
    92  		t.Fatal(c)
    93  	}
    94  }