Debian Codegraph @

instrumentation/google.golang.org/grpc/otelgrpc/grpc_test.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelgrpc_test
5
6 import (
7 "context"
8 "net"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 "go.opentelemetry.io/otel/sdk/instrumentation"
14 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
15 "google.golang.org/grpc"
16 "google.golang.org/grpc/credentials/insecure"
17 pb "google.golang.org/grpc/interop/grpc_testing"
18
19 "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
20 "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/internal/test"
21 )
22
23 var wantInstrumentationScope = instrumentation.Scope{
24 Name: "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc",
25 SchemaURL: semconv.SchemaURL,
26 Version: otelgrpc.Version,
27 }
28
29 // newGrpcTest creates a grpc server, starts it, and returns the client, closes everything down during test cleanup.
30 func newGrpcTest(t testing.TB, listener net.Listener, cOpt []grpc.DialOption, sOpt []grpc.ServerOption) pb.TestServiceClient {
31 grpcServer := grpc.NewServer(sOpt...)
32 pb.RegisterTestServiceServer(grpcServer, test.NewTestServer())
33 errCh := make(chan error)
34 go func() {
35 errCh <- grpcServer.Serve(listener)
36 }()
37 t.Cleanup(func() {
38 grpcServer.Stop()
39 assert.NoError(t, <-errCh)
40 })
41
42 cOpt = append(cOpt, grpc.WithTransportCredentials(insecure.NewCredentials()))
43
44 dialAddr := listener.Addr().String()
45
46 if l, ok := listener.(interface{ Dial() (net.Conn, error) }); ok {
47 dial := func(context.Context, string) (net.Conn, error) { return l.Dial() }
48 cOpt = append(cOpt, grpc.WithContextDialer(dial))
49 dialAddr = "passthrough:" + dialAddr
50 }
51
52 conn, err := grpc.NewClient(
53 dialAddr,
54 cOpt...,
55 )
56 require.NoError(t, err)
57 t.Cleanup(func() {
58 assert.NoError(t, conn.Close())
59 })
60
61 return pb.NewTestServiceClient(conn)
62 }
63
64 func doCalls(ctx context.Context, client pb.TestServiceClient) {
65 test.DoEmptyUnaryCall(ctx, client)
66 test.DoLargeUnaryCall(ctx, client)
67 test.DoClientStreaming(ctx, client)
68 test.DoServerStreaming(ctx, client)
69 test.DoPingPong(ctx, client)
70 }