-
Notifications
You must be signed in to change notification settings - Fork 0
/
labels_test.go
143 lines (116 loc) · 3.37 KB
/
labels_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
// Copyright 2024 The original author or 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 gslog_test
import (
"context"
"fmt"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"m4o.io/gslog"
)
var _ = Describe("gslog labels", func() {
var ctx context.Context
BeforeEach(func() {
ctx = context.Background()
})
When("context is initialized with bad labels", func() {
BeforeEach(func() {
ctx = gslog.WithLabels(ctx, gslog.LabelPair{})
})
It("should panic when extracting from the context", func() {
Ω(func() {
gslog.ExtractLabels(ctx)
}).Should(PanicWith("invalid label passed to WithLabels()"))
})
})
When("context is initialized with several labels", func() {
BeforeEach(func() {
ctx = gslog.WithLabels(ctx,
gslog.Label("how", "now"),
gslog.Label("brown", "cow"),
)
})
It("they can be extracted from the context", func() {
labels := gslog.ExtractLabels(ctx)
Ω(labels).Should(HaveLen(2))
Ω(labels).Should(HaveKeyWithValue("how", "now"))
Ω(labels).Should(HaveKeyWithValue("brown", "cow"))
})
Context("and a label overridden", func() {
BeforeEach(func() {
ctx = gslog.WithLabels(ctx, gslog.Label("brown", "cat"))
})
It("the overrides can be extracted from the context", func() {
labels := gslog.ExtractLabels(ctx)
Ω(labels).Should(HaveLen(2))
Ω(labels).Should(HaveKeyWithValue("how", "now"))
Ω(labels).Should(HaveKeyWithValue("brown", "cat"))
})
})
})
When("context is initialized with too many labels", func() {
BeforeEach(func() {
ctx = gslog.WithLabels(ctx,
gslog.Label("how", "now"),
gslog.Label("brown", "cow"),
)
for i := 0; i < 64; i++ {
key := fmt.Sprintf("key_%06d", i)
value := fmt.Sprintf("val_%06d", i)
ctx = gslog.WithLabels(ctx,
gslog.Label(key, value),
)
}
})
It("only 64 labels can be obtained from the context", func() {
labels := gslog.ExtractLabels(ctx)
Ω(labels).Should(HaveLen(64))
Ω(labels).Should(HaveKeyWithValue("how", "now"))
Ω(labels).Should(HaveKeyWithValue("brown", "cow"))
})
})
})
const (
count = 10
)
var (
labels map[string]string
ctx context.Context
)
type mockKey struct{}
func init() {
labels = make(map[string]string, count)
for i := 1; i <= count; i++ {
key := fmt.Sprintf("key_%06d", i)
value := fmt.Sprintf("val_%06d", i)
labels[key] = value
}
ctx = context.Background()
for i := 1; i <= count; i++ {
k := fmt.Sprintf("key_%06d", i)
v := fmt.Sprintf("overridden_%06d", i)
ctx = gslog.WithLabels(ctx, gslog.Label(k, v))
ctx = context.WithValue(ctx, mockKey{}, v)
}
for i := 1; i <= count; i++ {
k := fmt.Sprintf("key_%06d", i)
v := fmt.Sprintf("val_%06d", i)
ctx = gslog.WithLabels(ctx, gslog.Label(k, v))
ctx = context.WithValue(ctx, mockKey{}, v)
}
}
func BenchmarkExtractLabels(b *testing.B) {
gslog.ExtractLabels(ctx)
}