Skip to content

Commit

Permalink
adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhlidd committed Nov 8, 2024
1 parent 687c23f commit 4d35314
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions ddtrace/tracer/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,41 @@ func TestStartSpanFromContext(t *testing.T) {
assert.Equal("/", got.Resource)
}

func TestStartSpanWithSpanLinks(t *testing.T) {
_, _, _, stop := startTestTracer(t)
defer stop()
spanLink := ddtrace.SpanLink{TraceID: 789, TraceIDHigh: 0, SpanID: 789, Attributes: map[string]string{"reason": "terminated_context", "context_headers": "datadog"}, Flags: 0}
var ctx ddtrace.SpanContext
ctx = &spanContext{spanID: 789, traceID: traceIDFrom64Bits(789), spanLinks: []ddtrace.SpanLink{spanLink}}

t.Run("spanContext with spanLinks satisfies SpanContextWithLinks interface", func(t *testing.T) {
ctxLink, ok := ctx.(ddtrace.SpanContextWithLinks)
assert.True(t, ok)
assert.Equal(t, len(ctxLink.SpanLinks()), 1)
assert.Equal(t, ctxLink.SpanLinks()[0], spanLink)
})

t.Run("create span from spancontext with links", func(t *testing.T) {
var s ddtrace.Span
s, _ = StartSpanFromContext(
context.Background(),
"http.request",
WithSpanLinks([]ddtrace.SpanLink{spanLink}),
ChildOf(ctx),
)
//checking that a span links are added to a child span that is created where span links are passed as an StartSpanOption
sp, ok := s.(*span)
if !ok {
assert.Fail(t, "couldn't cast to span")
}

assert.Equal(t, 1, len(sp.SpanLinks))
assert.Equal(t, spanLink, sp.SpanLinks[0])

assert.Equal(t, 0, len(sp.context.spanLinks)) // ensure that the span links are not added to the parent context
})
}

func TestStartSpanFromContextRace(t *testing.T) {
_, _, _, stop := startTestTracer(t)
defer stop()
Expand Down

0 comments on commit 4d35314

Please sign in to comment.