github.com/msales/pkg/v3@v3.24.0/stats/stats_internal_test.go (about) 1 package stats 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestWithStats(t *testing.T) { 11 ctx := WithStats(context.Background(), Null) 12 13 got := ctx.Value(ctxKey) 14 15 assert.Equal(t, Null, got) 16 } 17 18 func TestWithStats_NilStats(t *testing.T) { 19 ctx := WithStats(context.Background(), nil) 20 21 got := ctx.Value(ctxKey) 22 23 assert.Equal(t, Null, got) 24 } 25 26 func TestFromContext(t *testing.T) { 27 ctx := context.WithValue(context.Background(), ctxKey, Null) 28 29 got, ok := FromContext(ctx) 30 31 assert.True(t, ok) 32 assert.Equal(t, Null, got) 33 } 34 35 func TestFromContext_NotSet(t *testing.T) { 36 ctx := context.Background() 37 38 got, ok := FromContext(ctx) 39 40 assert.False(t, ok) 41 assert.Nil(t, got) 42 } 43 44 func TestWithStatsFunc(t *testing.T) { 45 tests := []struct { 46 ctx context.Context 47 expect Stats 48 }{ 49 {context.Background(), Null}, 50 } 51 52 for _, tt := range tests { 53 withStats(tt.ctx, func(s Stats) error { 54 assert.Equal(t, tt.expect, s) 55 return nil 56 }) 57 } 58 } 59 60 func TestTaggedStats_Collapses(t *testing.T) { 61 o := NewTaggedStats(Null, "test", "test") 62 63 s := NewTaggedStats(o, "global", "foobar") 64 65 assert.Equal(t, Null, s.stats) 66 assert.Equal(t, []interface{}{"test", "test", "global", "foobar"}, s.tags) 67 } 68 69 func TestNullStats(t *testing.T) { 70 s := Null 71 72 assert.Nil(t, s.Inc("test", 1, 1.0)) 73 assert.Nil(t, s.Dec("test", 1, 1.0)) 74 assert.Nil(t, s.Gauge("test", 1.0, 1.0)) 75 assert.Nil(t, s.Timing("test", 0, 1.0)) 76 77 assert.NoError(t, s.Close()) 78 } 79 80 func TestMergeTags(t *testing.T) { 81 tags := []interface{}{ 82 "test1", "foo", 83 "test2", "bar", 84 } 85 86 tests := []struct { 87 tags []interface{} 88 expected []interface{} 89 }{ 90 { 91 nil, 92 []interface{}{"test1", "foo", "test2", "bar"}, 93 }, 94 { 95 []interface{}{}, 96 []interface{}{"test1", "foo", "test2", "bar"}, 97 }, 98 { 99 []interface{}{"foo", "bar", "baz", "bat"}, 100 []interface{}{"test1", "foo", "test2", "bar", "foo", "bar", "baz", "bat"}, 101 }, 102 } 103 104 for _, test := range tests { 105 assert.Equal(t, test.expected, mergeTags(tags, test.tags)) 106 } 107 } 108 109 func BenchmarkTaggedStats_MergeTags(b *testing.B) { 110 tags := []interface{}{ 111 "test1", "test", 112 "test2", "test", 113 "test3", "test", 114 "test4", "test", 115 "test5", "test", 116 } 117 addedTags := []interface{}{ 118 "k1", "v", 119 "k2", "v", 120 "k3", "v", 121 "k4", "v", 122 "k5", "v", 123 } 124 125 b.ReportAllocs() 126 127 for n := 0; n < b.N; n++ { 128 mergeTags(tags, addedTags) 129 } 130 } 131 132 func TestDeduplicateTags(t *testing.T) { 133 tests := []struct { 134 tags []interface{} 135 expected []interface{} 136 }{ 137 { 138 []interface{}{"test1", "foo", "test1", "bar"}, 139 []interface{}{"test1", "bar"}, 140 }, 141 { 142 []interface{}{"test1", "foo", "test2", "bar"}, 143 []interface{}{"test1", "foo", "test2", "bar"}, 144 }, 145 { 146 []interface{}{"test1", "foo", "test2", "bar", "test1", "baz"}, 147 []interface{}{"test1", "baz", "test2", "bar"}, 148 }, 149 } 150 151 for _, test := range tests { 152 assert.Equal(t, test.expected, deduplicateTags(test.tags)) 153 } 154 } 155 156 func BenchmarkTaggedStats_DeduplicateTags(b *testing.B) { 157 tags := []interface{}{ 158 "test1", "foo", 159 "test2", "bar", 160 "test1", "baz", 161 "test3", "test", 162 "test4", "test", 163 "test5", "test", 164 } 165 166 b.ReportAllocs() 167 168 for n := 0; n < b.N; n++ { 169 deduplicateTags(tags) 170 } 171 }