-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext_test.go
More file actions
320 lines (273 loc) · 8.45 KB
/
context_test.go
File metadata and controls
320 lines (273 loc) · 8.45 KB
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
package opik
import (
"context"
"testing"
"time"
)
func TestContextWithTrace(t *testing.T) {
ctx := context.Background()
trace := &Trace{
id: "test-trace-id",
name: "test-trace",
startTime: time.Now(),
}
newCtx := ContextWithTrace(ctx, trace)
// Verify trace is in new context
retrievedTrace := TraceFromContext(newCtx)
if retrievedTrace == nil {
t.Fatal("TraceFromContext returned nil")
}
if retrievedTrace.ID() != trace.ID() {
t.Errorf("TraceFromContext ID = %q, want %q", retrievedTrace.ID(), trace.ID())
}
if retrievedTrace.Name() != trace.Name() {
t.Errorf("TraceFromContext Name = %q, want %q", retrievedTrace.Name(), trace.Name())
}
}
func TestTraceFromContextEmpty(t *testing.T) {
ctx := context.Background()
trace := TraceFromContext(ctx)
if trace != nil {
t.Errorf("TraceFromContext on empty context = %v, want nil", trace)
}
}
func TestContextWithSpan(t *testing.T) {
ctx := context.Background()
span := &Span{
id: "test-span-id",
traceID: "test-trace-id",
name: "test-span",
startTime: time.Now(),
}
newCtx := ContextWithSpan(ctx, span)
// Verify span is in new context
retrievedSpan := SpanFromContext(newCtx)
if retrievedSpan == nil {
t.Fatal("SpanFromContext returned nil")
}
if retrievedSpan.ID() != span.ID() {
t.Errorf("SpanFromContext ID = %q, want %q", retrievedSpan.ID(), span.ID())
}
if retrievedSpan.TraceID() != span.TraceID() {
t.Errorf("SpanFromContext TraceID = %q, want %q", retrievedSpan.TraceID(), span.TraceID())
}
if retrievedSpan.Name() != span.Name() {
t.Errorf("SpanFromContext Name = %q, want %q", retrievedSpan.Name(), span.Name())
}
}
func TestSpanFromContextEmpty(t *testing.T) {
ctx := context.Background()
span := SpanFromContext(ctx)
if span != nil {
t.Errorf("SpanFromContext on empty context = %v, want nil", span)
}
}
func TestContextWithClient(t *testing.T) {
ctx := context.Background()
client := &Client{
config: &Config{
URL: "http://localhost:5173/api",
Workspace: "test",
},
}
newCtx := ContextWithClient(ctx, client)
// Verify client is in new context
retrievedClient := ClientFromContext(newCtx)
if retrievedClient == nil {
t.Fatal("ClientFromContext returned nil")
}
if retrievedClient.config.URL != client.config.URL {
t.Errorf("ClientFromContext URL = %q, want %q", retrievedClient.config.URL, client.config.URL)
}
}
func TestClientFromContextEmpty(t *testing.T) {
ctx := context.Background()
client := ClientFromContext(ctx)
if client != nil {
t.Errorf("ClientFromContext on empty context = %v, want nil", client)
}
}
func TestContextChaining(t *testing.T) {
ctx := context.Background()
trace := &Trace{
id: "trace-1",
name: "my-trace",
startTime: time.Now(),
}
span := &Span{
id: "span-1",
traceID: "trace-1",
name: "my-span",
startTime: time.Now(),
}
client := &Client{
config: &Config{URL: "http://localhost:5173/api"},
}
// Chain all context values
ctx = ContextWithTrace(ctx, trace)
ctx = ContextWithSpan(ctx, span)
ctx = ContextWithClient(ctx, client)
// Verify all values are accessible
if TraceFromContext(ctx) == nil {
t.Error("TraceFromContext returned nil after chaining")
}
if SpanFromContext(ctx) == nil {
t.Error("SpanFromContext returned nil after chaining")
}
if ClientFromContext(ctx) == nil {
t.Error("ClientFromContext returned nil after chaining")
}
}
func TestContextOverwrite(t *testing.T) {
ctx := context.Background()
trace1 := &Trace{id: "trace-1", startTime: time.Now()}
trace2 := &Trace{id: "trace-2", startTime: time.Now()}
ctx = ContextWithTrace(ctx, trace1)
ctx = ContextWithTrace(ctx, trace2)
retrieved := TraceFromContext(ctx)
if retrieved.ID() != "trace-2" {
t.Errorf("TraceFromContext after overwrite = %q, want %q", retrieved.ID(), "trace-2")
}
}
func TestCurrentTraceID(t *testing.T) {
t.Run("from trace", func(t *testing.T) {
ctx := context.Background()
trace := &Trace{id: "trace-123", startTime: time.Now()}
ctx = ContextWithTrace(ctx, trace)
traceID := CurrentTraceID(ctx)
if traceID != "trace-123" {
t.Errorf("CurrentTraceID = %q, want %q", traceID, "trace-123")
}
})
t.Run("from span", func(t *testing.T) {
ctx := context.Background()
span := &Span{id: "span-123", traceID: "trace-456", startTime: time.Now()}
ctx = ContextWithSpan(ctx, span)
traceID := CurrentTraceID(ctx)
if traceID != "trace-456" {
t.Errorf("CurrentTraceID = %q, want %q", traceID, "trace-456")
}
})
t.Run("trace takes precedence", func(t *testing.T) {
ctx := context.Background()
trace := &Trace{id: "trace-primary", startTime: time.Now()}
span := &Span{id: "span-123", traceID: "trace-secondary", startTime: time.Now()}
ctx = ContextWithTrace(ctx, trace)
ctx = ContextWithSpan(ctx, span)
traceID := CurrentTraceID(ctx)
if traceID != "trace-primary" {
t.Errorf("CurrentTraceID = %q, want %q", traceID, "trace-primary")
}
})
t.Run("empty context", func(t *testing.T) {
ctx := context.Background()
traceID := CurrentTraceID(ctx)
if traceID != "" {
t.Errorf("CurrentTraceID on empty context = %q, want empty", traceID)
}
})
}
func TestCurrentSpanID(t *testing.T) {
t.Run("from span", func(t *testing.T) {
ctx := context.Background()
span := &Span{id: "span-123", startTime: time.Now()}
ctx = ContextWithSpan(ctx, span)
spanID := CurrentSpanID(ctx)
if spanID != "span-123" {
t.Errorf("CurrentSpanID = %q, want %q", spanID, "span-123")
}
})
t.Run("empty context", func(t *testing.T) {
ctx := context.Background()
spanID := CurrentSpanID(ctx)
if spanID != "" {
t.Errorf("CurrentSpanID on empty context = %q, want empty", spanID)
}
})
}
func TestEndTraceNoActiveTrace(t *testing.T) {
ctx := context.Background()
err := EndTrace(ctx)
if err != ErrNoActiveTrace {
t.Errorf("EndTrace on empty context = %v, want ErrNoActiveTrace", err)
}
}
func TestEndSpanNoActiveSpan(t *testing.T) {
ctx := context.Background()
err := EndSpan(ctx)
if err != ErrNoActiveSpan {
t.Errorf("EndSpan on empty context = %v, want ErrNoActiveSpan", err)
}
}
func TestStartSpanNoActiveTrace(t *testing.T) {
ctx := context.Background()
_, span, err := StartSpan(ctx, "test-span")
if err != ErrNoActiveTrace {
t.Errorf("StartSpan without trace = %v, want ErrNoActiveTrace", err)
}
if span != nil {
t.Errorf("StartSpan without trace returned span = %v, want nil", span)
}
}
func TestContextKeyType(t *testing.T) {
// Ensure context keys are unique and don't collide with other packages
ctx := context.Background()
// Add values with our keys
ctx = ContextWithTrace(ctx, &Trace{id: "test", startTime: time.Now()})
ctx = ContextWithSpan(ctx, &Span{id: "test", startTime: time.Now()})
ctx = ContextWithClient(ctx, &Client{config: &Config{}})
// Add a value with a different int key (simulating another package)
type otherKey int
ctx = context.WithValue(ctx, otherKey(0), "other-value")
// Our values should still be accessible
if TraceFromContext(ctx) == nil {
t.Error("TraceFromContext returned nil with other key present")
}
if SpanFromContext(ctx) == nil {
t.Error("SpanFromContext returned nil with other key present")
}
if ClientFromContext(ctx) == nil {
t.Error("ClientFromContext returned nil with other key present")
}
}
func TestNilValues(t *testing.T) {
t.Run("nil trace", func(t *testing.T) {
ctx := context.Background()
ctx = ContextWithTrace(ctx, nil)
trace := TraceFromContext(ctx)
if trace != nil {
t.Error("TraceFromContext should return nil for nil trace")
}
})
t.Run("nil span", func(t *testing.T) {
ctx := context.Background()
ctx = ContextWithSpan(ctx, nil)
span := SpanFromContext(ctx)
if span != nil {
t.Error("SpanFromContext should return nil for nil span")
}
})
t.Run("nil client", func(t *testing.T) {
ctx := context.Background()
ctx = ContextWithClient(ctx, nil)
client := ClientFromContext(ctx)
if client != nil {
t.Error("ClientFromContext should return nil for nil client")
}
})
}
func TestContextWithCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
trace := &Trace{id: "test-trace", startTime: time.Now()}
ctx = ContextWithTrace(ctx, trace)
// Trace should be accessible before cancellation
if TraceFromContext(ctx) == nil {
t.Error("TraceFromContext returned nil before cancellation")
}
cancel()
// Trace should still be accessible after cancellation
// (context values are not affected by cancellation)
if TraceFromContext(ctx) == nil {
t.Error("TraceFromContext returned nil after cancellation")
}
}