go.undefinedlabs.com/scopeagent@v0.4.2/tracer/api_test.go (about)

     1  package tracer
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/uuid"
     7  	ot "github.com/opentracing/opentracing-go"
     8  	"github.com/opentracing/opentracing-go/harness"
     9  )
    10  
    11  // newTracer creates a new tracer for each test, and returns a nil cleanup function.
    12  func newTracer() (tracer ot.Tracer, closer func()) {
    13  	tracer = NewWithOptions(Options{
    14  		Recorder:     NewInMemoryRecorder(),
    15  		ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
    16  	})
    17  	return tracer, nil
    18  }
    19  
    20  func TestAPICheck(t *testing.T) {
    21  	harness.RunAPIChecks(t,
    22  		newTracer,
    23  		harness.CheckEverything(),
    24  		harness.UseProbe(apiCheckProbe{}),
    25  	)
    26  }
    27  
    28  // implements harness.APICheckProbe
    29  type apiCheckProbe struct{}
    30  
    31  // SameTrace helps tests assert that this tracer's spans are from the same trace.
    32  func (apiCheckProbe) SameTrace(first, second ot.Span) bool {
    33  	span1, ok := first.(*spanImpl)
    34  	if !ok { // some other tracer's span
    35  		return false
    36  	}
    37  	span2, ok := second.(*spanImpl)
    38  	if !ok { // some other tracer's span
    39  		return false
    40  	}
    41  	return span1.raw.Context.TraceID == span2.raw.Context.TraceID
    42  }
    43  
    44  // SameSpanContext helps tests assert that a span and a context are from the same trace and span.
    45  func (apiCheckProbe) SameSpanContext(span ot.Span, spanContext ot.SpanContext) bool {
    46  	sp, ok := span.(*spanImpl)
    47  	if !ok {
    48  		return false
    49  	}
    50  	ctx, ok := spanContext.(SpanContext)
    51  	if !ok {
    52  		return false
    53  	}
    54  	return sp.raw.Context.TraceID == ctx.TraceID && sp.raw.Context.SpanID == ctx.SpanID
    55  }