github.com/cloudwego/kitex@v0.9.0/pkg/utils/thrift_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 utils 18 19 import ( 20 "errors" 21 "testing" 22 23 "github.com/apache/thrift/lib/go/thrift" 24 25 mt "github.com/cloudwego/kitex/internal/mocks/thrift" 26 "github.com/cloudwego/kitex/internal/test" 27 ) 28 29 func TestRPCCodec(t *testing.T) { 30 rc := NewThriftMessageCodec() 31 32 req1 := mt.NewMockReq() 33 req1.Msg = "Hello Kitex" 34 strMap := make(map[string]string) 35 strMap["aa"] = "aa" 36 strMap["bb"] = "bb" 37 req1.StrMap = strMap 38 39 args1 := mt.NewMockTestArgs() 40 args1.Req = req1 41 42 // encode 43 buf, err := rc.Encode("mockMethod", thrift.CALL, 100, args1) 44 test.Assert(t, err == nil, err) 45 46 var argsDecode1 mt.MockTestArgs 47 // decode 48 method, seqID, err := rc.Decode(buf, &argsDecode1) 49 50 test.Assert(t, err == nil) 51 test.Assert(t, method == "mockMethod") 52 test.Assert(t, seqID == 100) 53 test.Assert(t, argsDecode1.Req.Msg == req1.Msg) 54 test.Assert(t, len(argsDecode1.Req.StrMap) == len(req1.StrMap)) 55 for k := range argsDecode1.Req.StrMap { 56 test.Assert(t, argsDecode1.Req.StrMap[k] == req1.StrMap[k]) 57 } 58 59 // *** reuse ThriftMessageCodec 60 req2 := mt.NewMockReq() 61 req2.Msg = "Hello Kitex1" 62 strMap = make(map[string]string) 63 strMap["cc"] = "cc" 64 strMap["dd"] = "dd" 65 req2.StrMap = strMap 66 args2 := mt.NewMockTestArgs() 67 args2.Req = req2 68 // encode 69 buf, err = rc.Encode("mockMethod1", thrift.CALL, 101, args2) 70 test.Assert(t, err == nil, err) 71 72 // decode 73 var argsDecode2 mt.MockTestArgs 74 method, seqID, err = rc.Decode(buf, &argsDecode2) 75 76 test.Assert(t, err == nil, err) 77 test.Assert(t, method == "mockMethod1") 78 test.Assert(t, seqID == 101) 79 test.Assert(t, argsDecode2.Req.Msg == req2.Msg) 80 test.Assert(t, len(argsDecode2.Req.StrMap) == len(req2.StrMap)) 81 for k := range argsDecode2.Req.StrMap { 82 test.Assert(t, argsDecode2.Req.StrMap[k] == req2.StrMap[k]) 83 } 84 } 85 86 func TestSerializer(t *testing.T) { 87 rc := NewThriftMessageCodec() 88 89 req := mt.NewMockReq() 90 req.Msg = "Hello Kitex" 91 strMap := make(map[string]string) 92 strMap["aa"] = "aa" 93 strMap["bb"] = "bb" 94 req.StrMap = strMap 95 96 args := mt.NewMockTestArgs() 97 args.Req = req 98 99 b, err := rc.Serialize(args) 100 test.Assert(t, err == nil, err) 101 102 var args2 mt.MockTestArgs 103 err = rc.Deserialize(&args2, b) 104 test.Assert(t, err == nil, err) 105 106 test.Assert(t, args2.Req.Msg == req.Msg) 107 test.Assert(t, len(args2.Req.StrMap) == len(req.StrMap)) 108 for k := range args2.Req.StrMap { 109 test.Assert(t, args2.Req.StrMap[k] == req.StrMap[k]) 110 } 111 } 112 113 func TestException(t *testing.T) { 114 errMsg := "my error" 115 b := MarshalError("some method", errors.New(errMsg)) 116 test.Assert(t, UnmarshalError(b).Error() == errMsg) 117 }