github.com/blend/go-sdk@v1.20220411.3/stats/multicollector_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package stats 9 10 import ( 11 "fmt" 12 "testing" 13 "time" 14 15 "github.com/blend/go-sdk/assert" 16 ) 17 18 func TestCount(t *testing.T) { 19 assert := assert.New(t) 20 21 assertTags := func(actualTags []string) { 22 assert.Len(actualTags, 1) 23 assert.Equal("k1:v1", actualTags[0]) 24 } 25 26 c1 := NewMockCollector(32) 27 c2 := NewMockCollector(32) 28 29 mc := MultiCollector{c1, c2} 30 31 err := mc.Count("event", 1, "k1:v1") 32 assert.Nil(err) 33 metric1 := <-c1.Metrics 34 metric2 := <-c2.Metrics 35 assert.Equal("event", metric1.Name) 36 assert.Equal(1, metric1.Count) 37 assertTags(metric1.Tags) 38 assert.Zero(metric1.Gauge) 39 assert.Zero(metric1.Histogram) 40 assert.Zero(metric1.TimeInMilliseconds) 41 assert.Equal(metric1, metric2) 42 43 mc = MultiCollector{c1} 44 c1.Errors <- fmt.Errorf("error") 45 err = mc.Count("event", 1, "k1:v1") 46 assert.NotNil(err) 47 assert.Equal("error", err.Error()) 48 metric1 = <-c1.Metrics 49 assert.Zero(metric1.Gauge) 50 assert.Zero(metric1.Histogram) 51 assert.Zero(metric1.TimeInMilliseconds) 52 } 53 54 func TestIncrement(t *testing.T) { 55 assert := assert.New(t) 56 57 c1 := NewMockCollector(32) 58 c2 := NewMockCollector(32) 59 60 var err error 61 mc := MultiCollector{c1, c2} 62 err = mc.Increment("event", "k1:v1") 63 assert.Nil(err) 64 65 metric1 := <-c1.Metrics 66 metric2 := <-c2.Metrics 67 assert.Equal("event", metric1.Name) 68 assert.Equal(1, metric1.Count) 69 assert.Zero(metric1.Gauge) 70 assert.Zero(metric1.Histogram) 71 assert.Zero(metric1.TimeInMilliseconds) 72 assert.Equal(metric1, metric2) 73 74 mc = MultiCollector{c1} 75 76 c1.Errors <- fmt.Errorf("error") 77 err = mc.Increment("event", "k1:v1") 78 assert.NotNil(err) 79 assert.Equal("error", err.Error()) 80 metric1 = <-c1.Metrics 81 assert.Zero(metric1.Gauge) 82 assert.Zero(metric1.Histogram) 83 assert.Zero(metric1.TimeInMilliseconds) 84 } 85 86 func TestGauge(t *testing.T) { 87 assert := assert.New(t) 88 c1 := NewMockCollector(32) 89 c2 := NewMockCollector(32) 90 91 var err error 92 mc := MultiCollector{c1, c2} 93 err = mc.Gauge("event", .01) 94 assert.Nil(err) 95 96 metric1 := <-c1.Metrics 97 metric2 := <-c2.Metrics 98 assert.Equal("event", metric1.Name) 99 assert.Equal(.01, metric1.Gauge) 100 assert.Zero(metric1.Count) 101 assert.Zero(metric1.Histogram) 102 assert.Zero(metric1.TimeInMilliseconds) 103 assert.Equal(metric1, metric2) 104 105 mc = MultiCollector{c1} 106 107 c1.Errors <- fmt.Errorf("error") 108 err = mc.Gauge("event", .01) 109 assert.NotNil(err) 110 assert.Equal("error", err.Error()) 111 metric1 = <-c1.Metrics 112 assert.Zero(metric1.Count) 113 assert.Zero(metric1.Histogram) 114 assert.Zero(metric1.TimeInMilliseconds) 115 } 116 117 func TestHistogram(t *testing.T) { 118 assert := assert.New(t) 119 c1 := NewMockCollector(32) 120 c2 := NewMockCollector(32) 121 122 var err error 123 mc := MultiCollector{c1, c2} 124 err = mc.Histogram("event", .01) 125 assert.Nil(err) 126 127 metric1 := <-c1.Metrics 128 metric2 := <-c2.Metrics 129 assert.Equal("event", metric1.Name) 130 assert.Equal(.01, metric1.Histogram) 131 assert.Zero(metric1.Count) 132 assert.Zero(metric1.Gauge) 133 assert.Zero(metric1.TimeInMilliseconds) 134 assert.Equal(metric1, metric2) 135 136 mc = MultiCollector{c1, c2} 137 138 c1.Errors <- fmt.Errorf("error") 139 err = mc.Histogram("event", .01) 140 assert.NotNil(err) 141 assert.Equal("error", err.Error()) 142 metric1 = <-c1.Metrics 143 assert.Zero(metric1.Count) 144 assert.Zero(metric1.Gauge) 145 assert.Zero(metric1.TimeInMilliseconds) 146 } 147 148 func TestTimeInMilliseconds(t *testing.T) { 149 assert := assert.New(t) 150 151 assertTags := func(actualTags []string) { 152 assert.Len(actualTags, 1) 153 assert.Equal("k1:v1", actualTags[0]) 154 } 155 156 c1 := NewMockCollector(32) 157 c2 := NewMockCollector(32) 158 159 var err error 160 mc := MultiCollector{c1, c2} 161 err = mc.TimeInMilliseconds("event", time.Second, "k1:v1") 162 assert.Nil(err) 163 metric1 := <-c1.Metrics 164 metric2 := <-c2.Metrics 165 assert.Equal("event", metric1.Name) 166 assert.Equal(1000, metric1.TimeInMilliseconds) 167 assertTags(metric1.Tags) 168 assert.Equal(metric1, metric2) 169 170 mc = MultiCollector{c1, c2} 171 c1.Errors <- fmt.Errorf("error") 172 err = mc.TimeInMilliseconds("event", time.Second, "k1:v1") 173 assert.NotNil(err) 174 assert.Equal("error", err.Error()) 175 metric1 = <-c1.Metrics 176 assert.Zero(metric1.Gauge) 177 assert.Zero(metric1.Histogram) 178 assert.Zero(metric1.Count) 179 } 180 181 func TestFlush(t *testing.T) { 182 assert := assert.New(t) 183 184 c1 := NewMockCollector(32) 185 c2 := NewMockCollector(32) 186 187 var err error 188 mc := MultiCollector{c1, c2} 189 190 err = mc.Flush() 191 assert.Nil(err) 192 193 expectedError := fmt.Errorf("err") 194 c2.FlushErrors <- expectedError 195 err = mc.Flush() 196 assert.Equal(expectedError.Error(), err.Error()) 197 } 198 199 func TestClose(t *testing.T) { 200 assert := assert.New(t) 201 202 c1 := NewMockCollector(32) 203 c2 := NewMockCollector(32) 204 205 var err error 206 mc := MultiCollector{c1, c2} 207 208 err = mc.Close() 209 assert.Nil(err) 210 211 expectedError := fmt.Errorf("err") 212 c2.CloseErrors <- expectedError 213 err = mc.Close() 214 assert.Equal(expectedError.Error(), err.Error()) 215 }