github.com/cloudwego/kitex@v0.9.0/pkg/remote/bound/transmeta_bound_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 bound 18 19 import ( 20 "context" 21 "errors" 22 "testing" 23 24 "github.com/bytedance/gopkg/cloud/metainfo" 25 "github.com/golang/mock/gomock" 26 27 mocksremote "github.com/cloudwego/kitex/internal/mocks/remote" 28 "github.com/cloudwego/kitex/internal/test" 29 "github.com/cloudwego/kitex/pkg/consts" 30 "github.com/cloudwego/kitex/pkg/remote" 31 "github.com/cloudwego/kitex/pkg/remote/trans/invoke" 32 "github.com/cloudwego/kitex/pkg/rpcinfo" 33 "github.com/cloudwego/kitex/pkg/rpcinfo/remoteinfo" 34 "github.com/cloudwego/kitex/pkg/transmeta" 35 ) 36 37 // TestNewTransMetaHandler test NewTransMetaHandler function and assert the result not nil. 38 func TestNewTransMetaHandler(t *testing.T) { 39 ctrl := gomock.NewController(t) 40 defer ctrl.Finish() 41 42 metaHandler1 := mocksremote.NewMockMetaHandler(ctrl) 43 metaHandler2 := mocksremote.NewMockMetaHandler(ctrl) 44 metaHandler3 := mocksremote.NewMockMetaHandler(ctrl) 45 mhs := []remote.MetaHandler{ 46 metaHandler1, metaHandler2, metaHandler3, 47 } 48 49 handler := NewTransMetaHandler(mhs) 50 51 test.Assert(t, handler != nil) 52 } 53 54 // TestTransMetaHandlerWrite test Write function of transMetaHandler. 55 func TestTransMetaHandlerWrite(t *testing.T) { 56 t.Run("Test transMetahandler Write success", func(t *testing.T) { 57 ctrl := gomock.NewController(t) 58 defer ctrl.Finish() 59 60 metaHandler1 := mocksremote.NewMockMetaHandler(ctrl) 61 metaHandler2 := mocksremote.NewMockMetaHandler(ctrl) 62 metaHandler3 := mocksremote.NewMockMetaHandler(ctrl) 63 64 invokeMessage := invoke.NewMessage(nil, nil) 65 remoteMessage := remote.NewMessage( 66 nil, nil, nil, remote.Call, remote.Client) 67 ctx := context.Background() 68 69 metaHandler1.EXPECT().WriteMeta(gomock.Any(), remoteMessage).DoAndReturn(func(ctx context.Context, msg remote.Message) (context.Context, error) { 70 ctx = context.WithValue(ctx, "key1", "val1") 71 return ctx, nil 72 }).Times(1) 73 metaHandler2.EXPECT().WriteMeta(gomock.Any(), remoteMessage).DoAndReturn(func(ctx context.Context, msg remote.Message) (context.Context, error) { 74 ctx = context.WithValue(ctx, "key2", "val2") 75 return ctx, nil 76 }).Times(1) 77 metaHandler3.EXPECT().WriteMeta(gomock.Any(), remoteMessage).DoAndReturn(func(ctx context.Context, msg remote.Message) (context.Context, error) { 78 ctx = context.WithValue(ctx, "key3", "val3") 79 return ctx, nil 80 }).Times(1) 81 82 mhs := []remote.MetaHandler{ 83 metaHandler1, metaHandler2, metaHandler3, 84 } 85 86 handler := NewTransMetaHandler(mhs) 87 test.Assert(t, handler != nil) 88 89 newctx, err := handler.Write(ctx, invokeMessage, remoteMessage) 90 91 test.Assert(t, err == nil, err) 92 val1 := newctx.Value("key1") 93 test.Assert(t, val1 == "val1") 94 val2 := newctx.Value("key2") 95 test.Assert(t, val2 == "val2") 96 val3 := newctx.Value("key3") 97 test.Assert(t, val3 == "val3") 98 }) 99 100 t.Run("Test TransMetahandler Write with error", func(t *testing.T) { 101 ctrl := gomock.NewController(t) 102 defer ctrl.Finish() 103 104 metaHandler1 := mocksremote.NewMockMetaHandler(ctrl) 105 metaHandler2 := mocksremote.NewMockMetaHandler(ctrl) 106 metaHandler3 := mocksremote.NewMockMetaHandler(ctrl) 107 108 invokeMessage := invoke.NewMessage(nil, nil) 109 remoteMessage := remote.NewMessage( 110 nil, nil, nil, remote.Call, remote.Client) 111 ctx := context.Background() 112 113 metaHandler1.EXPECT().WriteMeta(ctx, remoteMessage).Return(context.Background(), errFoo).Times(1) 114 metaHandler2.EXPECT().WriteMeta(ctx, remoteMessage).Return(context.Background(), nil).Times(0) 115 metaHandler3.EXPECT().WriteMeta(ctx, remoteMessage).Return(context.Background(), nil).Times(0) 116 117 mhs := []remote.MetaHandler{ 118 metaHandler1, metaHandler2, metaHandler3, 119 } 120 121 handler := NewTransMetaHandler(mhs) 122 test.Assert(t, handler != nil) 123 124 ctx, err := handler.Write(context.Background(), invokeMessage, remoteMessage) 125 test.Assert(t, ctx != nil) 126 test.Assert(t, errors.Is(errFoo, err)) 127 }) 128 } 129 130 // TestTransMetaHandlerOnMessage test OnMessage function of transMetaHandler, on client side and on server side. 131 func TestTransMetaHandlerOnMessage(t *testing.T) { 132 t.Run("Test TransMetaHandler OnMessage success client side", func(t *testing.T) { 133 ctrl := gomock.NewController(t) 134 defer ctrl.Finish() 135 136 metaHandler1 := mocksremote.NewMockMetaHandler(ctrl) 137 metaHandler2 := mocksremote.NewMockMetaHandler(ctrl) 138 metaHandler3 := mocksremote.NewMockMetaHandler(ctrl) 139 140 args := remote.NewMessage( 141 nil, nil, nil, remote.Call, remote.Client) 142 result := remote.NewMessage( 143 nil, nil, nil, remote.Reply, remote.Client) 144 ctx := context.Background() 145 146 metaHandler1.EXPECT().ReadMeta(gomock.Any(), result).DoAndReturn(func(ctx context.Context, msg remote.Message) (context.Context, error) { 147 ctx = context.WithValue(ctx, "key1", "val1") 148 return ctx, nil 149 }).Times(1) 150 metaHandler2.EXPECT().ReadMeta(gomock.Any(), result).DoAndReturn(func(ctx context.Context, msg remote.Message) (context.Context, error) { 151 ctx = context.WithValue(ctx, "key2", "val2") 152 return ctx, nil 153 }).Times(1) 154 metaHandler3.EXPECT().ReadMeta(gomock.Any(), result).DoAndReturn(func(ctx context.Context, msg remote.Message) (context.Context, error) { 155 ctx = context.WithValue(ctx, "key3", "val3") 156 return ctx, nil 157 }).Times(1) 158 159 mhs := []remote.MetaHandler{ 160 metaHandler1, metaHandler2, metaHandler3, 161 } 162 163 handler := NewTransMetaHandler(mhs) 164 test.Assert(t, handler != nil) 165 166 newctx, err := handler.OnMessage(ctx, args, result) 167 168 test.Assert(t, err == nil, err) 169 val1 := newctx.Value("key1") 170 test.Assert(t, val1 == "val1") 171 val2 := newctx.Value("key2") 172 test.Assert(t, val2 == "val2") 173 val3 := newctx.Value("key3") 174 test.Assert(t, val3 == "val3") 175 }) 176 177 t.Run("Test TransMetaHandler OnMessage success server side", func(t *testing.T) { 178 ctrl := gomock.NewController(t) 179 defer ctrl.Finish() 180 181 metaHandler1 := mocksremote.NewMockMetaHandler(ctrl) 182 metaHandler2 := mocksremote.NewMockMetaHandler(ctrl) 183 metaHandler3 := mocksremote.NewMockMetaHandler(ctrl) 184 185 ink := rpcinfo.NewInvocation("", "mock") 186 to := remoteinfo.NewRemoteInfo(&rpcinfo.EndpointBasicInfo{}, "") 187 ri := rpcinfo.NewRPCInfo(nil, to, ink, nil, nil) 188 189 args := remote.NewMessage( 190 nil, nil, ri, remote.Call, remote.Server) 191 result := remote.NewMessage( 192 nil, nil, nil, remote.Reply, remote.Server) 193 ctx := context.Background() 194 195 metaHandler1.EXPECT().ReadMeta(ctx, args).Return(context.Background(), nil).Times(1) 196 metaHandler2.EXPECT().ReadMeta(ctx, args).Return(context.Background(), nil).Times(1) 197 metaHandler3.EXPECT().ReadMeta(ctx, args).Return(context.Background(), nil).Times(1) 198 mhs := []remote.MetaHandler{ 199 metaHandler1, metaHandler2, metaHandler3, 200 } 201 202 handler := NewTransMetaHandler(mhs) 203 204 test.Assert(t, handler != nil) 205 ctx, err := handler.OnMessage(ctx, args, result) 206 test.Assert(t, err == nil) 207 test.Assert(t, ctx != nil && args.RPCInfo().To().Method() == ctx.Value(consts.CtxKeyMethod)) 208 }) 209 210 t.Run("Test metainfo transient key", func(t *testing.T) { 211 ctrl := gomock.NewController(t) 212 defer ctrl.Finish() 213 214 metaHandler1 := transmeta.MetainfoServerHandler 215 metaHandler2 := mocksremote.NewMockMetaHandler(ctrl) 216 217 ink := rpcinfo.NewInvocation("", "mock") 218 to := remoteinfo.NewRemoteInfo(&rpcinfo.EndpointBasicInfo{}, "") 219 ri := rpcinfo.NewRPCInfo(nil, to, ink, nil, nil) 220 221 args := remote.NewMessage( 222 nil, nil, ri, remote.Call, remote.Server) 223 result := remote.NewMessage( 224 nil, nil, nil, remote.Reply, remote.Server) 225 ctx := context.Background() 226 227 tk1, tv1 := "tk1", "tv1" 228 tk2, tv2 := "tk2", "tv2" 229 args.TransInfo().PutTransStrInfo(map[string]string{metainfo.PrefixTransient + tk1: tv1}) 230 metaHandler2.EXPECT().ReadMeta(gomock.Any(), args).DoAndReturn(func(ctx context.Context, msg remote.Message) (context.Context, error) { 231 ctx = metainfo.SetMetaInfoFromMap(ctx, map[string]string{metainfo.PrefixTransient + tk2: tv2}) 232 return ctx, nil 233 }).Times(1) 234 mhs := []remote.MetaHandler{ 235 metaHandler1, metaHandler2, 236 } 237 238 handler := NewTransMetaHandler(mhs) 239 240 test.Assert(t, handler != nil) 241 ctx, err := handler.OnMessage(ctx, args, result) 242 test.Assert(t, err == nil) 243 v, ok := metainfo.GetValue(ctx, tk1) 244 test.Assert(t, ok) 245 test.Assert(t, v == tv1) 246 v, ok = metainfo.GetValue(ctx, tk2) 247 test.Assert(t, ok) 248 test.Assert(t, v == tv2) 249 250 kvs := make(map[string]string) 251 metainfo.SaveMetaInfoToMap(ctx, kvs) 252 test.Assert(t, len(kvs) == 0) 253 }) 254 } 255 256 // TestGetValidMsg test getValidMsg function with message of server side and client side. 257 func TestGetValidMsg(t *testing.T) { 258 t.Run("Test getValidMsg server side read args", func(t *testing.T) { 259 args := remote.NewMessage(nil, nil, nil, remote.Call, remote.Server) 260 result := remote.NewMessage(nil, nil, nil, remote.Reply, remote.Server) 261 msg, isServer := getValidMsg(args, result) 262 test.Assert(t, isServer) 263 test.Assert(t, args == msg) 264 }) 265 266 t.Run("Test getValidMsg client side read result", func(t *testing.T) { 267 args := remote.NewMessage(nil, nil, nil, remote.Call, remote.Client) 268 result := remote.NewMessage(nil, nil, nil, remote.Reply, remote.Client) 269 msg, isServer := getValidMsg(args, result) 270 test.Assert(t, !isServer) 271 test.Assert(t, result == msg) 272 }) 273 } 274 275 // TestTransMetaHandlerOnActive test OnActive function of transMetaHandler 276 func TestTransMetaHandlerOnActive(t *testing.T) { 277 ctrl := gomock.NewController(t) 278 defer ctrl.Finish() 279 280 mhs := []remote.MetaHandler{ 281 mocksremote.NewMockMetaHandler(ctrl), 282 } 283 ctx := context.Background() 284 handler := NewTransMetaHandler(mhs) 285 ctx, err := handler.OnActive(ctx, invoke.NewMessage(nil, nil)) 286 test.Assert(t, err == nil) 287 test.Assert(t, ctx != nil) 288 } 289 290 // TestTransMetaHandlerOnRead test OnRead function of transMetaHandler 291 func TestTransMetaHandlerOnRead(t *testing.T) { 292 ctrl := gomock.NewController(t) 293 defer ctrl.Finish() 294 295 mhs := []remote.MetaHandler{ 296 mocksremote.NewMockMetaHandler(ctrl), 297 } 298 ctx := context.Background() 299 handler := NewTransMetaHandler(mhs) 300 ctx, err := handler.OnRead(ctx, invoke.NewMessage(nil, nil)) 301 test.Assert(t, err == nil) 302 test.Assert(t, ctx != nil) 303 } 304 305 // TestTransMetaHandlerOnInactive test OnInactive function of transMetaHandler 306 func TestTransMetaHandlerOnInactive(t *testing.T) { 307 ctrl := gomock.NewController(t) 308 defer ctrl.Finish() 309 310 mhs := []remote.MetaHandler{ 311 mocksremote.NewMockMetaHandler(ctrl), 312 } 313 ctx := context.Background() 314 handler := NewTransMetaHandler(mhs) 315 ctx = handler.OnInactive(ctx, invoke.NewMessage(nil, nil)) 316 test.Assert(t, ctx != nil) 317 }