forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric_test.go
143 lines (111 loc) · 3.79 KB
/
metric_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
132
133
134
135
136
137
138
139
140
141
142
143
package msgpack
import (
"encoding/hex"
"math"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMsgPackTime32(t *testing.T) {
// Maximum of 4 bytes encodable time
var sec int64 = 0xFFFFFFFF
var nsec int64 = 0
t1 := MessagePackTime{time: time.Unix(sec, nsec)}
assert.Equal(t, t1.Len(), 4)
buf := make([]byte, t1.Len())
assert.NoError(t, t1.MarshalBinaryTo(buf))
t2 := new(MessagePackTime)
t2.UnmarshalBinary(buf)
assert.Equal(t, t1.time, t2.time)
}
func TestMsgPackTime64(t *testing.T) {
// Maximum of 8 bytes encodable time
var sec int64 = 0x3FFFFFFFF
var nsec int64 = 999999999
t1 := MessagePackTime{time: time.Unix(sec, nsec)}
assert.Equal(t, t1.Len(), 8)
buf := make([]byte, t1.Len())
assert.NoError(t, t1.MarshalBinaryTo(buf))
t2 := new(MessagePackTime)
t2.UnmarshalBinary(buf)
assert.Equal(t, t1.time, t2.time)
}
func TestMsgPackTime96(t *testing.T) {
// Testing 12 bytes timestamp
var sec int64 = 0x400000001
var nsec int64 = 111111111
t1 := MessagePackTime{time: time.Unix(sec, nsec)}
assert.Equal(t, t1.Len(), 12)
buf := make([]byte, t1.Len())
assert.NoError(t, t1.MarshalBinaryTo(buf))
t2 := new(MessagePackTime)
t2.UnmarshalBinary(buf)
assert.True(t, t1.time.Equal(t2.time))
// Testing the default value: 0001-01-01T00:00:00Z
t1 = MessagePackTime{}
assert.Equal(t, t1.Len(), 12)
assert.NoError(t, t1.MarshalBinaryTo(buf))
t2 = new(MessagePackTime)
t2.UnmarshalBinary(buf)
assert.True(t, t1.time.Equal(t2.time))
}
func TestMsgPackTimeEdgeCases(t *testing.T) {
times := make([]time.Time, 0)
expected := make([][]byte, 0)
// Unix epoch. Begin of 4bytes dates
// Nanoseconds: 0x00000000, Seconds: 0x0000000000000000
ts, _ := time.Parse(time.RFC3339, "1970-01-01T00:00:00Z")
bs, _ := hex.DecodeString("d6ff00000000")
times = append(times, ts)
expected = append(expected, bs)
// End of 4bytes dates
// Nanoseconds: 0x00000000, Seconds: 0x00000000ffffffff
ts, _ = time.Parse(time.RFC3339, "2106-02-07T06:28:15Z")
bs, _ = hex.DecodeString("d6ffffffffff")
times = append(times, ts)
expected = append(expected, bs)
// Begin of 8bytes dates
// Nanoseconds: 0x00000000, Seconds: 0x0000000100000000
ts, _ = time.Parse(time.RFC3339, "2106-02-07T06:28:16Z")
bs, _ = hex.DecodeString("d7ff0000000100000000")
times = append(times, ts)
expected = append(expected, bs)
// Just after Unix epoch. Non zero nanoseconds
// Nanoseconds: 0x00000001, Seconds: 0x0000000000000000
ts, _ = time.Parse(time.RFC3339Nano, "1970-01-01T00:00:00.000000001Z")
bs, _ = hex.DecodeString("d7ff0000000400000000")
times = append(times, ts)
expected = append(expected, bs)
// End of 8bytes dates
// Nanoseconds: 0x00000000, Seconds: 0x00000003ffffffff
ts, _ = time.Parse(time.RFC3339Nano, "2514-05-30T01:53:03.000000000Z")
bs, _ = hex.DecodeString("d7ff00000003ffffffff")
times = append(times, ts)
expected = append(expected, bs)
// Begin of 12bytes date
// Nanoseconds: 0x00000000, Seconds: 0x0000000400000000
ts, _ = time.Parse(time.RFC3339Nano, "2514-05-30T01:53:04.000000000Z")
bs, _ = hex.DecodeString("c70cff000000000000000400000000")
times = append(times, ts)
expected = append(expected, bs)
// Zero value, 0001-01-01T00:00:00Z
// Nanoseconds: 0x00000000, Seconds: 0xfffffff1886e0900
ts = time.Time{}
bs, _ = hex.DecodeString("c70cff00000000fffffff1886e0900")
times = append(times, ts)
expected = append(expected, bs)
// Max value
// Nanoseconds: 0x3b9ac9ff, Seconds: 0x7fffffffffffffff
ts = time.Unix(math.MaxInt64, 999_999_999).UTC()
bs, _ = hex.DecodeString("c70cff3b9ac9ff7fffffffffffffff")
times = append(times, ts)
expected = append(expected, bs)
buf := make([]byte, 0)
for i, ts := range times {
t1 := MessagePackTime{time: ts}
m := Metric{Time: t1}
buf = buf[:0]
buf, _ = m.MarshalMsg(buf)
assert.Equal(t, expected[i], buf[12:len(buf)-14])
}
}