forked from timescale/tsbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_serializer_utils.go
104 lines (90 loc) · 2.85 KB
/
point_serializer_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package serialize
import (
"bytes"
"fmt"
"github.com/keshav-kk/tsbs/pkg/data"
"testing"
"time"
)
var (
TestNow = time.Unix(1451606400, 0)
TestMeasurement = []byte("cpu")
TestTagKeys = [][]byte{[]byte("hostname"), []byte("region"), []byte("datacenter")}
TestTagVals = []interface{}{"host_0", "eu-west-1", "eu-west-1b"}
TestColFloat = []byte("usage_guest_nice")
TestColInt = []byte("usage_guest")
TestColInt64 = []byte("big_usage_guest")
)
const (
TestFloat = float64(38.24311829)
TestInt = 38
TestInt64 = int64(5000000000)
ErrWriterAlwaysErr = "bad write: I always error"
ErrWriterSometimesErr = "bad write: I sometimes error"
)
type ErrWriter struct {
SkipOne bool
Cnt int
}
func (w *ErrWriter) Write(p []byte) (n int, err error) {
if !w.SkipOne {
return 0, fmt.Errorf(ErrWriterAlwaysErr)
} else if w.Cnt < 1 {
w.Cnt++
return len(p), nil
} else {
return 0, fmt.Errorf(ErrWriterSometimesErr)
}
}
func generateTestPoint(name []byte, tagKeys [][]byte, tagVals []interface{}, ts *time.Time, fieldKeys [][]byte, fieldValues []interface{}) *data.Point {
p := &data.Point{}
p.SetMeasurementName(name)
p.SetTimestamp(ts)
for i, tagKey := range tagKeys {
p.AppendTag(tagKey, tagVals[i])
}
for i, fieldKey := range fieldKeys {
p.AppendField(fieldKey, fieldValues[i])
}
return p
}
func TestPointDefault() *data.Point {
return generateTestPoint(TestMeasurement, TestTagKeys, TestTagVals, &TestNow,
[][]byte{TestColFloat}, []interface{}{TestFloat})
}
func TestPointMultiField() *data.Point {
return generateTestPoint(TestMeasurement, TestTagKeys, TestTagVals,
&TestNow, [][]byte{TestColInt64, TestColInt, TestColFloat},
[]interface{}{TestInt64, TestInt, TestFloat})
}
func TestPointInt() *data.Point {
return generateTestPoint(TestMeasurement, TestTagKeys, TestTagVals, &TestNow,
[][]byte{TestColInt}, []interface{}{TestInt})
}
func TestPointNoTags() *data.Point {
return generateTestPoint(TestMeasurement, [][]byte{}, []interface{}{}, &TestNow,
[][]byte{TestColFloat}, []interface{}{TestFloat})
}
func TestPointWithNilTag() *data.Point {
return generateTestPoint(TestMeasurement, [][]byte{[]byte("hostname")}, []interface{}{nil}, &TestNow,
[][]byte{TestColFloat}, []interface{}{TestFloat})
}
func TestPointWithNilField() *data.Point {
return generateTestPoint(TestMeasurement, [][]byte{}, []interface{}{}, &TestNow,
[][]byte{TestColInt64, TestColFloat}, []interface{}{nil, TestFloat})
}
type SerializeCase struct {
Desc string
InputPoint *data.Point
Output string
}
func SerializerTest(t *testing.T, cases []SerializeCase, ps PointSerializer) {
for _, c := range cases {
b := new(bytes.Buffer)
ps.Serialize(c.InputPoint, b)
got := b.String()
if got != c.Output {
t.Errorf("%s \nOutput incorrect: \nWant: '%s' \nGot: '%s'", c.Desc, c.Output, got)
}
}
}