github.com/sunblockterminal/go-sunblocktediuma@v0.0.0-20210616083421-160a35ed7cfa/metrics/counter_test.go (about)

     1  package metrics
     2  
     3  import "testing"
     4  
     5  func BenchmarkCounter(b *testing.B) {
     6  	c := NewCounter()
     7  	b.ResetTimer()
     8  	for i := 0; i < b.N; i++ {
     9  		c.Inc(1)
    10  	}
    11  }
    12  
    13  func TestCounterClear(t *testing.T) {
    14  	c := NewCounter()
    15  	c.Inc(1)
    16  	c.Clear()
    17  	if count := c.Count(); 0 != count {
    18  		t.Errorf("c.Count(): 0 != %v\n", count)
    19  	}
    20  }
    21  
    22  func TestCounterDec1(t *testing.T) {
    23  	c := NewCounter()
    24  	c.Dec(1)
    25  	if count := c.Count(); -1 != count {
    26  		t.Errorf("c.Count(): -1 != %v\n", count)
    27  	}
    28  }
    29  
    30  func TestCounterDec2(t *testing.T) {
    31  	c := NewCounter()
    32  	c.Dec(2)
    33  	if count := c.Count(); -2 != count {
    34  		t.Errorf("c.Count(): -2 != %v\n", count)
    35  	}
    36  }
    37  
    38  func TestCounterInc1(t *testing.T) {
    39  	c := NewCounter()
    40  	c.Inc(1)
    41  	if count := c.Count(); 1 != count {
    42  		t.Errorf("c.Count(): 1 != %v\n", count)
    43  	}
    44  }
    45  
    46  func TestCounterInc2(t *testing.T) {
    47  	c := NewCounter()
    48  	c.Inc(2)
    49  	if count := c.Count(); 2 != count {
    50  		t.Errorf("c.Count(): 2 != %v\n", count)
    51  	}
    52  }
    53  
    54  func TestCounterSnapshot(t *testing.T) {
    55  	c := NewCounter()
    56  	c.Inc(1)
    57  	snapshot := c.Snapshot()
    58  	c.Inc(1)
    59  	if count := snapshot.Count(); 1 != count {
    60  		t.Errorf("c.Count(): 1 != %v\n", count)
    61  	}
    62  }
    63  
    64  func TestCounterZero(t *testing.T) {
    65  	c := NewCounter()
    66  	if count := c.Count(); 0 != count {
    67  		t.Errorf("c.Count(): 0 != %v\n", count)
    68  	}
    69  }
    70  
    71  func TestGetOrRegisterCounter(t *testing.T) {
    72  	r := NewRegistry()
    73  	NewRegisteredCounter("foo", r).Inc(47)
    74  	if c := GetOrRegisterCounter("foo", r); 47 != c.Count() {
    75  		t.Fatal(c)
    76  	}
    77  }