|
| 1 | +// Copyright 2024 Prometheus Team |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package writev2 |
| 15 | + |
| 16 | +import ( |
| 17 | + "testing" |
| 18 | + |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | + |
| 21 | + "github.com/prometheus/prometheus/model/labels" |
| 22 | +) |
| 23 | + |
| 24 | +func TestSymbolsTable(t *testing.T) { |
| 25 | + s := NewSymbolTable() |
| 26 | + require.Equal(t, []string{""}, s.Symbols(), "required empty reference does not exist") |
| 27 | + require.Equal(t, uint32(0), s.Symbolize("")) |
| 28 | + require.Equal(t, []string{""}, s.Symbols()) |
| 29 | + |
| 30 | + require.Equal(t, uint32(1), s.Symbolize("abc")) |
| 31 | + require.Equal(t, []string{"", "abc"}, s.Symbols()) |
| 32 | + |
| 33 | + require.Equal(t, uint32(2), s.Symbolize("__name__")) |
| 34 | + require.Equal(t, []string{"", "abc", "__name__"}, s.Symbols()) |
| 35 | + |
| 36 | + require.Equal(t, uint32(3), s.Symbolize("foo")) |
| 37 | + require.Equal(t, []string{"", "abc", "__name__", "foo"}, s.Symbols()) |
| 38 | + |
| 39 | + s.Reset() |
| 40 | + require.Equal(t, []string{""}, s.Symbols(), "required empty reference does not exist") |
| 41 | + require.Equal(t, uint32(0), s.Symbolize("")) |
| 42 | + |
| 43 | + require.Equal(t, uint32(1), s.Symbolize("__name__")) |
| 44 | + require.Equal(t, []string{"", "__name__"}, s.Symbols()) |
| 45 | + |
| 46 | + require.Equal(t, uint32(2), s.Symbolize("abc")) |
| 47 | + require.Equal(t, []string{"", "__name__", "abc"}, s.Symbols()) |
| 48 | + |
| 49 | + ls := labels.FromStrings("__name__", "qwer", "zxcv", "1234") |
| 50 | + encoded := s.SymbolizeLabels(ls, nil) |
| 51 | + require.Equal(t, []uint32{1, 3, 4, 5}, encoded) |
| 52 | + decoded := DesymbolizeLabels(encoded, s.Symbols()) |
| 53 | + require.Equal(t, ls, decoded) |
| 54 | + |
| 55 | + // Different buf. |
| 56 | + ls = labels.FromStrings("__name__", "qwer", "zxcv2222", "1234") |
| 57 | + encoded = s.SymbolizeLabels(ls, []uint32{1, 3, 4, 5}) |
| 58 | + require.Equal(t, []uint32{1, 3, 6, 5}, encoded) |
| 59 | +} |
0 commit comments