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