github.com/phillinzzz/newBsc@v1.1.6/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(); count != 0 { 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(); count != -1 { 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(); count != -2 { 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(); count != 1 { 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(); count != 2 { 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(); count != 1 { 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(); count != 0 { 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); c.Count() != 47 { 75 t.Fatal(c) 76 } 77 }