@@ -14,7 +14,7 @@ func TestUserTaskSpan(t *testing.T) {
14
14
bgctx , cancel := context .WithCancel (context .Background ())
15
15
defer cancel ()
16
16
17
- // TODO(hyangah): test pre-existing spans don't cause troubles
17
+ preExistingSpanEnd := StartSpan ( bgctx , " pre-existing span" )
18
18
19
19
buf := new (bytes.Buffer )
20
20
if err := Start (buf ); err != nil {
@@ -27,17 +27,27 @@ func TestUserTaskSpan(t *testing.T) {
27
27
wg .Add (1 )
28
28
go func () {
29
29
defer wg .Done ()
30
- defer end () // EvUserTaskEnd("span0 ")
30
+ defer end () // EvUserTaskEnd("task0 ")
31
31
32
32
WithSpan (ctx , "span0" , func (ctx context.Context ) {
33
33
// EvUserSpanCreate("span0", start)
34
- Log (ctx , "key0" , "0123456789abcdef" ) // EvUserLog("task0", "key0", "0....f")
34
+ WithSpan (ctx , "span1" , func (ctx context.Context ) {
35
+ Log (ctx , "key0" , "0123456789abcdef" ) // EvUserLog("task0", "key0", "0....f")
36
+ })
35
37
// EvUserSpan("span0", end)
36
38
})
37
39
}()
40
+
38
41
wg .Wait ()
42
+
43
+ preExistingSpanEnd ()
44
+ postExistingSpanEnd := StartSpan (bgctx , "post-existing span" )
45
+
39
46
// End of traced execution
40
47
Stop ()
48
+
49
+ postExistingSpanEnd ()
50
+
41
51
saveTrace (t , buf , "TestUserTaskSpan" )
42
52
res , err := trace .Parse (buf , "" )
43
53
if err != nil {
@@ -46,9 +56,10 @@ func TestUserTaskSpan(t *testing.T) {
46
56
47
57
// Check whether we see all user annotation related records in order
48
58
type testData struct {
49
- typ byte
50
- strs []string
51
- args []uint64
59
+ typ byte
60
+ strs []string
61
+ args []uint64
62
+ setLink bool
52
63
}
53
64
54
65
var got []testData
@@ -58,27 +69,40 @@ func TestUserTaskSpan(t *testing.T) {
58
69
switch e .Type {
59
70
case trace .EvUserTaskCreate :
60
71
taskName := e .SArgs [0 ]
61
- got = append (got , testData {trace .EvUserTaskCreate , []string {taskName }, nil })
72
+ got = append (got , testData {trace .EvUserTaskCreate , []string {taskName }, nil , e .Link != nil })
73
+ if e .Link != nil && e .Link .Type != trace .EvUserTaskEnd {
74
+ t .Errorf ("Unexpected linked event %q->%q" , e , e .Link )
75
+ }
62
76
tasks [e .Args [0 ]] = taskName
63
77
case trace .EvUserLog :
64
78
key , val := e .SArgs [0 ], e .SArgs [1 ]
65
79
taskName := tasks [e .Args [0 ]]
66
- got = append (got , testData {trace .EvUserLog , []string {taskName , key , val }, nil })
80
+ got = append (got , testData {trace .EvUserLog , []string {taskName , key , val }, nil , e . Link != nil })
67
81
case trace .EvUserTaskEnd :
68
82
taskName := tasks [e .Args [0 ]]
69
- got = append (got , testData {trace .EvUserTaskEnd , []string {taskName }, nil })
83
+ got = append (got , testData {trace .EvUserTaskEnd , []string {taskName }, nil , e .Link != nil })
84
+ if e .Link != nil && e .Link .Type != trace .EvUserTaskCreate {
85
+ t .Errorf ("Unexpected linked event %q->%q" , e , e .Link )
86
+ }
70
87
case trace .EvUserSpan :
71
88
taskName := tasks [e .Args [0 ]]
72
89
spanName := e .SArgs [0 ]
73
- got = append (got , testData {trace .EvUserSpan , []string {taskName , spanName }, []uint64 {e .Args [1 ]}})
90
+ got = append (got , testData {trace .EvUserSpan , []string {taskName , spanName }, []uint64 {e .Args [1 ]}, e .Link != nil })
91
+ if e .Link != nil && (e .Link .Type != trace .EvUserSpan || e .Link .SArgs [0 ] != spanName ) {
92
+ t .Errorf ("Unexpected linked event %q->%q" , e , e .Link )
93
+ }
74
94
}
75
95
}
76
96
want := []testData {
77
- {trace .EvUserTaskCreate , []string {"task0" }, nil },
78
- {trace .EvUserSpan , []string {"task0" , "span0" }, []uint64 {0 }},
79
- {trace .EvUserLog , []string {"task0" , "key0" , "0123456789abcdef" }, nil },
80
- {trace .EvUserSpan , []string {"task0" , "span0" }, []uint64 {1 }},
81
- {trace .EvUserTaskEnd , []string {"task0" }, nil },
97
+ {trace .EvUserTaskCreate , []string {"task0" }, nil , true },
98
+ {trace .EvUserSpan , []string {"task0" , "span0" }, []uint64 {0 }, true },
99
+ {trace .EvUserSpan , []string {"task0" , "span1" }, []uint64 {0 }, true },
100
+ {trace .EvUserLog , []string {"task0" , "key0" , "0123456789abcdef" }, nil , false },
101
+ {trace .EvUserSpan , []string {"task0" , "span1" }, []uint64 {1 }, true },
102
+ {trace .EvUserSpan , []string {"task0" , "span0" }, []uint64 {1 }, true },
103
+ {trace .EvUserTaskEnd , []string {"task0" }, nil , true },
104
+ {trace .EvUserSpan , []string {"" , "pre-existing span" }, []uint64 {1 }, false },
105
+ {trace .EvUserSpan , []string {"" , "post-existing span" }, []uint64 {0 }, false },
82
106
}
83
107
if ! reflect .DeepEqual (got , want ) {
84
108
t .Errorf ("Got user span related events %+v\n want: %+v" , got , want )
0 commit comments