File: gocontext_test.go

package info (click to toggle)
golang-github-opentracing-opentracing-go 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, bookworm-proposed-updates, forky, sid, trixie
  • size: 268 kB
  • sloc: makefile: 18
file content (121 lines) | stat: -rw-r--r-- 3,490 bytes parent folder | download | duplicates (2)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package opentracing

import (
	"context"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestContextWithSpan(t *testing.T) {
	span := &noopSpan{}
	ctx := ContextWithSpan(context.Background(), span)
	span2 := SpanFromContext(ctx)
	if span != span2 {
		t.Errorf("Not the same span returned from context, expected=%+v, actual=%+v", span, span2)
	}

	ctx = context.Background()
	span2 = SpanFromContext(ctx)
	if span2 != nil {
		t.Errorf("Expected nil span, found %+v", span2)
	}

	ctx = ContextWithSpan(ctx, span)
	span2 = SpanFromContext(ctx)
	if span != span2 {
		t.Errorf("Not the same span returned from context, expected=%+v, actual=%+v", span, span2)
	}

	ctx = ContextWithSpan(ctx, nil)
	if s := SpanFromContext(ctx); s != nil {
		t.Errorf("Not able to reset span in context, expected=nil, actual=%+v", s)
	}
}

type noopExtTracer struct {
	NoopTracer
}

type noopExtTracerCtxType struct{}

func (noopExtTracer) ContextWithSpanHook(ctx context.Context, span Span) context.Context {
	return context.WithValue(ctx, noopExtTracerCtxType{}, noopExtTracerCtxType{})
}

var _ Tracer = noopExtTracer{}
var _ TracerContextWithSpanExtension = noopExtTracer{}

type noopExtSpan struct {
	noopSpan
}

func (noopExtSpan) Tracer() Tracer {
	return noopExtTracer{}
}

var _ Span = noopExtSpan{}

func TestContextWithSpanWithExtension(t *testing.T) {
	span := &noopExtSpan{}
	ctx := ContextWithSpan(context.Background(), span)
	span2 := SpanFromContext(ctx)
	if span != span2 {
		t.Errorf("Not the same span returned from context, expected=%+v, actual=%+v", span, span2)
	}
	if _, ok := ctx.Value(noopExtTracerCtxType{}).(noopExtTracerCtxType); !ok {
		t.Error("ContextWithSpanHook was not called")
	}
}

func TestStartSpanFromContext(t *testing.T) {
	testTracer := testTracer{}

	// Test the case where there *is* a Span in the Context.
	{
		parentSpan := &testSpan{}
		parentCtx := ContextWithSpan(context.Background(), parentSpan)
		childSpan, childCtx := StartSpanFromContextWithTracer(parentCtx, testTracer, "child")
		if !childSpan.Context().(testSpanContext).HasParent {
			t.Errorf("Failed to find parent: %v", childSpan)
		}
		if !childSpan.(testSpan).Equal(SpanFromContext(childCtx)) {
			t.Errorf("Unable to find child span in context: %v", childCtx)
		}
	}

	// Test the case where there *is not* a Span in the Context.
	{
		emptyCtx := context.Background()
		childSpan, childCtx := StartSpanFromContextWithTracer(emptyCtx, testTracer, "child")
		if childSpan.Context().(testSpanContext).HasParent {
			t.Errorf("Should not have found parent: %v", childSpan)
		}
		if !childSpan.(testSpan).Equal(SpanFromContext(childCtx)) {
			t.Errorf("Unable to find child span in context: %v", childCtx)
		}
	}
}

func TestStartSpanFromContextOptions(t *testing.T) {
	testTracer := testTracer{}

	// Test options are passed to tracer

	startTime := time.Now().Add(-10 * time.Second) // ten seconds ago
	span, ctx := StartSpanFromContextWithTracer(
		context.Background(), testTracer, "parent", StartTime(startTime), Tag{"component", "test"})

	assert.Equal(t, "test", span.(testSpan).Tags["component"])
	assert.Equal(t, startTime, span.(testSpan).StartTime)

	// Test it also works for a child span

	childStartTime := startTime.Add(3 * time.Second)
	childSpan, _ := StartSpanFromContextWithTracer(
		ctx, testTracer, "child", StartTime(childStartTime))

	assert.Equal(t, childSpan.(testSpan).Tags["component"], nil)
	assert.Equal(t, childSpan.(testSpan).StartTime, childStartTime)
}