-
Notifications
You must be signed in to change notification settings - Fork 817
/
Copy pathcompat_test.go
138 lines (113 loc) · 4.31 KB
/
compat_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
package cortexpb
import (
"encoding/json"
"math"
"testing"
"unsafe"
jsoniter "github.com/json-iterator/go"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// This test verifies that jsoninter uses our custom method for marshalling.
// We do that by using "test sample" recognized by marshal function when in testing mode.
func TestJsoniterMarshalForSample(t *testing.T) {
testMarshalling(t, jsoniter.Marshal, "test sample")
}
func TestStdlibJsonMarshalForSample(t *testing.T) {
testMarshalling(t, json.Marshal, "json: error calling MarshalJSON for type cortexpb.Sample: test sample")
}
func testMarshalling(t *testing.T, marshalFn func(v interface{}) ([]byte, error), expectedError string) {
isTesting = true
defer func() { isTesting = false }()
out, err := marshalFn(Sample{Value: 12345, TimestampMs: 98765})
require.NoError(t, err)
require.Equal(t, `[98.765,"12345"]`, string(out))
_, err = marshalFn(Sample{Value: math.NaN(), TimestampMs: 0})
require.EqualError(t, err, expectedError)
// If not testing, we get normal output.
isTesting = false
out, err = marshalFn(Sample{Value: math.NaN(), TimestampMs: 0})
require.NoError(t, err)
require.Equal(t, `[0,"NaN"]`, string(out))
}
// This test verifies that jsoninter uses our custom method for unmarshalling Sample.
// As with Marshal, we rely on testing mode and special value that reports error.
func TestJsoniterUnmarshalForSample(t *testing.T) {
testUnmarshalling(t, jsoniter.Unmarshal, "test sample")
}
func TestStdlibJsonUnmarshalForSample(t *testing.T) {
testUnmarshalling(t, json.Unmarshal, "test sample")
}
func testUnmarshalling(t *testing.T, unmarshalFn func(data []byte, v interface{}) error, expectedError string) {
isTesting = true
defer func() { isTesting = false }()
sample := Sample{}
err := unmarshalFn([]byte(`[98.765,"12345"]`), &sample)
require.NoError(t, err)
require.Equal(t, Sample{Value: 12345, TimestampMs: 98765}, sample)
err = unmarshalFn([]byte(`[0.0,"NaN"]`), &sample)
require.EqualError(t, err, expectedError)
isTesting = false
err = unmarshalFn([]byte(`[0.0,"NaN"]`), &sample)
require.NoError(t, err)
require.Equal(t, int64(0), sample.TimestampMs)
require.True(t, math.IsNaN(sample.Value))
}
func TestMetricMetadataToMetricTypeToMetricType(t *testing.T) {
tc := []struct {
desc string
input MetricMetadata_MetricType
expected model.MetricType
}{
{
desc: "with a single-word metric",
input: COUNTER,
expected: model.MetricTypeCounter,
},
{
desc: "with a two-word metric",
input: STATESET,
expected: model.MetricTypeStateset,
},
{
desc: "with an unknown metric",
input: MetricMetadata_MetricType(100),
expected: model.MetricTypeUnknown,
},
}
for _, tt := range tc {
t.Run(tt.desc, func(t *testing.T) {
m := MetricMetadataMetricTypeToMetricType(tt.input)
assert.Equal(t, tt.expected, m)
})
}
}
func TestFromLabelAdaptersToLabels(t *testing.T) {
input := []LabelPair{{Name: "hello", Value: "world"}}
expected := labels.Labels{labels.Label{Name: "hello", Value: "world"}}
actual := FromLabelAdaptersToLabels(input)
assert.Equal(t, expected, actual)
// All strings must NOT be copied.
assert.Equal(t, uintptr(unsafe.Pointer(&input[0].Name)), uintptr(unsafe.Pointer(&actual[0].Name)))
assert.Equal(t, uintptr(unsafe.Pointer(&input[0].Value)), uintptr(unsafe.Pointer(&actual[0].Value)))
}
func TestFromLabelAdaptersToLabelsWithCopy(t *testing.T) {
input := []LabelPair{{Name: "hello", Value: "world"}}
expected := labels.Labels{labels.Label{Name: "hello", Value: "world"}}
actual := FromLabelAdaptersToLabelsWithCopy(input)
assert.Equal(t, expected, actual)
// All strings must be copied.
assert.NotEqual(t, uintptr(unsafe.Pointer(&input[0].Name)), uintptr(unsafe.Pointer(&actual[0].Name)))
assert.NotEqual(t, uintptr(unsafe.Pointer(&input[0].Value)), uintptr(unsafe.Pointer(&actual[0].Value)))
}
func BenchmarkFromLabelAdaptersToLabelsWithCopy(b *testing.B) {
input := []LabelPair{
{Name: "hello", Value: "world"},
{Name: "some label", Value: "and its value"},
{Name: "long long long long long label name", Value: "perhaps even longer label value, but who's counting anyway?"}}
for i := 0; i < b.N; i++ {
FromLabelAdaptersToLabelsWithCopy(input)
}
}