1
1
package chunk
2
2
3
3
import (
4
+ "context"
4
5
"testing"
5
6
6
7
"github.com/prometheus/common/model"
7
8
"github.com/prometheus/prometheus/storage/metric"
8
9
"github.com/stretchr/testify/require"
10
+ "github.com/weaveworks/common/user"
9
11
)
10
12
11
13
func TestLazySeriesIterator_Metric (t * testing.T ) {
12
14
store := newTestChunkStore (t , StoreConfig {})
13
15
now := model .Now ()
14
16
sampleMetric := model.Metric {model .MetricNameLabel : "foo" }
15
- iterator , err := NewLazySeriesIterator (store , sampleMetric , now , now )
17
+ iterator , err := NewLazySeriesIterator (store , sampleMetric , now , now , userID )
16
18
require .NoError (t , err )
17
19
for _ , c := range []struct {
18
20
iterator * LazySeriesIterator
@@ -29,45 +31,91 @@ func TestLazySeriesIterator_Metric(t *testing.T) {
29
31
}
30
32
31
33
func TestLazySeriesIterator_ValueAtOrBeforeTime (t * testing.T ) {
32
- store := newTestChunkStore (t , StoreConfig {})
33
34
now := model .Now ()
35
+ ctx := user .InjectOrgID (context .Background (), userID )
36
+
34
37
sampleMetric := model.Metric {model .MetricNameLabel : "foo" }
35
- iterator , err := NewLazySeriesIterator (store , sampleMetric , now , now )
38
+ dummyChunk := dummyChunkFor (sampleMetric )
39
+ dummySamples , err := dummyChunk .Samples ()
36
40
require .NoError (t , err )
37
- for _ , c := range [] struct {
38
- iterator * LazySeriesIterator
39
- timestamp model. Time
40
- expectedSample model. SamplePair
41
+
42
+ schemas := [] struct {
43
+ name string
44
+ fn func ( cfg SchemaConfig ) Schema
41
45
}{
42
- {
43
- iterator : iterator ,
44
- timestamp : now ,
45
- expectedSample : model .ZeroSamplePair ,
46
- },
47
- } {
48
- sample := c .iterator .ValueAtOrBeforeTime (c .timestamp )
49
- require .Equal (t , c .expectedSample , sample )
46
+ {"v8 schema" , v8Schema },
47
+ }
48
+
49
+ for _ , schema := range schemas {
50
+ // Create store with the dummy chunk.
51
+ store := newTestChunkStore (t , StoreConfig {schemaFactory : schema .fn })
52
+ store .Put (ctx , []Chunk {dummyChunk })
53
+
54
+ // Create the lazy series iterator
55
+ iterator , err := NewLazySeriesIterator (store , sampleMetric , now , now , userID )
56
+ require .NoError (t , err )
57
+ for _ , tc := range []struct {
58
+ iterator * LazySeriesIterator
59
+ timestamp model.Time
60
+ expectedSample model.SamplePair
61
+ }{
62
+ {
63
+ iterator : iterator ,
64
+ timestamp : now ,
65
+ expectedSample : dummySamples [0 ],
66
+ },
67
+ } {
68
+ // sampleSeriesIterator should be created lazily only when RangeValues is called.
69
+ require .Nil (t , tc .iterator .sampleSeriesIterator )
70
+ sample := tc .iterator .ValueAtOrBeforeTime (tc .timestamp )
71
+ require .NotNil (t , tc .iterator .sampleSeriesIterator )
72
+
73
+ require .Equal (t , tc .expectedSample , sample )
74
+ }
50
75
}
51
76
}
52
77
53
78
func TestLazySeriesIterator_RangeValues (t * testing.T ) {
54
- store := newTestChunkStore (t , StoreConfig {})
55
79
now := model .Now ()
80
+ ctx := user .InjectOrgID (context .Background (), userID )
81
+
56
82
sampleMetric := model.Metric {model .MetricNameLabel : "foo" }
57
- iterator , err := NewLazySeriesIterator (store , sampleMetric , now , now )
83
+ dummyChunk := dummyChunkFor (sampleMetric )
84
+ dummySamples , err := dummyChunk .Samples ()
58
85
require .NoError (t , err )
59
- for _ , c := range [] struct {
60
- iterator * LazySeriesIterator
61
- interval metric. Interval
62
- expectedSamples []model. SamplePair
86
+
87
+ schemas := [] struct {
88
+ name string
89
+ fn func ( cfg SchemaConfig ) Schema
63
90
}{
64
- {
65
- iterator : iterator ,
66
- interval : metric.Interval {OldestInclusive : now , NewestInclusive : now },
67
- expectedSamples : nil ,
68
- },
69
- } {
70
- samples := c .iterator .RangeValues (c .interval )
71
- require .Equal (t , c .expectedSamples , samples )
91
+ {"v8 schema" , v8Schema },
92
+ }
93
+
94
+ for _ , schema := range schemas {
95
+ // Create store with the dummy chunk.
96
+ store := newTestChunkStore (t , StoreConfig {schemaFactory : schema .fn })
97
+ store .Put (ctx , []Chunk {dummyChunk })
98
+
99
+ // Create the lazy series iterator
100
+ iterator , err := NewLazySeriesIterator (store , sampleMetric , now , now , userID )
101
+ require .NoError (t , err )
102
+ for _ , tc := range []struct {
103
+ iterator * LazySeriesIterator
104
+ interval metric.Interval
105
+ expectedSamples []model.SamplePair
106
+ }{
107
+ {
108
+ iterator : iterator ,
109
+ interval : metric.Interval {OldestInclusive : now , NewestInclusive : now },
110
+ expectedSamples : dummySamples ,
111
+ },
112
+ } {
113
+ // sampleSeriesIterator should be created lazily only when RangeValues is called.
114
+ require .Nil (t , tc .iterator .sampleSeriesIterator )
115
+ samples := tc .iterator .RangeValues (tc .interval )
116
+ require .NotNil (t , tc .iterator .sampleSeriesIterator )
117
+
118
+ require .Equal (t , tc .expectedSamples , samples )
119
+ }
72
120
}
73
121
}
0 commit comments