|
1 | 1 | package decoder |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
5 | 6 | "sync" |
6 | 7 | "testing" |
@@ -148,7 +149,7 @@ func TestDecodeLabels(t *testing.T) { |
148 | 149 | } |
149 | 150 |
|
150 | 151 | for i, c := range cases { |
151 | | - s, err := NewSet() |
| 152 | + s, err := NewSet(0) |
152 | 153 | if err != nil { |
153 | 154 | t.Fatal(err) |
154 | 155 | } |
@@ -178,6 +179,118 @@ func TestDecodeLabels(t *testing.T) { |
178 | 179 | } |
179 | 180 | } |
180 | 181 |
|
| 182 | +func TestDecodeSkipLabels(t *testing.T) { |
| 183 | + cases := []struct { |
| 184 | + in []byte |
| 185 | + skipCacheIn string |
| 186 | + labels []config.Label |
| 187 | + out []string |
| 188 | + err bool |
| 189 | + }{ |
| 190 | + { |
| 191 | + in: append([]byte{0x8, 0x0, 0x0, 0x0}, zeroPaddedString("bananas", 32)...), |
| 192 | + skipCacheIn: "", |
| 193 | + labels: []config.Label{ |
| 194 | + { |
| 195 | + Name: "number", |
| 196 | + Size: 4, |
| 197 | + Decoders: []config.Decoder{ |
| 198 | + { |
| 199 | + Name: "uint", |
| 200 | + }, |
| 201 | + }, |
| 202 | + }, |
| 203 | + { |
| 204 | + Name: "fruit", |
| 205 | + Size: 32, |
| 206 | + Decoders: []config.Decoder{ |
| 207 | + { |
| 208 | + Name: "string", |
| 209 | + }, |
| 210 | + { |
| 211 | + Name: "regexp", |
| 212 | + Regexps: []string{ |
| 213 | + "^bananas$", |
| 214 | + "$is-banana-even-fruit$", |
| 215 | + }, |
| 216 | + }, |
| 217 | + }, |
| 218 | + }, |
| 219 | + }, |
| 220 | + out: []string{"8", "bananas"}, |
| 221 | + err: false, |
| 222 | + }, |
| 223 | + { |
| 224 | + in: append([]byte{0x8, 0x0, 0x0, 0x0}, zeroPaddedString("bananas", 32)...), |
| 225 | + skipCacheIn: string(zeroPaddedString("bananas", 32)), |
| 226 | + labels: []config.Label{ |
| 227 | + { |
| 228 | + Name: "number", |
| 229 | + Size: 4, |
| 230 | + Decoders: []config.Decoder{ |
| 231 | + { |
| 232 | + Name: "uint", |
| 233 | + }, |
| 234 | + }, |
| 235 | + }, |
| 236 | + { |
| 237 | + Name: "fruit", |
| 238 | + Size: 32, |
| 239 | + Decoders: []config.Decoder{ |
| 240 | + { |
| 241 | + Name: "string", |
| 242 | + }, |
| 243 | + { |
| 244 | + Name: "regexp", |
| 245 | + Regexps: []string{ |
| 246 | + "^tomato$", |
| 247 | + }, |
| 248 | + }, |
| 249 | + }, |
| 250 | + }, |
| 251 | + }, |
| 252 | + out: []string{"8", "bananas"}, |
| 253 | + err: true, // this label should be skipped, only tomatoes allowed |
| 254 | + }, |
| 255 | + } |
| 256 | + |
| 257 | + for i, c := range cases { |
| 258 | + s, err := NewSet(100) |
| 259 | + if err != nil { |
| 260 | + t.Fatal(err) |
| 261 | + } |
| 262 | + |
| 263 | + out, err := s.DecodeLabelsForMetrics(c.in, fmt.Sprintf("test:%d", i), c.labels) |
| 264 | + if c.err { |
| 265 | + if err == nil { |
| 266 | + t.Errorf("Expected error for input %#v and labels %#v, but did not receive it", c.in, c.labels) |
| 267 | + } |
| 268 | + |
| 269 | + if errors.Is(err, ErrSkipLabelSet) { |
| 270 | + if !s.skipCache.Contains(c.skipCacheIn) { |
| 271 | + t.Errorf("Expected skipCache to have input %#v", c.skipCacheIn) |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + continue |
| 276 | + } |
| 277 | + |
| 278 | + if err != nil { |
| 279 | + t.Errorf("Error decoding %#v with labels set to %#v: %s", c.in, c.labels, err) |
| 280 | + } |
| 281 | + |
| 282 | + if len(c.out) != len(out) { |
| 283 | + t.Errorf("Expected %d outputs (%v), received %d (%v)", len(c.out), c.out, len(out), out) |
| 284 | + } |
| 285 | + |
| 286 | + for i := 0; i < len(c.out) && i < len(out); i++ { |
| 287 | + if c.out[i] != out[i] { |
| 288 | + t.Errorf("Output label %d for input %#v is wrong: expected %s, but received %s", i, c.in, c.out[i], out[i]) |
| 289 | + } |
| 290 | + } |
| 291 | + } |
| 292 | +} |
| 293 | + |
181 | 294 | func TestDecoderSetConcurrency(t *testing.T) { |
182 | 295 | in := append([]byte{0x8, 0x0, 0x0, 0x0}, zeroPaddedString("bananas", 32)...) |
183 | 296 |
|
@@ -209,7 +322,7 @@ func TestDecoderSetConcurrency(t *testing.T) { |
209 | 322 | }, |
210 | 323 | } |
211 | 324 |
|
212 | | - s, err := NewSet() |
| 325 | + s, err := NewSet(0) |
213 | 326 | if err != nil { |
214 | 327 | t.Fatal(err) |
215 | 328 | } |
@@ -274,7 +387,7 @@ func TestDecoderSetCache(t *testing.T) { |
274 | 387 | }, |
275 | 388 | } |
276 | 389 |
|
277 | | - s, err := NewSet() |
| 390 | + s, err := NewSet(0) |
278 | 391 | if err != nil { |
279 | 392 | t.Fatal(err) |
280 | 393 | } |
@@ -345,7 +458,7 @@ func BenchmarkCache(b *testing.B) { |
345 | 458 | }, |
346 | 459 | } |
347 | 460 |
|
348 | | - s, err := NewSet() |
| 461 | + s, err := NewSet(0) |
349 | 462 | if err != nil { |
350 | 463 | b.Fatal(err) |
351 | 464 | } |
|
0 commit comments