Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move testTracer struct in tracingtest package and reuse it #3322

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions filters/scheduler/fifo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"testing/iotest"
"time"

"github.com/opentracing/opentracing-go/mocktracer"
"github.com/sirupsen/logrus"

"github.com/zalando/skipper/eskip"
Expand Down Expand Up @@ -296,10 +295,8 @@ func TestFifoWithBody(t *testing.T) {
rt := routing.New(ro)
defer rt.Close()
<-rt.FirstLoad()
tracer := &testTracer{MockTracer: mocktracer.New()}
RomanZavodskikh marked this conversation as resolved.
Show resolved Hide resolved
pr := proxy.WithParams(proxy.Params{
Routing: rt,
OpenTracing: &proxy.OpenTracingParams{Tracer: tracer},
Routing: rt,
})
defer pr.Close()
ts := stdlibhttptest.NewServer(pr)
Expand Down Expand Up @@ -508,10 +505,8 @@ func TestFifo(t *testing.T) {
defer rt.Close()
<-rt.FirstLoad()

tracer := &testTracer{MockTracer: mocktracer.New()}
pr := proxy.WithParams(proxy.Params{
Routing: rt,
OpenTracing: &proxy.OpenTracingParams{Tracer: tracer},
Routing: rt,
})
defer pr.Close()

Expand Down Expand Up @@ -636,10 +631,8 @@ func TestFifoConstantRouteUpdates(t *testing.T) {
defer rt.Close()
<-rt.FirstLoad()

tracer := &testTracer{MockTracer: mocktracer.New()}
pr := proxy.WithParams(proxy.Params{
Routing: rt,
OpenTracing: &proxy.OpenTracingParams{Tracer: tracer},
Routing: rt,
})
defer pr.Close()

Expand Down
38 changes: 2 additions & 36 deletions filters/scheduler/lifo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ import (
"net/http/httptest"
"net/url"
"sync"
"sync/atomic"
"testing"
"time"

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

"github.com/aryszka/jobqueue"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/metrics/metricstest"
"github.com/zalando/skipper/proxy"
"github.com/zalando/skipper/routing"
"github.com/zalando/skipper/routing/testdataclient"
"github.com/zalando/skipper/scheduler"
"github.com/zalando/skipper/tracing/tracingtest"
)

func TestNewLIFO(t *testing.T) {
Expand Down Expand Up @@ -442,38 +440,6 @@ func TestNewLIFO(t *testing.T) {
}
}

type testTracer struct {
*mocktracer.MockTracer
spans int32
}

func (t *testTracer) Reset() {
atomic.StoreInt32(&t.spans, 0)
t.MockTracer.Reset()
}

func (t *testTracer) StartSpan(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span {
atomic.AddInt32(&t.spans, 1)
return t.MockTracer.StartSpan(operationName, opts...)
}

func (t *testTracer) FinishedSpans() []*mocktracer.MockSpan {
timeout := time.After(1 * time.Second)
retry := time.NewTicker(100 * time.Millisecond)
defer retry.Stop()
for {
finished := t.MockTracer.FinishedSpans()
if len(finished) == int(atomic.LoadInt32(&t.spans)) {
return finished
}
select {
case <-retry.C:
case <-timeout:
return nil
}
}
}

func TestLifoErrors(t *testing.T) {
backend := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
time.Sleep(time.Second)
Expand Down Expand Up @@ -508,7 +474,7 @@ func TestLifoErrors(t *testing.T) {

<-rt.FirstLoad()

tracer := &testTracer{MockTracer: mocktracer.New()}
tracer := tracingtest.NewMockTracer()
RomanZavodskikh marked this conversation as resolved.
Show resolved Hide resolved
pr := proxy.WithParams(proxy.Params{
Routing: rt,
OpenTracing: &proxy.OpenTracingParams{Tracer: tracer},
Expand Down
53 changes: 53 additions & 0 deletions tracing/tracingtest/mocktracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tracingtest

import (
"sync/atomic"
"time"

"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
)

type MockTracer struct {
mockTracer *mocktracer.MockTracer
spans atomic.Int32
}

func NewMockTracer() *MockTracer {
RomanZavodskikh marked this conversation as resolved.
Show resolved Hide resolved
return &MockTracer{mockTracer: &mocktracer.MockTracer{}}
}

func (t *MockTracer) Reset() {
t.spans.Store(0)
t.mockTracer.Reset()
}

func (t *MockTracer) StartSpan(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span {
t.spans.Add(1)
return t.mockTracer.StartSpan(operationName, opts...)
}

func (t *MockTracer) FinishedSpans() []*mocktracer.MockSpan {
timeout := time.After(1 * time.Second)
retry := time.NewTicker(100 * time.Millisecond)
defer retry.Stop()
for {
finished := t.mockTracer.FinishedSpans()
if len(finished) == int(t.spans.Load()) {
return finished
}
select {
case <-retry.C:
case <-timeout:
return nil
}
}
}

func (t *MockTracer) Inject(sm opentracing.SpanContext, format interface{}, carrier interface{}) error {
RomanZavodskikh marked this conversation as resolved.
Show resolved Hide resolved
return t.mockTracer.Inject(sm, format, carrier)
}

func (t *MockTracer) Extract(format interface{}, carrier interface{}) (opentracing.SpanContext, error) {
return t.mockTracer.Extract(format, carrier)
}
Loading