forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgpack_test.go
131 lines (96 loc) · 2.66 KB
/
msgpack_test.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package msgpack
import (
"testing"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
)
func toTelegrafMetric(m Metric) telegraf.Metric {
tm := metric.New(m.Name, m.Tags, m.Fields, m.Time.time)
return tm
}
func TestSerializeMetricInt(t *testing.T) {
m := testutil.TestMetric(int64(90))
s := Serializer{}
var buf []byte
buf, err := s.Serialize(m)
assert.NoError(t, err)
m2 := &Metric{}
left, err := m2.UnmarshalMsg(buf)
assert.NoError(t, err)
assert.Equal(t, len(left), 0)
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2))
}
func TestSerializeMetricString(t *testing.T) {
m := testutil.TestMetric("foobar")
s := Serializer{}
var buf []byte
buf, err := s.Serialize(m)
assert.NoError(t, err)
m2 := &Metric{}
left, err := m2.UnmarshalMsg(buf)
assert.NoError(t, err)
assert.Equal(t, len(left), 0)
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2))
}
func TestSerializeMultiFields(t *testing.T) {
m := testutil.TestMetric(int(90))
m.AddField("value2", 8559615)
s := Serializer{}
var buf []byte
buf, err := s.Serialize(m)
assert.NoError(t, err)
m2 := &Metric{}
left, err := m2.UnmarshalMsg(buf)
assert.NoError(t, err)
assert.Equal(t, len(left), 0)
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2))
}
func TestSerializeMetricWithEscapes(t *testing.T) {
m := testutil.TestMetric(int(90))
m.AddField("U,age=Idle", int64(90))
m.AddTag("cpu tag", "cpu0")
s := Serializer{}
var buf []byte
buf, err := s.Serialize(m)
assert.NoError(t, err)
m2 := &Metric{}
left, err := m2.UnmarshalMsg(buf)
assert.NoError(t, err)
assert.Equal(t, len(left), 0)
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*m2))
}
func TestSerializeMultipleMetric(t *testing.T) {
m := testutil.TestMetric(int(90))
s := Serializer{}
encoded, err := s.Serialize(m)
assert.NoError(t, err)
// Multiple metrics in continous bytes stream
var buf []byte
buf = append(buf, encoded...)
buf = append(buf, encoded...)
buf = append(buf, encoded...)
buf = append(buf, encoded...)
left := buf
for len(left) > 0 {
decodeM := &Metric{}
left, err = decodeM.UnmarshalMsg(left)
assert.NoError(t, err)
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*decodeM))
}
}
func TestSerializeBatch(t *testing.T) {
m := testutil.TestMetric(int(90))
metrics := []telegraf.Metric{m, m, m, m}
s := Serializer{}
buf, err := s.SerializeBatch(metrics)
assert.NoError(t, err)
left := buf
for len(left) > 0 {
decodeM := &Metric{}
left, err = decodeM.UnmarshalMsg(left)
assert.NoError(t, err)
testutil.RequireMetricEqual(t, m, toTelegrafMetric(*decodeM))
}
}