Skip to content

Commit

Permalink
saving
Browse files Browse the repository at this point in the history
  • Loading branch information
kl943 committed Dec 18, 2024
1 parent dc22b83 commit 8a30d9c
Showing 1 changed file with 135 additions and 96 deletions.
231 changes: 135 additions & 96 deletions transport/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ func (h handler) assertBaggage(ctx context.Context) {
assert.Equal(h.t, "knife", weapon, "baggage should propagate")
}

func createGRPCDispatcher(t *testing.T, tracer opentracing.Tracer) *yarpc.Dispatcher {
func createGRPCDispatcher(t *testing.T, tracer opentracing.Tracer, enableTracingMiddleware bool) *yarpc.Dispatcher {
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
grpcTransport := grpc.NewTransport(grpc.Tracer(tracer))
grpcTransport := grpc.NewTransport(grpc.Tracer(tracer), grpc.TracingInterceptorEnabled(enableTracingMiddleware))
return yarpc.NewDispatcher(yarpc.Config{
Name: "yarpc-test",
Inbounds: yarpc.Inbounds{
Expand All @@ -106,11 +106,9 @@ func createGRPCDispatcher(t *testing.T, tracer opentracing.Tracer) *yarpc.Dispat
})
}

func createHTTPDispatcher(tracer opentracing.Tracer) *yarpc.Dispatcher {
func createHTTPDispatcher(tracer opentracing.Tracer, enableTracingMiddleware bool) *yarpc.Dispatcher {
// TODO: Use port 0 once https://github.com/yarpc/yarpc-go/issues/381 is
// fixed.

httpTransport := http.NewTransport(http.Tracer(tracer))
httpTransport := http.NewTransport(http.Tracer(tracer), http.TracingInterceptorEnabled(enableTracingMiddleware))
dispatcher := yarpc.NewDispatcher(yarpc.Config{
Name: "yarpc-test",
Inbounds: yarpc.Inbounds{
Expand All @@ -127,12 +125,13 @@ func createHTTPDispatcher(tracer opentracing.Tracer) *yarpc.Dispatcher {
}

//lint:ignore U1000 Ignore "method not used" lint as this is invoked by skipped tests.
func createTChannelDispatcher(t *testing.T, tracer opentracing.Tracer) *yarpc.Dispatcher {
func createTChannelDispatcher(t *testing.T, tracer opentracing.Tracer, enableTracingMiddleware bool) *yarpc.Dispatcher {
hp := "127.0.0.1:4040"

tchannelTransport, err := ytchannel.NewChannelTransport(
ytchannel.ListenAddr(hp),
ytchannel.Tracer(tracer),
ytchannel.TracingInterceptorEnabled(enableTracingMiddleware),
// ytchannel.Tracer(tracer),
ytchannel.ServiceName("yarpc-test"),
)
require.NoError(t, err)
Expand All @@ -152,67 +151,94 @@ func createTChannelDispatcher(t *testing.T, tracer opentracing.Tracer) *yarpc.Di
return dispatcher
}

var tests = []struct {
name string
enableTracingMiddleware bool
}{
{
name: "disable-tracing-middleware",
enableTracingMiddleware: false,
},
{
name: "enable-tracing-middleware",
enableTracingMiddleware: true,
},
}

func TestGRPCTracer(t *testing.T) {
tracer := mocktracer.New()
dispatcher := createGRPCDispatcher(t, tracer)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tracer := mocktracer.New()
dispatcher := createGRPCDispatcher(t, tracer, test.enableTracingMiddleware)

client := json.New(dispatcher.ClientConfig("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)
client := json.New(dispatcher.ClientConfig("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)

require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()
require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()

ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()
ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()

err := handler.echo(ctx)
assert.NoError(t, err)
err := handler.echo(ctx)
assert.NoError(t, err)

AssertDepth1Spans(t, tracer)
AssertDepth1Spans(t, tracer)
})
}
}

func TestHTTPTracer(t *testing.T) {
tracer := mocktracer.New()
dispatcher := createHTTPDispatcher(tracer)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tracer := mocktracer.New()
dispatcher := createHTTPDispatcher(tracer, test.enableTracingMiddleware)

client := json.New(dispatcher.ClientConfig("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)
client := json.New(dispatcher.ClientConfig("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)

require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()
require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()

ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()
ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()

err := handler.echo(ctx)
assert.NoError(t, err)
err := handler.echo(ctx)
assert.NoError(t, err)

AssertDepth1Spans(t, tracer)
AssertDepth1Spans(t, tracer)
})
}
}

func TestTChannelTracer(t *testing.T) {
t.Skip("TODO this test is flaky, we need to fix")
tracer := mocktracer.New()
dispatcher := createTChannelDispatcher(t, tracer)
// Make this assertion at the end of the defer stack, when the channel has
// been shut down. This ensures that all message exchanges have been shut
// down, which means that all spans have been closed.
defer AssertDepth1Spans(t, tracer)

client := json.New(dispatcher.ClientConfig("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)

require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()

ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()

err := handler.echo(ctx)
assert.NoError(t, err)
// t.Skip("TODO this test is flaky, we need to fix")

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tracer := mocktracer.New()
dispatcher := createTChannelDispatcher(t, tracer, test.enableTracingMiddleware)
// Make this assertion at the end of the defer stack, when the channel has
// been shut down. This ensures that all message exchanges have been shut
// down, which means that all spans have been closed.
defer AssertDepth1Spans(t, tracer)

client := json.New(dispatcher.ClientConfig("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)

require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()

ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()

err := handler.echo(ctx)
assert.NoError(t, err)
})
}
}

func AssertDepth1Spans(t *testing.T, tracer *mocktracer.MockTracer) {
Expand All @@ -233,64 +259,77 @@ func AssertDepth1Spans(t *testing.T, tracer *mocktracer.MockTracer) {
}

func TestGRPCTracerDepth2(t *testing.T) {
tracer := mocktracer.New()
dispatcher := createGRPCDispatcher(t, tracer)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tracer := mocktracer.New()
dispatcher := createGRPCDispatcher(t, tracer, test.enableTracingMiddleware)

client := json.New(dispatcher.ClientConfig("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)
client := json.New(dispatcher.ClientConfig("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)

require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()
require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()

ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()
ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()

err := handler.echoEcho(ctx)
assert.NoError(t, err)
AssertDepth2Spans(t, tracer)
err := handler.echoEcho(ctx)
assert.NoError(t, err)
AssertDepth2Spans(t, tracer)
})
}
}

func TestHTTPTracerDepth2(t *testing.T) {
tracer := mocktracer.New()
dispatcher := createHTTPDispatcher(tracer)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tracer := mocktracer.New()
dispatcher := createHTTPDispatcher(tracer, test.enableTracingMiddleware)

client := json.New(dispatcher.ClientConfig("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)
client := json.New(dispatcher.ClientConfig("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)

require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()
require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()

ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()
ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()

err := handler.echoEcho(ctx)
assert.NoError(t, err)
AssertDepth2Spans(t, tracer)
err := handler.echoEcho(ctx)
assert.NoError(t, err)
AssertDepth2Spans(t, tracer)
})
}
}

func TestTChannelTracerDepth2(t *testing.T) {
t.Skip("TODO this test is flaky, we need to fix")
tracer := mocktracer.New()
dispatcher := createTChannelDispatcher(t, tracer)
// Make this assertion at the end of the defer stack, when the channel has
// been shut down. This ensures that all message exchanges have been shut
// down, which means that all spans have been closed.
defer AssertDepth2Spans(t, tracer)

client := json.New(dispatcher.ClientConfig("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)

require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()

ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()

err := handler.echoEcho(ctx)
assert.NoError(t, err)
// t.Skip("TODO this test is flaky, we need to fix")

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tracer := mocktracer.New()
dispatcher := createTChannelDispatcher(t, tracer, test.enableTracingMiddleware)
// Make this assertion at the end of the defer stack, when the channel has
// been shut down. This ensures that all message exchanges have been shut
// down, which means that all spans have been closed.
defer AssertDepth2Spans(t, tracer)

client := json.New(dispatcher.ClientConfig("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)

require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()

ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()

err := handler.echoEcho(ctx)
assert.NoError(t, err)
})
}
}

func AssertDepth2Spans(t *testing.T, tracer *mocktracer.MockTracer) {
Expand Down

0 comments on commit 8a30d9c

Please sign in to comment.