github.com/blend/go-sdk@v1.20220411.3/grpcutil/rpc_stream_message_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 "encoding/json" 13 "fmt" 14 "testing" 15 "time" 16 17 "github.com/blend/go-sdk/assert" 18 "github.com/blend/go-sdk/logger" 19 ) 20 21 func TestRPCStreamMessageEvent(t *testing.T) { 22 assert := assert.New(t) 23 24 re := NewRPCStreamMessageEvent("/v1.foo", StreamMessageDirectionReceive, time.Second, 25 OptRPCStreamMessageAuthority("event-authority"), 26 OptRPCStreamMessageContentType("event-content-type"), 27 OptRPCStreamMessageElapsed(time.Millisecond), 28 OptRPCStreamMessageEngine("event-engine"), 29 OptRPCStreamMessageErr(fmt.Errorf("test error")), 30 OptRPCStreamMessageMethod("/v1.bar"), 31 OptRPCStreamMessagePeer("event-peer"), 32 OptRPCStreamMessageUserAgent("event-user-agent"), 33 OptRPCStreamMessageDirection(StreamMessageDirectionSend), 34 ) 35 36 assert.Equal("event-authority", re.Authority) 37 assert.Equal("event-content-type", re.ContentType) 38 assert.Equal(time.Millisecond, re.Elapsed) 39 assert.Equal("event-engine", re.Engine) 40 assert.Equal(fmt.Errorf("test error"), re.Err) 41 assert.Equal("/v1.bar", re.Method) 42 assert.Equal("event-peer", re.Peer) 43 assert.Equal("event-user-agent", re.UserAgent) 44 assert.Equal(StreamMessageDirectionSend, re.Direction) 45 46 buf := new(bytes.Buffer) 47 noColor := logger.TextOutputFormatter{ 48 NoColor: true, 49 } 50 51 re.WriteText(noColor, buf) 52 assert.Equal("[event-engine] /v1.bar send event-peer event-authority event-user-agent event-content-type 1ms failed", buf.String()) 53 54 contents, err := json.Marshal(re) 55 assert.Nil(err) 56 assert.Contains(string(contents), "event-engine") 57 }