github.com/blend/go-sdk@v1.20220411.3/grpcutil/rpc_event_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  	"bytes"
    12  	"context"
    13  	"encoding/json"
    14  	"fmt"
    15  	"testing"
    16  	"time"
    17  
    18  	"google.golang.org/grpc/codes"
    19  	"google.golang.org/grpc/status"
    20  
    21  	"github.com/blend/go-sdk/assert"
    22  	"github.com/blend/go-sdk/logger"
    23  )
    24  
    25  func TestRPCEvent(t *testing.T) {
    26  	assert := assert.New(t)
    27  
    28  	re := NewRPCEvent("/v1.foo", time.Second,
    29  		OptRPCAuthority("event-authority"),
    30  		OptRPCContentType("event-content-type"),
    31  		OptRPCElapsed(time.Millisecond),
    32  		OptRPCEngine("event-engine"),
    33  		OptRPCErr(fmt.Errorf("test error")),
    34  		OptRPCMethod("/v1.bar"),
    35  		OptRPCPeer("event-peer"),
    36  		OptRPCUserAgent("event-user-agent"),
    37  	)
    38  
    39  	assert.Equal("event-authority", re.Authority)
    40  	assert.Equal("event-content-type", re.ContentType)
    41  	assert.Equal(time.Millisecond, re.Elapsed)
    42  	assert.Equal("event-engine", re.Engine)
    43  	assert.Equal(fmt.Errorf("test error"), re.Err)
    44  	assert.Equal("/v1.bar", re.Method)
    45  	assert.Equal("event-peer", re.Peer)
    46  	assert.Equal("event-user-agent", re.UserAgent)
    47  
    48  	buf := new(bytes.Buffer)
    49  	noColor := logger.TextOutputFormatter{
    50  		NoColor: true,
    51  	}
    52  
    53  	re.WriteText(noColor, buf)
    54  	assert.Equal("[event-engine] /v1.bar event-peer event-authority event-user-agent event-content-type 1ms failed", buf.String())
    55  
    56  	contents, err := json.Marshal(re)
    57  	assert.Nil(err)
    58  	assert.Contains(string(contents), "event-engine")
    59  }
    60  
    61  func TestRPCEvent_StatusCode(t *testing.T) {
    62  	assert := assert.New(t)
    63  
    64  	re := NewRPCEvent("/v1.foo", time.Second,
    65  		OptRPCAuthority("event-authority"),
    66  		OptRPCContentType("event-content-type"),
    67  		OptRPCElapsed(time.Millisecond),
    68  		OptRPCEngine("event-engine"),
    69  		OptRPCErr(status.Error(codes.ResourceExhausted, "done goofed kid")),
    70  		OptRPCMethod("/v1.bar"),
    71  		OptRPCPeer("event-peer"),
    72  		OptRPCUserAgent("event-user-agent"),
    73  	)
    74  
    75  	buf := new(bytes.Buffer)
    76  	noColor := logger.TextOutputFormatter{
    77  		NoColor: true,
    78  	}
    79  
    80  	re.WriteText(noColor, buf)
    81  	assert.Equal("[event-engine] /v1.bar event-peer event-authority event-user-agent event-content-type 1ms failed (8 - ResourceExhausted)", buf.String())
    82  
    83  	contents, err := json.Marshal(re)
    84  	assert.Nil(err)
    85  	assert.Contains(string(contents), "event-engine")
    86  }
    87  
    88  func TestRPCEventListener(t *testing.T) {
    89  	assert := assert.New(t)
    90  
    91  	re := NewRPCEvent("/v1.foo", time.Second)
    92  
    93  	var didCall bool
    94  	ml := NewRPCEventListener(func(ctx context.Context, e RPCEvent) {
    95  		didCall = true
    96  	})
    97  	ml(context.Background(), re)
    98  	assert.True(didCall)
    99  }