-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
codec_test.go
125 lines (101 loc) · 3.02 KB
/
codec_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
package prometheus_test
import (
"bytes"
"testing"
pr "github.com/influxdata/influxdb/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
)
func Test_CodecExpfmt(t *testing.T) {
mf1 := []*dto.MetricFamily{NewCounter("mf1", 1.0, pr.L("n1", "v1"))}
mf2 := []*dto.MetricFamily{NewCounter("mf2", 1.0, pr.L("n2", "v2"))}
b1, err := pr.EncodeExpfmt(mf1)
if err != nil {
t.Fatalf("encodeExpfmt() error = %v", err)
}
got1, err := pr.DecodeExpfmt(bytes.NewBuffer(b1), expfmt.FmtProtoDelim)
if err != nil {
t.Fatalf("decodeExpfmt() error = %v", err)
}
for i := range got1 {
if got1[i].String() != mf1[i].String() {
t.Errorf("codec1() = %v, want %v", got1[i].String(), mf1[i].String())
}
}
b2, err := pr.EncodeExpfmt(mf2)
if err != nil {
t.Fatalf("encodeExpfmt() error = %v", err)
}
got2, err := pr.DecodeExpfmt(bytes.NewBuffer(b2), expfmt.FmtProtoDelim)
if err != nil {
t.Fatalf("decodeExpfmt() error = %v", err)
}
for i := range got2 {
if got2[i].String() != mf2[i].String() {
t.Errorf("codec2() = %v, want %v", got2[i].String(), mf2[i].String())
}
}
b3 := append(b2, b1...)
b3 = append(b3, b2...)
mf3 := []*dto.MetricFamily{
NewCounter("mf2", 1.0, pr.L("n2", "v2")),
NewCounter("mf1", 1.0, pr.L("n1", "v1")),
NewCounter("mf2", 1.0, pr.L("n2", "v2")),
}
got3, err := pr.DecodeExpfmt(bytes.NewBuffer(b3), expfmt.FmtProtoDelim)
if err != nil {
t.Fatalf("decodeExpfmt() error = %v", err)
}
for i := range got3 {
if got3[i].String() != mf3[i].String() {
t.Errorf("codec3() = %v, want %v", got3[i].String(), mf3[i].String())
}
}
}
func Test_CodecJSON(t *testing.T) {
mf1 := []*dto.MetricFamily{NewCounter("mf1", 1.0, pr.L("n1", "v1")), NewCounter("mf1", 1.0, pr.L("n1", "v1"))}
mf2 := []*dto.MetricFamily{NewCounter("mf2", 1.0, pr.L("n2", "v2"))}
b1, err := pr.EncodeJSON(mf1)
if err != nil {
t.Fatalf("encodeJSON() error = %v", err)
}
got1, err := pr.DecodeJSON(bytes.NewBuffer(b1))
if err != nil {
t.Fatalf("decodeJSON() error = %v", err)
}
for i := range got1 {
if got1[i].String() != mf1[i].String() {
t.Errorf("codec1() = %v, want %v", got1[i].String(), mf1[i].String())
}
}
b2, err := pr.EncodeJSON(mf2)
if err != nil {
t.Fatalf("encodeJSON() error = %v", err)
}
got2, err := pr.DecodeJSON(bytes.NewBuffer(b2))
if err != nil {
t.Fatalf("decodeJSON() error = %v", err)
}
for i := range got2 {
if got2[i].String() != mf2[i].String() {
t.Errorf("codec2() = %v, want %v", got2[i].String(), mf2[i].String())
}
}
b3 := append(b2, b1...)
b3 = append(b3, b2...)
mf3 := []*dto.MetricFamily{
NewCounter("mf2", 1.0, pr.L("n2", "v2")),
NewCounter("mf1", 1.0, pr.L("n1", "v1")),
NewCounter("mf1", 1.0, pr.L("n1", "v1")),
NewCounter("mf2", 1.0, pr.L("n2", "v2")),
}
got3, err := pr.DecodeJSON(bytes.NewBuffer(b3))
if err != nil {
t.Fatalf("decodeJSON() error = %v", err)
}
for i := range got3 {
if got3[i].String() != mf3[i].String() {
t.Errorf("codec3() = %v, want %v", got3[i].String(), mf3[i].String())
}
}
}