...

Source file src/github.com/opentracing/opentracing-go/gocontext_test.go

Documentation: github.com/opentracing/opentracing-go

     1  package opentracing
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestContextWithSpan(t *testing.T) {
    12  	span := &noopSpan{}
    13  	ctx := ContextWithSpan(context.Background(), span)
    14  	span2 := SpanFromContext(ctx)
    15  	if span != span2 {
    16  		t.Errorf("Not the same span returned from context, expected=%+v, actual=%+v", span, span2)
    17  	}
    18  
    19  	ctx = context.Background()
    20  	span2 = SpanFromContext(ctx)
    21  	if span2 != nil {
    22  		t.Errorf("Expected nil span, found %+v", span2)
    23  	}
    24  
    25  	ctx = ContextWithSpan(ctx, span)
    26  	span2 = SpanFromContext(ctx)
    27  	if span != span2 {
    28  		t.Errorf("Not the same span returned from context, expected=%+v, actual=%+v", span, span2)
    29  	}
    30  
    31  	ctx = ContextWithSpan(ctx, nil)
    32  	if s := SpanFromContext(ctx); s != nil {
    33  		t.Errorf("Not able to reset span in context, expected=nil, actual=%+v", s)
    34  	}
    35  }
    36  
    37  type noopExtTracer struct {
    38  	NoopTracer
    39  }
    40  
    41  type noopExtTracerCtxType struct{}
    42  
    43  func (noopExtTracer) ContextWithSpanHook(ctx context.Context, span Span) context.Context {
    44  	return context.WithValue(ctx, noopExtTracerCtxType{}, noopExtTracerCtxType{})
    45  }
    46  
    47  var _ Tracer = noopExtTracer{}
    48  var _ TracerContextWithSpanExtension = noopExtTracer{}
    49  
    50  type noopExtSpan struct {
    51  	noopSpan
    52  }
    53  
    54  func (noopExtSpan) Tracer() Tracer {
    55  	return noopExtTracer{}
    56  }
    57  
    58  var _ Span = noopExtSpan{}
    59  
    60  func TestContextWithSpanWithExtension(t *testing.T) {
    61  	span := &noopExtSpan{}
    62  	ctx := ContextWithSpan(context.Background(), span)
    63  	span2 := SpanFromContext(ctx)
    64  	if span != span2 {
    65  		t.Errorf("Not the same span returned from context, expected=%+v, actual=%+v", span, span2)
    66  	}
    67  	if _, ok := ctx.Value(noopExtTracerCtxType{}).(noopExtTracerCtxType); !ok {
    68  		t.Error("ContextWithSpanHook was not called")
    69  	}
    70  }
    71  
    72  func TestStartSpanFromContext(t *testing.T) {
    73  	testTracer := testTracer{}
    74  
    75  	// Test the case where there *is* a Span in the Context.
    76  	{
    77  		parentSpan := &testSpan{}
    78  		parentCtx := ContextWithSpan(context.Background(), parentSpan)
    79  		childSpan, childCtx := StartSpanFromContextWithTracer(parentCtx, testTracer, "child")
    80  		if !childSpan.Context().(testSpanContext).HasParent {
    81  			t.Errorf("Failed to find parent: %v", childSpan)
    82  		}
    83  		if !childSpan.(testSpan).Equal(SpanFromContext(childCtx)) {
    84  			t.Errorf("Unable to find child span in context: %v", childCtx)
    85  		}
    86  	}
    87  
    88  	// Test the case where there *is not* a Span in the Context.
    89  	{
    90  		emptyCtx := context.Background()
    91  		childSpan, childCtx := StartSpanFromContextWithTracer(emptyCtx, testTracer, "child")
    92  		if childSpan.Context().(testSpanContext).HasParent {
    93  			t.Errorf("Should not have found parent: %v", childSpan)
    94  		}
    95  		if !childSpan.(testSpan).Equal(SpanFromContext(childCtx)) {
    96  			t.Errorf("Unable to find child span in context: %v", childCtx)
    97  		}
    98  	}
    99  }
   100  
   101  func TestStartSpanFromContextOptions(t *testing.T) {
   102  	testTracer := testTracer{}
   103  
   104  	// Test options are passed to tracer
   105  
   106  	startTime := time.Now().Add(-10 * time.Second) // ten seconds ago
   107  	span, ctx := StartSpanFromContextWithTracer(
   108  		context.Background(), testTracer, "parent", StartTime(startTime), Tag{"component", "test"})
   109  
   110  	assert.Equal(t, "test", span.(testSpan).Tags["component"])
   111  	assert.Equal(t, startTime, span.(testSpan).StartTime)
   112  
   113  	// Test it also works for a child span
   114  
   115  	childStartTime := startTime.Add(3 * time.Second)
   116  	childSpan, _ := StartSpanFromContextWithTracer(
   117  		ctx, testTracer, "child", StartTime(childStartTime))
   118  
   119  	assert.Equal(t, childSpan.(testSpan).Tags["component"], nil)
   120  	assert.Equal(t, childSpan.(testSpan).StartTime, childStartTime)
   121  }
   122  

View as plain text