-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
63 lines (55 loc) · 1.18 KB
/
context_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
package spanner
import (
"context"
"testing"
)
func TestContextData(t *testing.T) {
t.Run("Success", func(t *testing.T) {
trID := NewTraceID()
sID := NewSpanID()
cd := &ContextData{
Trace: &trID,
Span: &sID,
}
ctx := WithContextData(context.Background(), cd)
if ctx == nil {
t.Errorf("unexpected nil value")
return
}
// ensure Value() is covered, too
v := ctx.Value(ContextKey)
if v == nil {
t.Errorf("unexpected nil value")
return
}
if _, ok := v.(ContextData); !ok {
t.Errorf("type mismatch error: wanted %T ; got %T", &ContextData{}, v)
return
}
storedCD := GetContextData(ctx)
if storedCD == nil {
t.Errorf("unexpected nil value")
return
}
if storedCD.Trace == nil || storedCD.Span == nil {
t.Errorf("unexpected nil value")
return
}
if *storedCD.Trace != trID || *storedCD.Span != sID {
t.Errorf("content mismatch error")
return
}
})
t.Run("NilCD", func(t *testing.T) {
ctx := WithContextData(context.Background(), nil)
if ctx == nil {
t.Errorf("unexpected nil value")
return
}
storedCD := GetContextData(ctx)
if storedCD != nil {
t.Errorf("unexpected non-nil value")
return
}
})
}