Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Implement new interfaces for mock types
Browse files Browse the repository at this point in the history
  • Loading branch information
krnowak committed Aug 21, 2019
1 parent 4700b85 commit 9e8b5fd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions mocktracer/mockspan.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,8 @@ func (s *MockSpan) SetOperationName(operationName string) opentracing.Span {
func (s *MockSpan) Tracer() opentracing.Tracer {
return s.tracer
}

// TracerExt1 belongs to the SpanExt1 interface
func (s *MockSpan) TracerExt1() opentracing.TracerExt1 {
return s.tracer
}
13 changes: 13 additions & 0 deletions mocktracer/mocktracer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mocktracer

import (
"context"
"sync"

"github.com/opentracing/opentracing-go"
Expand Down Expand Up @@ -59,6 +60,18 @@ func (t *MockTracer) Reset() {

// StartSpan belongs to the Tracer interface.
func (t *MockTracer) StartSpan(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span {
return t.startMockSpan(operationName, opts...)
}

// StartSpanWithContext belongs to the TracerExt1 interface.
func (t *MockTracer) StartSpanWithContext(ctx context.Context, operationName string, opts ...opentracing.StartSpanOption) (context.Context, opentracing.SpanExt1) {
span := t.startMockSpan(operationName, opts...)
ctx = opentracing.ContextWithSpan(ctx, span)
return ctx, span
}

// StartSpan belongs to the Tracer interface.
func (t *MockTracer) startMockSpan(operationName string, opts ...opentracing.StartSpanOption) *MockSpan {
sso := opentracing.StartSpanOptions{}
for _, o := range opts {
o.Apply(&sso)
Expand Down
23 changes: 23 additions & 0 deletions mocktracer/mocktracer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mocktracer

import (
"context"
"net/http"
"reflect"
"sync"
Expand Down Expand Up @@ -34,6 +35,28 @@ func TestMockTracer_StartSpan(t *testing.T) {
assert.Equal(t, child.ParentID, parent.Context().(MockSpanContext).SpanID)
}

func TestMockTracer_StartSpanWithContext(t *testing.T) {
tracer := New()
ctx1, span1 := tracer.StartSpanWithContext(
context.Background(),
"a",
opentracing.Tags(map[string]interface{}{"x": "y"}))

ctx2, span2 := span1.TracerExt1().StartSpanWithContext(
ctx1, "", opentracing.ChildOf(span1.Context()))
span2.Finish()
span1.Finish()
spans := tracer.FinishedSpans()
assert.Equal(t, 2, len(spans))

parent := spans[1]
child := spans[0]
assert.Equal(t, map[string]interface{}{"x": "y"}, parent.Tags())
assert.Equal(t, child.ParentID, parent.Context().(MockSpanContext).SpanID)
assert.Equal(t, parent.Context().(MockSpanContext).SpanID, opentracing.SpanFromContext(ctx1).(*MockSpan).Context().(MockSpanContext).SpanID)
assert.Equal(t, child.Context().(MockSpanContext).SpanID, opentracing.SpanFromContext(ctx2).(*MockSpan).Context().(MockSpanContext).SpanID)
}

func TestMockSpan_SetOperationName(t *testing.T) {
tracer := New()
span := tracer.StartSpan("")
Expand Down

0 comments on commit 9e8b5fd

Please sign in to comment.