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 all 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
8 changes: 3 additions & 5 deletions filters/auth/jwt_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/opentracing/opentracing-go/mocktracer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zalando/skipper/eskip"
Expand All @@ -20,6 +18,7 @@ import (
"github.com/zalando/skipper/proxy"
"github.com/zalando/skipper/proxy/proxytest"
"github.com/zalando/skipper/routing"
"github.com/zalando/skipper/tracing/tracingtest"
)

func TestJwtMetrics(t *testing.T) {
Expand Down Expand Up @@ -275,7 +274,7 @@ func TestJwtMetrics(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
m := &metricstest.MockMetrics{}
defer m.Close()
tracer := mocktracer.New()
tracer := tracingtest.NewTracer()

fr := builtin.MakeRegistry()
fr.Register(auth.NewJwtMetrics())
Expand Down Expand Up @@ -304,8 +303,7 @@ func TestJwtMetrics(t *testing.T) {
resp.Body.Close()
require.Equal(t, tc.status, resp.StatusCode)

// wait for the span to be finished
require.Eventually(t, func() bool { return len(tracer.FinishedSpans()) == 1 }, time.Second, 100*time.Millisecond)
require.Equal(t, 1, len(tracer.FinishedSpans()))

span := tracer.FinishedSpans()[0]
if tc.expectedTag == "" {
Expand Down
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.NewTracer()
pr := proxy.WithParams(proxy.Params{
Routing: rt,
OpenTracing: &proxy.OpenTracingParams{Tracer: tracer},
Expand Down
3 changes: 2 additions & 1 deletion filters/tracing/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/filters/filtertest"
"github.com/zalando/skipper/tracing/tracingtest"
)

func TestTracingTagNil(t *testing.T) {
Expand Down Expand Up @@ -196,7 +197,7 @@ func TestTagCreateFilter(t *testing.T) {
}

func TestTracingTag(t *testing.T) {
tracer := mocktracer.New()
tracer := tracingtest.NewTracer()

for _, ti := range []struct {
name string
Expand Down
30 changes: 4 additions & 26 deletions net/httpclient_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,12 @@ import (
"github.com/lightstep/lightstep-tracer-go"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/sirupsen/logrus"
"github.com/zalando/skipper/net"
"github.com/zalando/skipper/secrets"
"github.com/zalando/skipper/tracing/tracingtest"
)

func waitForSpanViaMockTracer(mockTracer *mocktracer.MockTracer) {
for i := 0; i < 20; i++ {
if n := len(mockTracer.FinishedSpans()); n > 0 {
logrus.Printf("found %d spans", n)
return
}
time.Sleep(100 * time.Millisecond)
}
logrus.Println("no span found")
}

func ExampleTransport() {
tracer := lightstep.NewTracer(lightstep.Options{})

Expand Down Expand Up @@ -221,7 +210,7 @@ func (t *customTracer) StartSpan(operationName string, opts ...opentracing.Start
}

func ExampleClient_customTracer() {
mockTracer := mocktracer.New()
mockTracer := tracingtest.NewTracer()
cli := net.NewClient(net.Options{
Tracer: &customTracer{mockTracer},
OpentracingSpanName: "clientSpan",
Expand All @@ -232,10 +221,6 @@ func ExampleClient_customTracer() {
defer srv.Close()

cli.Get("http://" + srv.Listener.Addr().String() + "/")

// wait for the span to be finished
waitForSpanViaMockTracer(mockTracer)

fmt.Printf("customtag: %s", mockTracer.FinishedSpans()[0].Tags()["customtag"])

// Output:
Expand Down Expand Up @@ -343,7 +328,7 @@ func ExampleClient_hostSecret() {
}

func ExampleClient_withBeforeSendHook() {
mockTracer := mocktracer.New()
mockTracer := tracingtest.NewTracer()
peerService := "my-peer-service"
cli := net.NewClient(net.Options{
Tracer: &customTracer{mockTracer},
Expand All @@ -369,10 +354,6 @@ func ExampleClient_withBeforeSendHook() {
defer srv.Close()

cli.Get("http://" + srv.Listener.Addr().String() + "/")

// wait for the span to be finished
waitForSpanViaMockTracer(mockTracer)

fmt.Printf("request tag %q set to %q", string(ext.PeerService), mockTracer.FinishedSpans()[0].Tags()[string(ext.PeerService)])

// Output:
Expand All @@ -381,7 +362,7 @@ func ExampleClient_withBeforeSendHook() {
}

func ExampleClient_withAfterResponseHook() {
mockTracer := mocktracer.New()
mockTracer := tracingtest.NewTracer()
cli := net.NewClient(net.Options{
Tracer: &customTracer{mockTracer},
OpentracingComponentTag: "testclient",
Expand Down Expand Up @@ -411,9 +392,6 @@ func ExampleClient_withAfterResponseHook() {
log.Fatalf("Failed to get: %v", err)
}

// wait for the span to be finished
waitForSpanViaMockTracer(mockTracer)

fmt.Printf("response code: %d\n", rsp.StatusCode)
fmt.Printf("span status.code: %d", mockTracer.FinishedSpans()[0].Tags()["status.code"])

Expand Down
4 changes: 2 additions & 2 deletions net/httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"github.com/AlexanderYastrebov/noleak"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/zalando/skipper/secrets"
"github.com/zalando/skipper/tracing/tracers/basic"
"github.com/zalando/skipper/tracing/tracingtest"
)

var testToken = []byte("mytoken1")
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestClient(t *testing.T) {
}

func TestTransport(t *testing.T) {
mtracer := mocktracer.New()
mtracer := tracingtest.NewTracer()
tracer, err := basic.InitTracer(nil)
if err != nil {
t.Fatalf("Failed to get a tracer: %v", err)
Expand Down
Loading
Loading