...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package trace
16
17 import (
18 "context"
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22 )
23
24 type testSpan struct {
25 noopSpan
26
27 ID byte
28 Remote bool
29 }
30
31 func (s testSpan) SpanContext() SpanContext {
32 return SpanContext{
33 traceID: [16]byte{1},
34 spanID: [8]byte{s.ID},
35 remote: s.Remote,
36 }
37 }
38
39 var (
40 emptySpan = noopSpan{}
41 localSpan = testSpan{ID: 1, Remote: false}
42 remoteSpan = testSpan{ID: 1, Remote: true}
43 wrappedSpan = nonRecordingSpan{sc: remoteSpan.SpanContext()}
44 )
45
46 func TestSpanFromContext(t *testing.T) {
47 testCases := []struct {
48 name string
49 context context.Context
50 expectedSpan Span
51 }{
52 {
53 name: "empty context",
54 context: nil,
55 expectedSpan: emptySpan,
56 },
57 {
58 name: "background context",
59 context: context.Background(),
60 expectedSpan: emptySpan,
61 },
62 {
63 name: "local span",
64 context: ContextWithSpan(context.Background(), localSpan),
65 expectedSpan: localSpan,
66 },
67 {
68 name: "remote span",
69 context: ContextWithSpan(context.Background(), remoteSpan),
70 expectedSpan: remoteSpan,
71 },
72 {
73 name: "wrapped remote span",
74 context: ContextWithRemoteSpanContext(context.Background(), remoteSpan.SpanContext()),
75 expectedSpan: wrappedSpan,
76 },
77 {
78 name: "wrapped local span becomes remote",
79 context: ContextWithRemoteSpanContext(context.Background(), localSpan.SpanContext()),
80 expectedSpan: wrappedSpan,
81 },
82 }
83
84 for _, tc := range testCases {
85 t.Run(tc.name, func(t *testing.T) {
86 assert.Equal(t, tc.expectedSpan, SpanFromContext(tc.context))
87
88
89
90 assert.Equal(t, tc.expectedSpan.SpanContext(), SpanContextFromContext(tc.context))
91 })
92 }
93 }
94
View as plain text