Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: 🌲 Harry 🌊 John 🏔 <[email protected]>
  • Loading branch information
harry671003 committed Sep 9, 2024
1 parent 9c61312 commit a1f1b2d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tsdb/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,33 @@ func TestLabelValuesWithMatchers(t *testing.T) {
}
}

func TestBlockQuerierReturnsSortedLabelValues(t *testing.T) {
tmpdir := t.TempDir()
ctx := context.Background()

var seriesEntries []storage.Series
for i := 100; i > 0; i-- {
seriesEntries = append(seriesEntries, storage.NewListSeries(labels.FromStrings(
"__name__", fmt.Sprintf("value%d", i),
), []chunks.Sample{sample{100, 0, nil, nil}}))
}

blockDir := createBlock(t, tmpdir, seriesEntries)

// Check open err.
block, err := OpenBlock(nil, blockDir, nil)
require.NoError(t, err)
defer func() { require.NoError(t, block.Close()) }()

q, err := newBlockBaseQuerier(block, 0, 100)
require.NoError(t, err)
defer require.NoError(t, q.Close())

res, _, err := q.LabelValues(ctx, "__name__", nil)
require.NoError(t, err)
require.True(t, sort.StringsAreSorted(res))
}

// TestBlockSize ensures that the block size is calculated correctly.
func TestBlockSize(t *testing.T) {
tmpdir := t.TempDir()
Expand Down
30 changes: 30 additions & 0 deletions tsdb/head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,36 @@ func TestHead_LogRollback(t *testing.T) {
}
}

func TestHead_ReturnsSortedLabelValues(t *testing.T) {
h, _ := newTestHead(t, 1000, wlog.CompressionNone, false)
defer func() {
require.NoError(t, h.Close())
}()

h.initTime(0)

app := h.appender()
for i := 100; i > 0; i-- {
for j := 0; j < 10; j++ {
lset := labels.FromStrings(
"__name__", fmt.Sprintf("metric_%d", i),
"label", fmt.Sprintf("value_%d", j),
)
_, err := app.Append(0, lset, 2100, 1)
require.NoError(t, err)
}
}

q, err := NewBlockQuerier(h, 1500, 2500)
require.NoError(t, err)

res, _, err := q.LabelValues(context.Background(), "__name__", nil)
require.NoError(t, err)

require.True(t, sort.StringsAreSorted(res))
require.NoError(t, q.Close())
}

// TestWalRepair_DecodingError ensures that a repair is run for an error
// when decoding a record.
func TestWalRepair_DecodingError(t *testing.T) {
Expand Down

0 comments on commit a1f1b2d

Please sign in to comment.