Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yuchen-db committed Dec 12, 2024
1 parent c752686 commit 69db7c8
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions pkg/store/storepb/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/stretchr/testify/require"

"github.com/efficientgo/core/testutil"

Expand Down Expand Up @@ -672,3 +675,98 @@ func TestSeriesRequestToPromQL(t *testing.T) {
})
}
}

func getMetricValue(m prometheus.Collector) float64 {
metric := &dto.Metric{}
if err := m.(prometheus.Metric).Write(metric); err != nil {
return 0
}
return metric.GetCounter().GetValue()
}

type CacheAction_Type int32

const (
CacheHit CacheAction_Type = 0
CacheMiss CacheAction_Type = 1
NoCache CacheAction_Type = 2
)

func TestMatcherConverter_MatchersToPromMatchers(t *testing.T) {
reg := prometheus.NewRegistry()
converter, err := NewMatcherConverter(2, reg)
require.NoError(t, err)

cases := []struct {
name string
inputMatchers []LabelMatcher
expectCacheAction CacheAction_Type
expectMatchers []*labels.Matcher
expectError bool
}{
{
name: "Single EQ matcher, no cache",
inputMatchers: []LabelMatcher{
{Name: "__name__", Type: LabelMatcher_EQ, Value: "up"},
},
// EQ matchers are not cached
expectCacheAction: NoCache,
},
{
name: "Single RE matcher, cache miss",
inputMatchers: []LabelMatcher{
{Name: "job", Type: LabelMatcher_RE, Value: "test"},
},
expectCacheAction: CacheMiss,
},
{
name: "Single RE matcher, cache hit",
inputMatchers: []LabelMatcher{
{Name: "job", Type: LabelMatcher_RE, Value: "test"},
},
expectCacheAction: CacheHit,
},
{
name: "Multiple matchers, mixed cache",
inputMatchers: []LabelMatcher{
{Name: "__name__", Type: LabelMatcher_EQ, Value: "up"},
{Name: "job", Type: LabelMatcher_RE, Value: "test"},
},
// Only RE is cached, so cache hit expected for RE matcher
expectCacheAction: CacheHit,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
cacheHitsBefore := getMetricValue(converter.metrics.cacheHitCount)
cacheMissesBefore := getMetricValue(converter.metrics.cacheMissCount)

promMatchers, err := converter.MatchersToPromMatchers(c.inputMatchers...)

if c.expectError {
require.Error(t, err)
return
}

require.NoError(t, err)
require.Len(t, promMatchers, len(c.inputMatchers))

for i, m := range promMatchers {
require.Equal(t, c.inputMatchers[i].Name, m.Name)
require.Equal(t, c.inputMatchers[i].Value, m.Value)
}

if c.expectCacheAction == CacheHit {
require.Equal(t, cacheHitsBefore+1, getMetricValue(converter.metrics.cacheHitCount))
require.Equal(t, cacheMissesBefore, getMetricValue(converter.metrics.cacheMissCount))
} else if c.expectCacheAction == CacheMiss {
require.Equal(t, cacheHitsBefore, getMetricValue(converter.metrics.cacheHitCount))
require.Equal(t, cacheMissesBefore+1, getMetricValue(converter.metrics.cacheMissCount))
} else {
require.Equal(t, cacheHitsBefore, getMetricValue(converter.metrics.cacheHitCount))
require.Equal(t, cacheMissesBefore, getMetricValue(converter.metrics.cacheMissCount))
}
})
}
}

0 comments on commit 69db7c8

Please sign in to comment.