Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 0461e94

Browse files
committed
fix key encoding bug and add test for it
1 parent b0327ee commit 0461e94

File tree

2 files changed

+83
-12
lines changed

2 files changed

+83
-12
lines changed

mdata/cwr_test.go

+82-11
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,59 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/grafana/metrictank/mdata/chunk"
910
"github.com/raintank/schema"
1011
)
1112

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+
1253
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+
}
1462
originalRequest := ArchiveRequest{
1563
MetricData: schema.MetricData{
1664
Id: "98.12345678901234567890123456789012",
@@ -23,16 +71,17 @@ func TestArchiveRequestEncodingDecoding(t *testing.T) {
2371
Mtype: "test",
2472
Tags: []string{"some=tag"},
2573
},
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+
})
3685
}
3786

3887
encoded, err := originalRequest.MarshalCompressed()
@@ -49,6 +98,28 @@ func TestArchiveRequestEncodingDecoding(t *testing.T) {
4998
if !reflect.DeepEqual(originalRequest, got) {
5099
t.Fatalf("Decoded request is different than the encoded one.\nexp: %+v\ngot: %+v\n", originalRequest, got)
51100
}
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.\nexp: %+v\ngot: %+v\n", originalRequest, got)
122+
}
52123
}
53124

54125
func TestArchiveRequestEncodingWithUnknownVersion(t *testing.T) {

vendor/github.com/raintank/schema/key.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)