2014-09-10 16:41:52 +00:00
|
|
|
package codec
|
2014-08-21 20:06:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
)
|
|
|
|
|
2014-09-10 16:41:52 +00:00
|
|
|
func newCodableFingerprint(fp int64) *CodableFingerprint {
|
|
|
|
cfp := CodableFingerprint(fp)
|
2014-08-21 20:06:11 +00:00
|
|
|
return &cfp
|
|
|
|
}
|
|
|
|
|
2014-09-10 16:41:52 +00:00
|
|
|
func newCodableLabelName(ln string) *CodableLabelName {
|
|
|
|
cln := CodableLabelName(ln)
|
2014-08-21 20:06:11 +00:00
|
|
|
return &cln
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCodec(t *testing.T) {
|
|
|
|
scenarios := []struct {
|
|
|
|
in codable
|
|
|
|
out codable
|
|
|
|
equal func(in, out codable) bool
|
|
|
|
}{
|
|
|
|
{
|
2014-09-10 16:41:52 +00:00
|
|
|
in: CodableMetric{
|
2014-08-21 20:06:11 +00:00
|
|
|
"label_1": "value_2",
|
|
|
|
"label_2": "value_2",
|
|
|
|
"label_3": "value_3",
|
|
|
|
},
|
2014-09-10 16:41:52 +00:00
|
|
|
out: CodableMetric{},
|
2014-08-21 20:06:11 +00:00
|
|
|
equal: func(in, out codable) bool {
|
2014-09-10 16:41:52 +00:00
|
|
|
m1 := clientmodel.Metric(in.(CodableMetric))
|
|
|
|
m2 := clientmodel.Metric(out.(CodableMetric))
|
2014-08-21 20:06:11 +00:00
|
|
|
return m1.Equal(m2)
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
in: newCodableFingerprint(12345),
|
|
|
|
out: newCodableFingerprint(0),
|
|
|
|
equal: func(in, out codable) bool {
|
2014-09-10 16:41:52 +00:00
|
|
|
return *in.(*CodableFingerprint) == *out.(*CodableFingerprint)
|
2014-08-21 20:06:11 +00:00
|
|
|
},
|
|
|
|
}, {
|
2014-09-10 16:41:52 +00:00
|
|
|
in: &CodableFingerprints{1, 2, 56, 1234},
|
|
|
|
out: &CodableFingerprints{},
|
2014-08-21 20:06:11 +00:00
|
|
|
equal: func(in, out codable) bool {
|
2014-09-10 16:41:52 +00:00
|
|
|
fps1 := *in.(*CodableFingerprints)
|
|
|
|
fps2 := *out.(*CodableFingerprints)
|
2014-08-21 20:06:11 +00:00
|
|
|
if len(fps1) != len(fps2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, _ := range fps1 {
|
|
|
|
if fps1[i] != fps2[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}, {
|
2014-09-10 16:41:52 +00:00
|
|
|
in: &CodableLabelPair{
|
2014-08-21 20:06:11 +00:00
|
|
|
Name: "label_name",
|
|
|
|
Value: "label_value",
|
|
|
|
},
|
2014-09-10 16:41:52 +00:00
|
|
|
out: &CodableLabelPair{},
|
2014-08-21 20:06:11 +00:00
|
|
|
equal: func(in, out codable) bool {
|
2014-09-10 16:41:52 +00:00
|
|
|
lp1 := *in.(*CodableLabelPair)
|
|
|
|
lp2 := *out.(*CodableLabelPair)
|
2014-08-21 20:06:11 +00:00
|
|
|
return lp1 == lp2
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
in: newCodableLabelName("label_name"),
|
|
|
|
out: newCodableLabelName(""),
|
|
|
|
equal: func(in, out codable) bool {
|
2014-09-10 16:41:52 +00:00
|
|
|
ln1 := *in.(*CodableLabelName)
|
|
|
|
ln2 := *out.(*CodableLabelName)
|
2014-08-21 20:06:11 +00:00
|
|
|
return ln1 == ln2
|
|
|
|
},
|
|
|
|
}, {
|
2014-09-10 16:41:52 +00:00
|
|
|
in: &CodableLabelValues{"value_1", "value_2", "value_3"},
|
|
|
|
out: &CodableLabelValues{},
|
2014-08-21 20:06:11 +00:00
|
|
|
equal: func(in, out codable) bool {
|
2014-09-10 16:41:52 +00:00
|
|
|
lvs1 := *in.(*CodableLabelValues)
|
|
|
|
lvs2 := *out.(*CodableLabelValues)
|
2014-08-21 20:06:11 +00:00
|
|
|
if len(lvs1) != len(lvs2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, _ := range lvs1 {
|
|
|
|
if lvs1[i] != lvs2[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}, {
|
2014-09-10 16:41:52 +00:00
|
|
|
in: &CodableTimeRange{42, 2001},
|
|
|
|
out: &CodableTimeRange{},
|
2014-08-21 20:06:11 +00:00
|
|
|
equal: func(in, out codable) bool {
|
2014-09-10 16:41:52 +00:00
|
|
|
ln1 := *in.(*CodableTimeRange)
|
|
|
|
ln2 := *out.(*CodableTimeRange)
|
|
|
|
return ln1 == ln2
|
2014-08-21 20:06:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, s := range scenarios {
|
2014-09-09 12:36:26 +00:00
|
|
|
encoded, err := s.in.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := s.out.UnmarshalBinary(encoded); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-21 20:06:11 +00:00
|
|
|
if !s.equal(s.in, s.out) {
|
2014-09-09 12:36:26 +00:00
|
|
|
t.Errorf("%d. Got: %v; want %v; encoded bytes are: %v", i, s.out, s.in, encoded)
|
2014-08-21 20:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|