@@ -17,7 +17,6 @@ import (
1717 "fmt"
1818 "io"
1919 "math/rand"
20- "reflect"
2120 "testing"
2221
2322 "github.com/prometheus/tsdb/testutil"
@@ -35,28 +34,23 @@ func TestChunk(t *testing.T) {
3534 t .Run (fmt .Sprintf ("%v" , enc ), func (t * testing.T ) {
3635 for range make ([]struct {}, 1 ) {
3736 c := nc ()
38- if err := testChunk (c ); err != nil {
39- t .Fatal (err )
40- }
37+ testChunk (t , c )
4138 }
4239 })
4340 }
4441}
4542
46- func testChunk (c Chunk ) error {
43+ func testChunk (t * testing. T , c Chunk ) {
4744 app , err := c .Appender ()
48- if err != nil {
49- return err
50- }
45+ testutil .Ok (t , err )
5146
52- var exp []pair
47+ var all []pair
5348 var (
5449 ts = int64 (1234123324 )
5550 v = 1243535.123
5651 )
5752 for i := 0 ; i < 300 ; i ++ {
5853 ts += int64 (rand .Intn (10000 ) + 1 )
59- // v = rand.Float64()
6054 if i % 2 == 0 {
6155 v += float64 (rand .Intn (1000000 ))
6256 } else {
@@ -67,29 +61,53 @@ func testChunk(c Chunk) error {
6761 // appending to a partially filled chunk.
6862 if i % 10 == 0 {
6963 app , err = c .Appender ()
70- if err != nil {
71- return err
72- }
64+ testutil .Ok (t , err )
7365 }
7466
7567 app .Append (ts , v )
76- exp = append (exp , pair {t : ts , v : v })
77- // fmt.Println("appended", len(c.Bytes()), c.Bytes())
68+ all = append (all , pair {t : ts , v : v })
7869 }
7970
80- it := c .Iterator (nil )
81- var res []pair
82- for it .Next () {
83- ts , v := it .At ()
84- res = append (res , pair {t : ts , v : v })
71+ // 1. Expand iterator in simple case.
72+ it1 := c .Iterator (nil )
73+ var res1 []pair
74+ for it1 .Next () {
75+ ts , v := it1 .At ()
76+ res1 = append (res1 , pair {t : ts , v : v })
8577 }
86- if it .Err () != nil {
87- return it .Err ()
78+ testutil .Ok (t , it1 .Err ())
79+ testutil .Equals (t , all , res1 )
80+
81+ // 2. Expand second iterator while reusing first one.
82+ it2 := c .Iterator (it1 )
83+ var res2 []pair
84+ for it2 .Next () {
85+ ts , v := it2 .At ()
86+ res2 = append (res2 , pair {t : ts , v : v })
8887 }
89- if ! reflect .DeepEqual (exp , res ) {
90- return fmt .Errorf ("unexpected result\n \n got: %v\n \n exp: %v" , res , exp )
88+ testutil .Ok (t , it2 .Err ())
89+ testutil .Equals (t , all , res2 )
90+
91+ // 3. Test Iterator Seek.
92+ mid := len (all ) / 2
93+
94+ it3 := c .Iterator (nil )
95+ var res3 []pair
96+ testutil .Equals (t , true , it3 .Seek (all [mid ].t ))
97+ // Below ones should not matter.
98+ testutil .Equals (t , true , it3 .Seek (all [mid ].t ))
99+ testutil .Equals (t , true , it3 .Seek (all [mid ].t ))
100+ ts , v = it3 .At ()
101+ res3 = append (res3 , pair {t : ts , v : v })
102+
103+ for it3 .Next () {
104+ ts , v := it3 .At ()
105+ res3 = append (res3 , pair {t : ts , v : v })
91106 }
92- return nil
107+ testutil .Ok (t , it3 .Err ())
108+ testutil .Equals (t , all [mid :], res3 )
109+
110+ testutil .Equals (t , false , it3 .Seek (all [len (all )- 1 ].t + 1 ))
93111}
94112
95113func benchmarkIterator (b * testing.B , newChunk func () Chunk ) {
0 commit comments