@@ -6,11 +6,59 @@ import (
6
6
"testing"
7
7
"time"
8
8
9
+ "github.com/grafana/metrictank/mdata/chunk"
9
10
"github.com/raintank/schema"
10
11
)
11
12
13
+ func getTestData (ts , interval , length uint32 , val float64 ) []point {
14
+ testData := make ([]point , 0 , length )
15
+
16
+ for i := uint32 (0 ); i < length ; i ++ {
17
+ testData = append (testData , point {ts : ts , val : val })
18
+ val ++
19
+ ts += interval
20
+ }
21
+
22
+ return testData
23
+ }
24
+
25
+ func chunksFromPoints (points []point , chunkSpan uint32 ) []* chunk.Chunk {
26
+ var t0 , prevT0 uint32
27
+ var c * chunk.Chunk
28
+ var encodedChunks []* chunk.Chunk
29
+
30
+ for _ , point := range points {
31
+ t0 = point .ts - (point .ts % chunkSpan )
32
+
33
+ if prevT0 == 0 {
34
+ c = chunk .New (t0 )
35
+ prevT0 = t0
36
+ } else if prevT0 != t0 {
37
+ c .Finish ()
38
+ encodedChunks = append (encodedChunks , c )
39
+
40
+ c = chunk .New (t0 )
41
+ prevT0 = t0
42
+ }
43
+
44
+ c .Push (point .ts , point .val )
45
+ }
46
+
47
+ c .Finish ()
48
+ encodedChunks = append (encodedChunks , c )
49
+
50
+ return encodedChunks
51
+ }
52
+
12
53
func TestArchiveRequestEncodingDecoding (t * testing.T ) {
13
- key , _ := schema .AMKeyFromString ("98.12345678901234567890123456789012" )
54
+ testData := getTestData (6000 , 60 , 100 , 0 )
55
+ chunks := chunksFromPoints (testData , 1800 )
56
+
57
+ id := "98.12345678901234567890123456789012"
58
+ key , err := schema .AMKeyFromString (id + "_sum_3600" )
59
+ if err != nil {
60
+ t .Fatalf ("Expected no error when getting AMKey from string %q" , err )
61
+ }
14
62
originalRequest := ArchiveRequest {
15
63
MetricData : schema.MetricData {
16
64
Id : "98.12345678901234567890123456789012" ,
@@ -23,16 +71,17 @@ func TestArchiveRequestEncodingDecoding(t *testing.T) {
23
71
Mtype : "test" ,
24
72
Tags : []string {"some=tag" },
25
73
},
26
- ChunkWriteRequests : []ChunkWriteRequest {
27
- {
28
- Callback : nil ,
29
- Key : key ,
30
- TTL : 1 ,
31
- T0 : 2 ,
32
- Data : []byte ("abcdefghijklmnopqrstuvwxyz" ),
33
- Timestamp : time .Unix (123 , 456 ),
34
- },
35
- },
74
+ }
75
+
76
+ for i := 0 ; i < len (chunks ); i ++ {
77
+ originalRequest .ChunkWriteRequests = append (originalRequest .ChunkWriteRequests , ChunkWriteRequest {
78
+ Callback : nil ,
79
+ Key : key ,
80
+ TTL : 1 ,
81
+ T0 : chunks [i ].Series .T0 ,
82
+ Data : chunks [i ].Encode (1800 ),
83
+ Timestamp : time .Unix (123 , 456 ),
84
+ })
36
85
}
37
86
38
87
encoded , err := originalRequest .MarshalCompressed ()
@@ -49,6 +98,28 @@ func TestArchiveRequestEncodingDecoding(t *testing.T) {
49
98
if ! reflect .DeepEqual (originalRequest , got ) {
50
99
t .Fatalf ("Decoded request is different than the encoded one.\n exp: %+v\n got: %+v\n " , originalRequest , got )
51
100
}
101
+
102
+ var decodedData []point
103
+ for _ , c := range got .ChunkWriteRequests {
104
+ itgen , err := chunk .NewIterGen (uint32 (c .T0 ), 0 , c .Data )
105
+ if err != nil {
106
+ t .Fatalf ("Expected no error when getting IterGen %q" , err )
107
+ }
108
+
109
+ iter , err := itgen .Get ()
110
+ if err != nil {
111
+ t .Fatalf ("Expected no error when getting Iterator %q" , err )
112
+ }
113
+
114
+ for iter .Next () {
115
+ ts , val := iter .Values ()
116
+ decodedData = append (decodedData , point {ts : ts , val : val })
117
+ }
118
+ }
119
+
120
+ if ! reflect .DeepEqual (testData , decodedData ) {
121
+ t .Fatalf ("Decoded request is different than the encoded one.\n exp: %+v\n got: %+v\n " , originalRequest , got )
122
+ }
52
123
}
53
124
54
125
func TestArchiveRequestEncodingWithUnknownVersion (t * testing.T ) {
0 commit comments