github.com/cloudwego/kitex@v0.9.0/internal/stream/tracing_test.go (about) 1 /* 2 * Copyright 2024 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package stream 18 19 import ( 20 "context" 21 "errors" 22 "testing" 23 24 "github.com/cloudwego/kitex/internal/test" 25 "github.com/cloudwego/kitex/pkg/stats" 26 "github.com/cloudwego/kitex/pkg/streaming" 27 ) 28 29 type mockStream struct { 30 streaming.Stream 31 } 32 33 func (m *mockStream) Context() context.Context { 34 return context.Background() 35 } 36 37 func TestStreamingConfig_streamRecvTraceMW(t *testing.T) { 38 recvErr := errors.New("XXX") 39 handlerExecuted := false 40 c := &StreamingConfig{ 41 EventHandler: func(ctx context.Context, evt stats.Event, err error) { 42 test.Assert(t, evt == stats.StreamRecv) 43 test.Assert(t, err == recvErr) 44 handlerExecuted = true 45 }, 46 } 47 mw := c.streamRecvTraceMW(context.Background()) 48 ep := mw(func(stream streaming.Stream, message interface{}) (err error) { 49 return recvErr 50 }) 51 err := ep(&mockStream{}, nil) 52 test.Assert(t, err == recvErr) 53 test.Assert(t, handlerExecuted) 54 } 55 56 func TestStreamingConfig_streamSendTraceMW(t *testing.T) { 57 sendErr := errors.New("XXX") 58 handlerExecuted := false 59 c := &StreamingConfig{ 60 EventHandler: func(ctx context.Context, evt stats.Event, err error) { 61 test.Assert(t, evt == stats.StreamSend) 62 test.Assert(t, err == sendErr) 63 handlerExecuted = true 64 }, 65 } 66 mw := c.streamSendTraceMW(context.Background()) 67 ep := mw(func(stream streaming.Stream, message interface{}) (err error) { 68 return sendErr 69 }) 70 err := ep(&mockStream{}, nil) 71 test.Assert(t, err == sendErr) 72 test.Assert(t, handlerExecuted) 73 }