-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathcontext_test.go
64 lines (55 loc) · 1.55 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
64
package tracing
import (
"context"
"testing"
)
// nopSpan has no fields so all values are equal, adding an int allows us to
// differentiate
type mockSpan struct {
nopSpan
id int
}
func TestSpanContextAPIs(t *testing.T) {
parent := mockSpan{id: 1}
child := mockSpan{id: 2}
ctx := context.Background()
span, ok := GetSpan(ctx)
if ok {
t.Error("should have no span at the start but it did")
}
// set & expect parent
ctx = WithSpan(ctx, parent)
span, _ = GetSpan(ctx)
if actual, ok := span.(mockSpan); !ok || parent != actual {
t.Errorf("span %d != %d", parent.id, actual.id)
}
// set & expect child
ctx = WithSpan(ctx, child)
span, _ = GetSpan(ctx)
if actual, ok := span.(mockSpan); !ok || child != actual {
t.Errorf("span %d != %d", child.id, actual.id)
}
// pop, expect popped child, with parent remaining
ctx, span = PopSpan(ctx)
if actual, ok := span.(mockSpan); !ok || child != actual {
t.Errorf("span %d != %d", child.id, actual.id)
}
span, _ = GetSpan(ctx)
if actual, ok := span.(mockSpan); !ok || parent != actual {
t.Errorf("span %d != %d", parent.id, actual.id)
}
// pop, expect popped parent, with no span remaining
ctx, span = PopSpan(ctx)
if actual, ok := span.(mockSpan); !ok || parent != actual {
t.Errorf("span %d != %d", parent.id, actual.id)
}
span, ok = GetSpan(ctx)
if ok {
t.Error("should have no span at the end but it did")
}
// pop, expect it to be a nop since nothing is left
ctx, span = PopSpan(ctx)
if _, ok := span.(nopSpan); !ok {
t.Errorf("should have been nop span on last pop but was %T", span)
}
}