Test that IndexReader.PostingsForMatchers is called

Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
This commit is contained in:
Oleg Zaytsev 2023-09-28 10:22:37 +02:00
parent 664c125d87
commit a8c31f279f
No known key found for this signature in database
GPG key ID: 7E9FE9FD48F512EF

View file

@ -2914,9 +2914,25 @@ func TestLabelsValuesWithMatchersOptimization(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
values, err := labelValuesWithMatchers(ctx, ir, c.labelName, c.matchers...)
cir := &indexReaderCountingPostingsForMatchersCalls{IndexReader: ir}
values, err := labelValuesWithMatchers(ctx, cir, c.labelName, c.matchers...)
require.NoError(t, err)
require.ElementsMatch(t, c.expectedResults, values)
require.Equal(t, 1, cir.postingsForMatchersCalls,
"expected PostingsForMatchers to be called once. "+
"labelValuesWithMatchers should call the IndexReader.PostingsForMatchers instead of calling the package function PostingsForMatchers "+
"because IndexReader may use the cached version of the PostingsForMatchers",
)
})
}
}
type indexReaderCountingPostingsForMatchersCalls struct {
IndexReader
postingsForMatchersCalls int
}
func (f *indexReaderCountingPostingsForMatchersCalls) PostingsForMatchers(ctx context.Context, concurrent bool, ms ...*labels.Matcher) (index.Postings, error) {
f.postingsForMatchersCalls++
return f.IndexReader.PostingsForMatchers(ctx, concurrent, ms...)
}