-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
set_test.go
369 lines (329 loc) · 10 KB
/
set_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package attribute_test
import (
"reflect"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
)
type testCase struct {
kvs []attribute.KeyValue
keyRe *regexp.Regexp
encoding string
fullEnc string
}
func expect(enc string, kvs ...attribute.KeyValue) testCase {
return testCase{
kvs: kvs,
encoding: enc,
}
}
func expectFiltered(enc, filter, fullEnc string, kvs ...attribute.KeyValue) testCase {
return testCase{
kvs: kvs,
keyRe: regexp.MustCompile(filter),
encoding: enc,
fullEnc: fullEnc,
}
}
func TestSetDedup(t *testing.T) {
cases := []testCase{
expect("A=B", attribute.String("A", "2"), attribute.String("A", "B")),
expect("A=B", attribute.String("A", "2"), attribute.Int("A", 1), attribute.String("A", "B")),
expect("A=B", attribute.String("A", "B"), attribute.String("A", "C"), attribute.String("A", "D"), attribute.String("A", "B")),
expect("A=B,C=D", attribute.String("A", "1"), attribute.String("C", "D"), attribute.String("A", "B")),
expect("A=B,C=D", attribute.String("A", "2"), attribute.String("A", "B"), attribute.String("C", "D")),
expect("A=B,C=D", attribute.Float64("C", 1.2), attribute.String("A", "2"), attribute.String("A", "B"), attribute.String("C", "D")),
expect("A=B,C=D", attribute.String("C", "D"), attribute.String("A", "B"), attribute.String("A", "C"), attribute.String("A", "D"), attribute.String("A", "B")),
expect("A=B,C=D", attribute.String("A", "B"), attribute.String("C", "D"), attribute.String("A", "C"), attribute.String("A", "D"), attribute.String("A", "B")),
expect("A=B,C=D", attribute.String("A", "B"), attribute.String("A", "C"), attribute.String("A", "D"), attribute.String("A", "B"), attribute.String("C", "D")),
}
enc := attribute.DefaultEncoder()
s2d := map[string][]attribute.Distinct{}
d2s := map[attribute.Distinct][]string{}
for _, tc := range cases {
cpy := make([]attribute.KeyValue, len(tc.kvs))
copy(cpy, tc.kvs)
sl := attribute.NewSet(cpy...)
// Ensure that the input was reordered but no elements went missing.
require.ElementsMatch(t, tc.kvs, cpy)
str := sl.Encoded(enc)
equ := sl.Equivalent()
s2d[str] = append(s2d[str], equ)
d2s[equ] = append(d2s[equ], str)
require.Equal(t, tc.encoding, str)
}
for s, d := range s2d {
// No other Distinct values are equal to this.
for s2, d2 := range s2d {
if s2 == s {
continue
}
for _, elt := range d {
for _, otherDistinct := range d2 {
require.NotEqual(t, otherDistinct, elt)
}
}
}
for _, strings := range d2s {
if strings[0] == s {
continue
}
for _, otherString := range strings {
require.NotEqual(t, otherString, s)
}
}
}
for d, s := range d2s {
// No other Distinct values are equal to this.
for d2, s2 := range d2s {
if d2 == d {
continue
}
for _, elt := range s {
for _, otherDistinct := range s2 {
require.NotEqual(t, otherDistinct, elt)
}
}
}
for _, distincts := range s2d {
if distincts[0] == d {
continue
}
for _, otherDistinct := range distincts {
require.NotEqual(t, otherDistinct, d)
}
}
}
}
func TestFiltering(t *testing.T) {
a := attribute.String("A", "a")
b := attribute.String("B", "b")
c := attribute.String("C", "c")
tests := []struct {
name string
in []attribute.KeyValue
filter attribute.Filter
kept, drop []attribute.KeyValue
}{
{
name: "A",
in: []attribute.KeyValue{a, b, c},
filter: func(kv attribute.KeyValue) bool { return kv.Key == "A" },
kept: []attribute.KeyValue{a},
drop: []attribute.KeyValue{b, c},
},
{
name: "B",
in: []attribute.KeyValue{a, b, c},
filter: func(kv attribute.KeyValue) bool { return kv.Key == "B" },
kept: []attribute.KeyValue{b},
drop: []attribute.KeyValue{a, c},
},
{
name: "C",
in: []attribute.KeyValue{a, b, c},
filter: func(kv attribute.KeyValue) bool { return kv.Key == "C" },
kept: []attribute.KeyValue{c},
drop: []attribute.KeyValue{a, b},
},
{
name: "A||B",
in: []attribute.KeyValue{a, b, c},
filter: func(kv attribute.KeyValue) bool {
return kv.Key == "A" || kv.Key == "B"
},
kept: []attribute.KeyValue{a, b},
drop: []attribute.KeyValue{c},
},
{
name: "B||C",
in: []attribute.KeyValue{a, b, c},
filter: func(kv attribute.KeyValue) bool {
return kv.Key == "B" || kv.Key == "C"
},
kept: []attribute.KeyValue{b, c},
drop: []attribute.KeyValue{a},
},
{
name: "A||C",
in: []attribute.KeyValue{a, b, c},
filter: func(kv attribute.KeyValue) bool {
return kv.Key == "A" || kv.Key == "C"
},
kept: []attribute.KeyValue{a, c},
drop: []attribute.KeyValue{b},
},
{
name: "None",
in: []attribute.KeyValue{a, b, c},
filter: func(kv attribute.KeyValue) bool { return false },
kept: nil,
drop: []attribute.KeyValue{a, b, c},
},
{
name: "All",
in: []attribute.KeyValue{a, b, c},
filter: func(kv attribute.KeyValue) bool { return true },
kept: []attribute.KeyValue{a, b, c},
drop: nil,
},
{
name: "Empty",
in: []attribute.KeyValue{},
filter: func(kv attribute.KeyValue) bool { return true },
kept: nil,
drop: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Run("NewSetWithFiltered", func(t *testing.T) {
fltr, drop := attribute.NewSetWithFiltered(test.in, test.filter)
assert.Equal(t, test.kept, fltr.ToSlice(), "filtered")
assert.ElementsMatch(t, test.drop, drop, "dropped")
})
t.Run("Set.Filter", func(t *testing.T) {
s := attribute.NewSet(test.in...)
fltr, drop := s.Filter(test.filter)
assert.Equal(t, test.kept, fltr.ToSlice(), "filtered")
assert.ElementsMatch(t, test.drop, drop, "dropped")
})
})
}
}
func TestUniqueness(t *testing.T) {
short := []attribute.KeyValue{
attribute.String("A", "0"),
attribute.String("B", "2"),
attribute.String("A", "1"),
}
long := []attribute.KeyValue{
attribute.String("B", "2"),
attribute.String("C", "5"),
attribute.String("B", "2"),
attribute.String("C", "1"),
attribute.String("A", "4"),
attribute.String("C", "3"),
attribute.String("A", "1"),
}
cases := []testCase{
expectFiltered("A=1", "^A$", "B=2", short...),
expectFiltered("B=2", "^B$", "A=1", short...),
expectFiltered("A=1,B=2", "^A|B$", "", short...),
expectFiltered("", "^C", "A=1,B=2", short...),
expectFiltered("A=1,C=3", "A|C", "B=2", long...),
expectFiltered("B=2,C=3", "C|B", "A=1", long...),
expectFiltered("C=3", "C", "A=1,B=2", long...),
expectFiltered("", "D", "A=1,B=2,C=3", long...),
}
enc := attribute.DefaultEncoder()
for _, tc := range cases {
cpy := make([]attribute.KeyValue, len(tc.kvs))
copy(cpy, tc.kvs)
distinct, uniq := attribute.NewSetWithFiltered(cpy, func(attr attribute.KeyValue) bool {
return tc.keyRe.MatchString(string(attr.Key))
})
full := attribute.NewSet(uniq...)
require.Equal(t, tc.encoding, distinct.Encoded(enc))
require.Equal(t, tc.fullEnc, full.Encoded(enc))
}
}
func TestLookup(t *testing.T) {
set := attribute.NewSet(attribute.Int("C", 3), attribute.Int("A", 1), attribute.Int("B", 2))
value, has := set.Value("C")
require.True(t, has)
require.Equal(t, int64(3), value.AsInt64())
value, has = set.Value("B")
require.True(t, has)
require.Equal(t, int64(2), value.AsInt64())
value, has = set.Value("A")
require.True(t, has)
require.Equal(t, int64(1), value.AsInt64())
_, has = set.Value("D")
require.False(t, has)
}
func TestZeroSetExportedMethodsNoPanic(t *testing.T) {
rType := reflect.TypeOf((*attribute.Set)(nil))
rVal := reflect.ValueOf(&attribute.Set{})
for n := 0; n < rType.NumMethod(); n++ {
mType := rType.Method(n)
if !mType.IsExported() {
t.Logf("ignoring unexported %s", mType.Name)
continue
}
t.Run(mType.Name, func(t *testing.T) {
m := rVal.MethodByName(mType.Name)
if !m.IsValid() {
t.Errorf("unknown method: %s", mType.Name)
}
assert.NotPanics(t, func() { _ = m.Call(args(mType)) })
})
}
}
func args(m reflect.Method) []reflect.Value {
numIn := m.Type.NumIn() - 1 // Do not include the receiver arg.
if numIn <= 0 {
return nil
}
if m.Type.IsVariadic() {
numIn--
}
out := make([]reflect.Value, numIn)
for i := range out {
aType := m.Type.In(i + 1) // Skip receiver arg.
out[i] = reflect.New(aType).Elem()
}
return out
}
func BenchmarkFiltering(b *testing.B) {
var kvs [26]attribute.KeyValue
buf := [1]byte{'A' - 1}
for i := range kvs {
buf[0]++ // A, B, C ... Z
kvs[i] = attribute.String(string(buf[:]), "")
}
var result struct {
set attribute.Set
dropped []attribute.KeyValue
}
benchFn := func(fltr attribute.Filter) func(*testing.B) {
return func(b *testing.B) {
b.Helper()
b.Run("Set.Filter", func(b *testing.B) {
s := attribute.NewSet(kvs[:]...)
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result.set, result.dropped = s.Filter(fltr)
}
})
b.Run("NewSetWithFiltered", func(b *testing.B) {
attrs := kvs[:]
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
result.set, result.dropped = attribute.NewSetWithFiltered(attrs, fltr)
}
})
}
}
b.Run("NoFilter", benchFn(nil))
b.Run("NoFiltered", benchFn(func(attribute.KeyValue) bool { return true }))
b.Run("Filtered", benchFn(func(kv attribute.KeyValue) bool { return kv.Key == "A" }))
b.Run("AllDropped", benchFn(func(attribute.KeyValue) bool { return false }))
}