github.com/msales/pkg/v3@v3.24.0/stats/stats_test.go (about) 1 package stats_test 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/msales/pkg/v3/stats" 9 "github.com/stretchr/testify/mock" 10 ) 11 12 func TestInc(t *testing.T) { 13 m := new(MockStats) 14 m.On("Inc", "test", int64(1), float32(1.0), []interface{}(nil)).Return(nil) 15 ctx := stats.WithStats(context.Background(), m) 16 17 stats.Inc(ctx, "test", 1, 1.0) 18 19 m.AssertExpectations(t) 20 } 21 22 func TestDec(t *testing.T) { 23 m := new(MockStats) 24 m.On("Dec", "test", int64(1), float32(1.0), []interface{}(nil)).Return(nil) 25 ctx := stats.WithStats(context.Background(), m) 26 27 stats.Dec(ctx, "test", 1, 1.0) 28 29 m.AssertExpectations(t) 30 } 31 32 func TestGauge(t *testing.T) { 33 m := new(MockStats) 34 m.On("Gauge", "test", float64(1), float32(1.0), []interface{}(nil)).Return(nil) 35 ctx := stats.WithStats(context.Background(), m) 36 37 stats.Gauge(ctx, "test", 1, 1.0) 38 39 m.AssertExpectations(t) 40 } 41 42 func TestTiming(t *testing.T) { 43 m := new(MockStats) 44 m.On("Timing", "test", time.Second, float32(1.0), []interface{}(nil)).Return(nil) 45 ctx := stats.WithStats(context.Background(), m) 46 47 stats.Timing(ctx, "test", time.Second, 1.0) 48 49 m.AssertExpectations(t) 50 } 51 52 func TestClose(t *testing.T) { 53 m := new(MockStats) 54 m.On("Close").Return(nil) 55 ctx := stats.WithStats(context.Background(), m) 56 57 stats.Close(ctx) 58 59 m.AssertExpectations(t) 60 } 61 62 func TestTaggedStats_Inc(t *testing.T) { 63 m := new(MockStats) 64 m.On("Inc", "test", int64(1), float32(1), []interface{}{"foo", "bar", "global", "foobar"}).Return(nil) 65 s := stats.NewTaggedStats(m, "global", "foobar") 66 67 s.Inc("test", 1, 1.0, "foo", "bar") 68 69 m.AssertExpectations(t) 70 } 71 72 func TestTaggedStats_Dec(t *testing.T) { 73 m := new(MockStats) 74 m.On("Dec", "test", int64(1), float32(1), []interface{}{"foo", "bar", "global", "foobar"}).Return(nil) 75 s := stats.NewTaggedStats(m, "global", "foobar") 76 77 s.Dec("test", 1, 1.0, "foo", "bar") 78 79 m.AssertExpectations(t) 80 } 81 82 func TestTaggedStats_Gauge(t *testing.T) { 83 m := new(MockStats) 84 m.On("Gauge", "test", float64(1), float32(1), []interface{}{"foo", "bar", "global", "foobar"}).Return(nil) 85 s := stats.NewTaggedStats(m, "global", "foobar") 86 87 s.Gauge("test", 1.0, 1.0, "foo", "bar") 88 89 m.AssertExpectations(t) 90 } 91 92 func TestTaggedStats_Timing(t *testing.T) { 93 m := new(MockStats) 94 m.On("Timing", "test", time.Millisecond, float32(1), []interface{}{"foo", "bar", "global", "foobar"}).Return(nil) 95 s := stats.NewTaggedStats(m, "global", "foobar") 96 97 s.Timing("test", time.Millisecond, 1.0, "foo", "bar") 98 99 m.AssertExpectations(t) 100 } 101 102 func TestTaggedStats_Close(t *testing.T) { 103 m := new(MockStats) 104 m.On("Close").Return(nil) 105 s := stats.NewTaggedStats(m, "global", "foobar") 106 107 s.Close() 108 109 m.AssertExpectations(t) 110 } 111 112 type MockStats struct { 113 mock.Mock 114 } 115 116 func (m *MockStats) Inc(name string, value int64, rate float32, tags ...interface{}) error { 117 args := m.Called(name, value, rate, tags) 118 return args.Error(0) 119 } 120 121 func (m *MockStats) Dec(name string, value int64, rate float32, tags ...interface{}) error { 122 args := m.Called(name, value, rate, tags) 123 return args.Error(0) 124 } 125 126 func (m *MockStats) Gauge(name string, value float64, rate float32, tags ...interface{}) error { 127 args := m.Called(name, value, rate, tags) 128 return args.Error(0) 129 } 130 131 func (m *MockStats) Timing(name string, value time.Duration, rate float32, tags ...interface{}) error { 132 args := m.Called(name, value, rate, tags) 133 return args.Error(0) 134 } 135 136 func (m *MockStats) Close() error { 137 args := m.Called() 138 return args.Error(0) 139 }