github.com/cloudwego/kitex@v0.9.0/pkg/serviceinfo/serviceinfo_test.go (about) 1 /* 2 * Copyright 2021 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 serviceinfo_test 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/cloudwego/kitex/internal/test" 24 "github.com/cloudwego/kitex/pkg/serviceinfo" 25 ) 26 27 func TestServiceInfo(t *testing.T) { 28 si := &serviceinfo.ServiceInfo{ 29 PackageName: "123", 30 Extra: map[string]interface{}{ 31 "PackageName": "456", 32 }, 33 } 34 test.Assert(t, si.GetPackageName() == "123") 35 36 si = &serviceinfo.ServiceInfo{ 37 Extra: map[string]interface{}{ 38 "PackageName": "456", 39 }, 40 } 41 test.Assert(t, si.GetPackageName() == "456") 42 } 43 44 func TestMethodInfo(t *testing.T) { 45 mi := serviceinfo.NewMethodInfo(nil, nil, nil, false) 46 test.Assert(t, mi != nil) 47 48 mh := func(ctx context.Context, handler, args, result interface{}) error { return nil } 49 af := func() interface{} { return "arg" } 50 rf := func() interface{} { return "res" } 51 mi = serviceinfo.NewMethodInfo(mh, af, rf, true) 52 test.Assert(t, mi != nil) 53 test.Assert(t, mi.Handler() != nil) 54 test.Assert(t, mi.NewArgs().(string) == "arg") 55 test.Assert(t, mi.NewResult().(string) == "res") 56 test.Assert(t, mi.OneWay()) 57 } 58 59 func TestPayloadCodec(t *testing.T) { 60 test.Assert(t, serviceinfo.Thrift.String() == "Thrift") 61 test.Assert(t, serviceinfo.Protobuf.String() == "Protobuf") 62 test.Panic(t, func() { 63 _ = serviceinfo.PayloadCodec(111).String() 64 }) 65 } 66 67 func TestNewMethodInfoStreaming(t *testing.T) { 68 t.Run("no-streaming", func(t *testing.T) { 69 mi := serviceinfo.NewMethodInfo(nil, nil, nil, false) 70 test.Assert(t, !mi.IsStreaming()) 71 }) 72 t.Run("streaming-client", func(t *testing.T) { 73 mi := serviceinfo.NewMethodInfo(nil, nil, nil, true, 74 serviceinfo.WithStreamingMode(serviceinfo.StreamingClient)) 75 test.Assert(t, mi.IsStreaming()) 76 test.Assert(t, mi.StreamingMode() == serviceinfo.StreamingClient) 77 }) 78 t.Run("streaming-unary", func(t *testing.T) { 79 mi := serviceinfo.NewMethodInfo(nil, nil, nil, true, 80 serviceinfo.WithStreamingMode(serviceinfo.StreamingUnary)) 81 test.Assert(t, mi.IsStreaming()) 82 test.Assert(t, mi.StreamingMode() == serviceinfo.StreamingUnary) 83 }) 84 t.Run("streaming-unary-compatible-middleware", func(t *testing.T) { 85 mi := serviceinfo.NewMethodInfo(nil, nil, nil, true, 86 serviceinfo.WithStreamingMode(serviceinfo.StreamingUnary)) 87 test.Assert(t, mi.IsStreaming()) 88 test.Assert(t, mi.StreamingMode() == serviceinfo.StreamingUnary) 89 }) 90 }