From 016b46fd3137a9c72bd581de86949dafa119e983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 9 Nov 2023 08:27:12 +0100 Subject: [PATCH] otelgrpc: Use net.Listen in TestStatsHandler (#4538) --- .../otelgrpc/test/grpc_stats_handler_test.go | 8 +++--- .../grpc/otelgrpc/test/grpc_test.go | 27 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_stats_handler_test.go b/instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_stats_handler_test.go index 4037c8f106d..6768dfcb4d6 100644 --- a/instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_stats_handler_test.go +++ b/instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_stats_handler_test.go @@ -16,13 +16,13 @@ package test import ( "context" + "net" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "google.golang.org/grpc" "google.golang.org/grpc/codes" - "google.golang.org/grpc/test/bufconn" "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" "go.opentelemetry.io/otel/attribute" @@ -47,9 +47,9 @@ func TestStatsHandler(t *testing.T) { serverMetricReader := metric.NewManualReader() serverMP := metric.NewMeterProvider(metric.WithReader(serverMetricReader)) - listener := bufconn.Listen(bufSize) - defer listener.Close() - err := newGrpcTest( + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err, "failed to open port") + err = newGrpcTest( listener, []grpc.DialOption{ grpc.WithStatsHandler(otelgrpc.NewClientHandler( diff --git a/instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_test.go b/instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_test.go index 40d8be75a7d..1c8cc790a2f 100644 --- a/instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_test.go +++ b/instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_test.go @@ -46,8 +46,6 @@ var wantInstrumentationScope = instrumentation.Scope{ Version: otelgrpc.Version(), } -const bufSize = 2048 - // newGrpcTest creats a grpc server, starts it, and executes all the calls, closes everything down. func newGrpcTest(listener net.Listener, cOpt []grpc.DialOption, sOpt []grpc.ServerOption) error { grpcServer := grpc.NewServer(sOpt...) @@ -108,18 +106,19 @@ func TestInterceptors(t *testing.T) { listener, err := net.Listen("tcp", "127.0.0.1:0") require.NoError(t, err, "failed to open port") - err = newGrpcTest(listener, []grpc.DialOption{ - //nolint:staticcheck // Interceptors are deprecated and will be removed in the next release. - grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor( - otelgrpc.WithTracerProvider(clientUnaryTP), - otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents), - )), - //nolint:staticcheck // Interceptors are deprecated and will be removed in the next release. - grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor( - otelgrpc.WithTracerProvider(clientStreamTP), - otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents), - )), - }, + err = newGrpcTest(listener, + []grpc.DialOption{ + //nolint:staticcheck // Interceptors are deprecated and will be removed in the next release. + grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor( + otelgrpc.WithTracerProvider(clientUnaryTP), + otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents), + )), + //nolint:staticcheck // Interceptors are deprecated and will be removed in the next release. + grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor( + otelgrpc.WithTracerProvider(clientStreamTP), + otelgrpc.WithMessageEvents(otelgrpc.ReceivedEvents, otelgrpc.SentEvents), + )), + }, []grpc.ServerOption{ //nolint:staticcheck // Interceptors are deprecated and will be removed in the next release. grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor(