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

Merged
merged 4 commits into from
Dec 5, 2024
Merged
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