Skip to content

Commit 6491ab9

Browse files
committed
Add unit test
Signed-off-by: Filip Petkovski <[email protected]>
1 parent 17fbb9b commit 6491ab9

File tree

2 files changed

+101
-16
lines changed

2 files changed

+101
-16
lines changed

pkg/query/iter_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) The Thanos Authors.
2+
// Licensed under the Apache License 2.0.
3+
4+
package query
5+
6+
import (
7+
"testing"
8+
9+
"github.com/efficientgo/core/testutil"
10+
"github.com/prometheus/prometheus/model/histogram"
11+
"github.com/prometheus/prometheus/tsdb/tsdbutil"
12+
)
13+
14+
func TestNativeHistogramDedup(t *testing.T) {
15+
cases := []struct {
16+
samples []sample
17+
tcase string
18+
}{
19+
{
20+
tcase: "unknown counter reset chunk",
21+
samples: []sample{
22+
{t: 10000, h: makeHistWithHint(1, histogram.UnknownCounterReset)},
23+
{t: 20000, h: makeHistWithHint(2, histogram.UnknownCounterReset)},
24+
},
25+
},
26+
{
27+
tcase: "not counter reset chunk",
28+
samples: []sample{
29+
{t: 10000, h: makeHistWithHint(1, histogram.NotCounterReset)},
30+
{t: 20000, h: makeHistWithHint(2, histogram.NotCounterReset)},
31+
},
32+
},
33+
{
34+
tcase: "counter reset chunk",
35+
samples: []sample{
36+
{t: 10000, h: makeHistWithHint(1, histogram.CounterReset)},
37+
{t: 20000, h: makeHistWithHint(2, histogram.NotCounterReset)},
38+
},
39+
},
40+
}
41+
42+
for _, c := range cases {
43+
t.Run(c.tcase, func(t *testing.T) {
44+
// When the first sample is read, the counter reset hint for the second sample
45+
// does not need to be reset.
46+
t.Run("read_first_sample", func(t *testing.T) {
47+
it := newHistogramResetDetector(newMockedSeriesIterator(c.samples))
48+
it.Next()
49+
_, h := it.AtHistogram()
50+
testutil.Equals(t, c.samples[0].h.CounterResetHint, h.CounterResetHint)
51+
52+
it.Next()
53+
_, h = it.AtHistogram()
54+
testutil.Equals(t, c.samples[1].h.CounterResetHint, h.CounterResetHint)
55+
})
56+
57+
// When the first sample is not read, the counter reset hint for the second
58+
// sample should be reset.
59+
t.Run("skip_first_sample", func(t *testing.T) {
60+
it := newHistogramResetDetector(newMockedSeriesIterator(c.samples))
61+
it.Next()
62+
it.Next()
63+
_, h := it.AtHistogram()
64+
testutil.Equals(t, h.CounterResetHint, histogram.UnknownCounterReset)
65+
})
66+
})
67+
}
68+
}
69+
70+
func makeHistWithHint(i int, hint histogram.CounterResetHint) *histogram.Histogram {
71+
h := tsdbutil.GenerateTestHistogram(i)
72+
h.CounterResetHint = hint
73+
return h
74+
}

pkg/query/querier_test.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,11 +1020,13 @@ func (s *mockedSeriesIterator) Seek(t int64) chunkenc.ValueType {
10201020
return s.samples[n].t >= t
10211021
})
10221022

1023-
if s.cur < len(s.samples) {
1024-
return chunkenc.ValFloat
1023+
if s.cur >= len(s.samples) {
1024+
return chunkenc.ValNone
10251025
}
1026-
1027-
return chunkenc.ValNone
1026+
if s.samples[s.cur].h != nil {
1027+
return chunkenc.ValHistogram
1028+
}
1029+
return chunkenc.ValFloat
10281030
}
10291031

10301032
func (s *mockedSeriesIterator) At() (t int64, v float64) {
@@ -1034,7 +1036,8 @@ func (s *mockedSeriesIterator) At() (t int64, v float64) {
10341036

10351037
// TODO(rabenhorst): Needs to be implemented for native histogram support.
10361038
func (s *mockedSeriesIterator) AtHistogram(*histogram.Histogram) (int64, *histogram.Histogram) {
1037-
panic("not implemented")
1039+
sample := s.samples[s.cur]
1040+
return sample.t, sample.h
10381041
}
10391042

10401043
func (s *mockedSeriesIterator) AtFloatHistogram(*histogram.FloatHistogram) (int64, *histogram.FloatHistogram) {
@@ -1047,11 +1050,13 @@ func (s *mockedSeriesIterator) AtT() int64 {
10471050

10481051
func (s *mockedSeriesIterator) Next() chunkenc.ValueType {
10491052
s.cur++
1050-
if s.cur < len(s.samples) {
1051-
return chunkenc.ValFloat
1053+
if s.cur >= len(s.samples) {
1054+
return chunkenc.ValNone
10521055
}
1053-
1054-
return chunkenc.ValNone
1056+
if s.samples[s.cur].h != nil {
1057+
return chunkenc.ValHistogram
1058+
}
1059+
return chunkenc.ValFloat
10551060
}
10561061

10571062
func (s *mockedSeriesIterator) Err() error { return nil }
@@ -1205,14 +1210,20 @@ func TestQuerierWithDedupUnderstoodByPromQL_Rate(t *testing.T) {
12051210
const hackyStaleMarker = float64(-99999999)
12061211

12071212
func expandSeries(t testing.TB, it chunkenc.Iterator) (res []sample) {
1208-
for it.Next() != chunkenc.ValNone {
1209-
t, v := it.At()
1210-
// Nan != Nan, so substitute for another value.
1211-
// This is required for testutil.Equals to work deterministically.
1212-
if math.IsNaN(v) {
1213-
v = hackyStaleMarker
1213+
for valType := it.Next(); valType != chunkenc.ValNone; valType = it.Next() {
1214+
switch valType {
1215+
case chunkenc.ValFloat:
1216+
t, v := it.At()
1217+
// Nan != Nan, so substitute for another value.
1218+
// This is required for testutil.Equals to work deterministically.
1219+
if math.IsNaN(v) {
1220+
v = hackyStaleMarker
1221+
}
1222+
res = append(res, sample{t: t, v: v})
1223+
case chunkenc.ValHistogram:
1224+
t, h := it.AtHistogram(nil)
1225+
res = append(res, sample{t: t, h: h})
12141226
}
1215-
res = append(res, sample{t: t, v: v})
12161227
}
12171228
testutil.Ok(t, it.Err())
12181229
return res

0 commit comments

Comments
 (0)