go.undefinedlabs.com/scopeagent@v0.4.2/instrumentation/grpc/grpc_test.go (about)

     1  package grpc
     2  
     3  import (
     4  	"context"
     5  	"log"
     6  	"net"
     7  	"os"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/opentracing/opentracing-go/ext"
    12  
    13  	"go.undefinedlabs.com/scopeagent"
    14  	"go.undefinedlabs.com/scopeagent/agent"
    15  	"go.undefinedlabs.com/scopeagent/tracer"
    16  
    17  	"google.golang.org/grpc"
    18  	pb "google.golang.org/grpc/examples/helloworld/helloworld"
    19  )
    20  
    21  const (
    22  	port        = ":50051"
    23  	address     = "localhost:50051"
    24  	defaultName = "world"
    25  )
    26  
    27  var client pb.GreeterClient
    28  var r *tracer.InMemorySpanRecorder
    29  
    30  // server is used to implement helloworld.GreeterServer.
    31  type server struct{}
    32  
    33  // SayHello implements helloworld.GreeterServer
    34  func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    35  	log.Printf("Received: %v", in.GetName())
    36  	return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
    37  }
    38  func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    39  	log.Printf("Received: %v", in.GetName())
    40  	return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
    41  }
    42  
    43  func TestMain(m *testing.M) {
    44  	lis, err := net.Listen("tcp", port)
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  	s := grpc.NewServer(GetServerInterceptors()...)
    49  	pb.RegisterGreeterServer(s, &server{})
    50  	go func() {
    51  		if err := s.Serve(lis); err != nil {
    52  			panic(err)
    53  		}
    54  	}()
    55  
    56  	opts := append([]grpc.DialOption{grpc.WithInsecure()}, GetClientInterceptors()...)
    57  	conn, err := grpc.Dial(address, opts...)
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  	defer conn.Close()
    62  	client = pb.NewGreeterClient(conn)
    63  
    64  	// Test tracer
    65  	r = tracer.NewInMemoryRecorder()
    66  	os.Exit(scopeagent.Run(m, agent.WithRecorders(r)))
    67  }
    68  
    69  func TestGrpc(t *testing.T) {
    70  	ctx := scopeagent.GetContextFromTest(t)
    71  	r.Reset()
    72  
    73  	ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
    74  	defer cancel()
    75  
    76  	ret, err := client.SayHello(ctx, &pb.HelloRequest{Name: defaultName})
    77  	if err != nil {
    78  		t.Fatalf("could not greet: %v", err)
    79  	}
    80  	t.Logf("Greeting: %s", ret.GetMessage())
    81  
    82  	spans := r.GetSpans()
    83  
    84  	if len(spans) != 2 {
    85  		t.Fatalf("there aren't the right number of spans: %d", len(spans))
    86  	}
    87  
    88  	serverSpan := spans[0]
    89  	if serverSpan.Tags[string(ext.SpanKind)] != ext.SpanKindRPCServerEnum {
    90  		t.Fatalf("the server span doesn't have the right span kind")
    91  	}
    92  	if serverSpan.Tags[Status] != "OK" {
    93  		t.Fatalf("the server span doesn't have the right status")
    94  	}
    95  
    96  	clientSpan := spans[1]
    97  	if clientSpan.Tags[string(ext.SpanKind)] != ext.SpanKindRPCClientEnum {
    98  		t.Fatalf("the client span doesn't have the right span kind")
    99  	}
   100  	if clientSpan.Tags[Status] != "OK" {
   101  		t.Fatalf("the client span doesn't have the right status")
   102  	}
   103  }