-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracy_test.go
199 lines (141 loc) · 4.06 KB
/
tracy_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
package tracygo_test
import (
"context"
"testing"
"github.com/Clarilab/tracygo/v2"
)
const (
keyCorrelationID = "X-Correlation-ID"
keyRequestID = "X-Request-ID"
)
func Test_NewContext(t *testing.T) {
t.Parallel()
tracer := tracygo.New()
t.Run("with correlationID", func(t *testing.T) {
t.Parallel()
ctx := tracer.NewContextWithCorrelationID(context.Background(), "Zitronenbaum")
id, ok := ctx.Value(keyCorrelationID).(string)
if !ok {
t.Error("invalid type for correlationID")
}
if id != "Zitronenbaum" {
t.Errorf("expected 'Zitronenbaum', got '%s'", id)
}
})
t.Run("with requestID", func(t *testing.T) {
t.Parallel()
ctx := tracer.NewContextWithRequestID(context.Background(), "Zitronenbaum")
id, ok := ctx.Value(keyRequestID).(string)
if !ok {
t.Error("invalid type for requestID")
}
if id != "Zitronenbaum" {
t.Errorf("expected 'Zitronenbaum', got '%s'", id)
}
})
t.Run("nil context", func(t *testing.T) {
t.Parallel()
ctx := tracer.NewContextWithCorrelationID(nil, "Zitronenbaum") //nolint:staticcheck // intended use for testing
id, ok := ctx.Value("X-Correlation-ID").(string)
if !ok {
t.Error("invalid type for correlationID")
}
if id != "Zitronenbaum" {
t.Errorf("expected 'Zitronenbaum', got '%s'", id)
}
})
}
func Test_FromContext(t *testing.T) {
t.Parallel()
tracer := tracygo.New()
t.Run("correlationID exists", func(t *testing.T) {
t.Parallel()
ctx := context.WithValue(context.Background(), keyCorrelationID, "Zitronenbaum") //nolint:staticcheck,revive // intended use for testing
id := tracer.CorrelationIDFromContext(ctx)
if id != "Zitronenbaum" {
t.Errorf("expected 'Zitronenbaum', got '%s'", id)
}
})
t.Run("correlationID does not exist", func(t *testing.T) {
t.Parallel()
id := tracer.CorrelationIDFromContext(context.Background())
if id != "" {
t.Errorf("expected '', got '%s'", id)
}
})
t.Run("requestID exists", func(t *testing.T) {
t.Parallel()
ctx := context.WithValue(context.Background(), keyRequestID, "Zitronenbaum") //nolint:staticcheck,revive // intended use for testing
id := tracer.RequestIDFromContext(ctx)
if id != "Zitronenbaum" {
t.Errorf("expected 'Zitronenbaum', got '%s'", id)
}
})
t.Run("requestID does not exist", func(t *testing.T) {
t.Parallel()
id := tracer.RequestIDFromContext(context.Background())
if id != "" {
t.Errorf("expected '', got '%s'", id)
}
})
t.Run("nil context", func(t *testing.T) {
t.Parallel()
id := tracer.CorrelationIDFromContext(nil) //nolint:staticcheck // intended use for testing
if id != "" {
t.Errorf("expected '', got '%s'", id)
}
})
}
func Test_GetIDs(t *testing.T) {
t.Parallel()
tracer := tracygo.New()
t.Run("correlationID type", func(t *testing.T) {
t.Parallel()
key := tracer.CorrelationIDKey()
if key != "X-Correlation-ID" {
t.Errorf("expected 'X-Correlation-ID', got '%s'", key)
}
})
t.Run("correlationID string", func(t *testing.T) {
t.Parallel()
key := tracer.CorrelationIDKey()
if key != "X-Correlation-ID" {
t.Errorf("expected 'X-Correlation-ID', got '%s'", key)
}
})
t.Run("requestID type", func(t *testing.T) {
t.Parallel()
key := tracer.RequestIDKey()
if key != "X-Request-ID" {
t.Errorf("expected 'X-Request-ID', got '%s'", key)
}
})
t.Run("requestID string", func(t *testing.T) {
t.Parallel()
key := tracer.RequestIDKey()
if key != "X-Request-ID" {
t.Errorf("expected 'X-Request-ID', got '%s'", key)
}
})
}
func Test_Options(t *testing.T) {
t.Parallel()
tracer := tracygo.New(
tracygo.WithCorrelationID("X-Correlation-Zitronenbaum"),
tracygo.WithRequestID("X-Request-Zitronenbaum"),
)
t.Run("correlationID", func(t *testing.T) {
t.Parallel()
key := tracer.CorrelationIDKey()
if key != "X-Correlation-Zitronenbaum" {
t.Errorf("expected 'X-Correlation-Zitronenbaum', got '%s'", key)
}
})
t.Run("requestID", func(t *testing.T) {
t.Parallel()
key := tracer.RequestIDKey()
if key != "X-Request-Zitronenbaum" {
t.Errorf("expected 'X-Request-Zitronenbaum', got '%s'", key)
}
})
}