github.com/blend/go-sdk@v1.20220411.3/grpcutil/logged_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package grpcutil
     9  
    10  import (
    11  	"context"
    12  	"sync"
    13  	"testing"
    14  
    15  	"google.golang.org/grpc"
    16  
    17  	"github.com/blend/go-sdk/assert"
    18  	"github.com/blend/go-sdk/logger"
    19  )
    20  
    21  func TestLoggedClientUnary(t *testing.T) {
    22  	assert := assert.New(t)
    23  
    24  	log := logger.All()
    25  	defer log.Close()
    26  
    27  	events := make(chan RPCEvent, 1)
    28  	var wg sync.WaitGroup
    29  	wg.Add(1)
    30  	log.Listen(FlagRPC, "test", func(_ context.Context, e logger.Event) {
    31  		wg.Done()
    32  		events <- e.(RPCEvent)
    33  	})
    34  	interceptor := LoggedClientUnary(log)
    35  
    36  	//func(ctx context.Context, method string, req interface{}, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
    37  	err := interceptor(context.TODO(), "/example-string/v1/dog", "treats", nil, nil, grpc.UnaryInvoker(func(_ context.Context, _ string, _, _ interface{}, _ *grpc.ClientConn, opts ...grpc.CallOption) error {
    38  		return nil
    39  	}))
    40  	assert.Nil(err)
    41  
    42  	wg.Wait()
    43  
    44  	assert.NotEmpty(events)
    45  	got := <-events
    46  	assert.Equal("/example-string/v1/dog", got.Method)
    47  }