trpc.group/trpc-go/trpc-go@v1.0.3/codec_stream_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package trpc_test 15 16 import ( 17 "context" 18 "net" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 trpcpb "trpc.group/trpc/trpc-protocol/pb/go/trpc" 24 25 trpc "trpc.group/trpc-go/trpc-go" 26 "trpc.group/trpc-go/trpc-go/codec" 27 "trpc.group/trpc-go/trpc-go/errs" 28 ) 29 30 // TestStreamCodecInit tests stream Init frame codec. 31 func TestStreamCodecInit(t *testing.T) { 32 33 msg := trpc.Message(ctx) 34 assert.NotNil(t, msg) 35 36 ctx := trpc.BackgroundContext() 37 assert.NotNil(t, ctx) 38 serverCodec := codec.GetServer("trpc") 39 clientCodec := codec.GetClient("trpc") 40 41 // Client encode 42 frameHead := &trpc.FrameHead{ 43 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 44 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT), 45 StreamID: 100, 46 } 47 initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x53, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 48 0xa, 0x41, 0xa, 0x17, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x65, 0x72, 0x76, 49 0x65, 0x72, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 0x74, 0x72, 0x70, 0x63, 50 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 51 0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f} 52 msg.WithFrameHead(frameHead) 53 msg.WithStreamID(100) 54 msg.WithCallerServiceName("trpc.app.server.service") 55 msg.WithClientRPCName("/trpc.test.helloworld.Greeter/SayHello") 56 laddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10000") 57 assert.Nil(t, err) 58 raddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10001") 59 assert.Nil(t, err) 60 msg.WithLocalAddr(laddr) 61 msg.WithRemoteAddr(raddr) 62 initBuf, err := clientCodec.Encode(msg, nil) 63 assert.Nil(t, err) 64 assert.Equal(t, initResult, initBuf) 65 66 // Client Encode With MetaData 67 initMetaResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x63, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 68 0x0, 0xa, 0x51, 0xa, 0x17, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x65, 0x72, 69 0x76, 0x65, 0x72, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 0x74, 0x72, 70 0x70, 0x63, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 71 0x6c, 0x64, 0x2e, 0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 72 0x6c, 0x6c, 0x6f, 0x2a, 0xe, 0xa, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x12, 0x5, 0x77, 0x6f, 0x72, 73 0x6c, 0x64} 74 msg.WithFrameHead(frameHead) 75 msg.WithStreamID(100) 76 meta := codec.MetaData{"hello": []byte("world")} 77 msg.WithClientMetaData(meta) 78 msg.WithCallerServiceName("trpc.app.server.service") 79 msg.WithClientRPCName("/trpc.test.helloworld.Greeter/SayHello") 80 initBuf, err = clientCodec.Encode(msg, nil) 81 assert.Nil(t, err) 82 assert.Equal(t, initMetaResult, initBuf) 83 84 // server Decode 85 serverCtx := context.Background() 86 _, serverMsg := codec.WithNewMessage(serverCtx) 87 serverMsg.WithLocalAddr(laddr) 88 serverMsg.WithRemoteAddr(raddr) 89 init, err := serverCodec.Decode(serverMsg, initResult) 90 assert.Nil(t, err) 91 assert.Nil(t, init) 92 assert.Equal(t, uint32(100), serverMsg.StreamID()) 93 assert.NotNil(t, serverMsg.CallerService()) 94 95 // server encode 96 ctx = context.Background() 97 _, encodeMsg := codec.WithNewMessage(ctx) 98 serverFrameHead := &trpc.FrameHead{ 99 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 100 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT), 101 StreamID: 100, 102 } 103 encodeMsg.WithFrameHead(serverFrameHead) 104 encodeMsg.WithStreamID(100) 105 errRsp := errs.NewFrameError(errs.RetServerEncodeFail, "server test encode fail") 106 encodeMsg.WithServerRspErr(errRsp) 107 108 serverEncodeData := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x2d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 109 0x0, 0x0, 0x12, 0x1b, 0x8, 0x2, 0x12, 0x17, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x74, 0x65, 110 0x73, 0x74, 0x20, 0x65, 0x6e, 0x63, 111 0x6f, 0x64, 0x65, 0x20, 0x66, 0x61, 0x69, 0x6c} 112 rspBuf, err := serverCodec.Encode(encodeMsg, nil) 113 assert.Nil(t, err) 114 assert.Equal(t, serverEncodeData, rspBuf) 115 116 // Client decode 117 clientCtx := context.Background() 118 _, clientMsg := codec.WithNewMessage(clientCtx) 119 initRsp, err := clientCodec.Decode(clientMsg, serverEncodeData) 120 assert.Nil(t, err) 121 assert.Nil(t, initRsp) 122 assert.Equal(t, uint32(100), clientMsg.StreamID()) 123 assert.NotNil(t, clientMsg.ClientRspErr()) 124 125 } 126 127 // TestStreamCodecData tests stream Data frame codec. 128 func TestStreamCodecData(t *testing.T) { 129 130 msg := trpc.Message(ctx) 131 assert.NotNil(t, msg) 132 133 ctx := trpc.BackgroundContext() 134 assert.NotNil(t, ctx) 135 136 serverCodec := codec.GetServer("trpc") 137 clientCodec := codec.GetClient("trpc") 138 139 // init first, ensuring that init frame is saved 140 initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x59, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0xa, 141 0x47, 0xa, 0x1d, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x74, 0x72, 0x70, 0x63, 0x2d, 0x67, 142 0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x73, 0x65, 143 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 144 0x74, 0x72, 0x70, 0x63, 0x2e, 0x74, 0x65, 0x73, 0x74, 145 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x47, 0x72, 146 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f} 147 // server Decode 148 serverCtx := context.Background() 149 _, serverMsg := codec.WithNewMessage(serverCtx) 150 laddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10000") 151 assert.Nil(t, err) 152 raddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10001") 153 assert.Nil(t, err) 154 serverMsg.WithLocalAddr(laddr) 155 serverMsg.WithRemoteAddr(raddr) 156 init, err := serverCodec.Decode(serverMsg, initResult) 157 assert.Nil(t, err) 158 assert.Nil(t, init) 159 assert.Equal(t, uint32(100), serverMsg.StreamID()) 160 assert.NotNil(t, serverMsg.CallerService()) 161 162 // client Encode 163 frameHead := &trpc.FrameHead{ 164 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 165 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_DATA), 166 StreamID: 100, 167 } 168 msg.WithFrameHead(frameHead) 169 msg.WithStreamID(100) 170 dataResult := []byte{0x9, 0x30, 0x1, 0x2, 0x0, 0x0, 0x0, 0x1b, 0x0, 0x0, 0x0, 171 0x0, 0x0, 0x64, 0x0, 0x0, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64} 172 data := []byte("hello world") 173 dataBuf, err := clientCodec.Encode(msg, data) 174 assert.Nil(t, err) 175 assert.Equal(t, dataBuf, dataResult) 176 177 // Server Decode 178 serverCtx = context.Background() 179 _, serverMsg = codec.WithNewMessage(serverCtx) 180 serverMsg.WithLocalAddr(laddr) 181 serverMsg.WithRemoteAddr(raddr) 182 dataDecode, err := serverCodec.Decode(serverMsg, dataResult) 183 assert.Nil(t, err) 184 assert.Equal(t, dataDecode, data) 185 assert.Equal(t, uint32(100), serverMsg.StreamID()) 186 187 // server Encode 188 ctx = context.Background() 189 _, encodeMsg := codec.WithNewMessage(ctx) 190 encodeMsg.WithLocalAddr(laddr) 191 encodeMsg.WithRemoteAddr(raddr) 192 serverFrameHead := &trpc.FrameHead{ 193 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 194 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_DATA), 195 StreamID: 100, 196 } 197 encodeMsg.WithFrameHead(serverFrameHead) 198 encodeMsg.WithStreamID(100) 199 encodeData := []byte("hi there") 200 serverEncodeData := []byte{0x9, 0x30, 0x1, 0x2, 0x0, 0x0, 0x0, 0x18, 0x0, 0x0, 0x0, 0x0, 0x0, 201 0x64, 0x0, 0x0, 0x68, 0x69, 0x20, 0x74, 0x68, 0x65, 0x72, 0x65} 202 rspBuf, err := serverCodec.Encode(encodeMsg, encodeData) 203 assert.Nil(t, err) 204 assert.Equal(t, serverEncodeData, rspBuf) 205 206 // Client Decode 207 clientCtx := context.Background() 208 _, clientMsg := codec.WithNewMessage(clientCtx) 209 rspData, err := clientCodec.Decode(clientMsg, serverEncodeData) 210 assert.Nil(t, err) 211 assert.Equal(t, uint32(100), clientMsg.StreamID()) 212 assert.Equal(t, string(rspData), "hi there") 213 214 // server Encode with ServerRspErr 215 encodeMsg.WithServerRspErr(errs.NewFrameError(1, "test error")) 216 rspBuf, err = serverCodec.Encode(encodeMsg, encodeData) 217 serverEncodeData = []byte{0x9, 0x30, 0x1, 0x4, 0x0, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 218 0x0, 0x0, 0x8, 0x1, 0x10, 0x1, 0x1a, 0xa, 0x74, 0x65, 0x73, 0x74, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72} 219 assert.Nil(t, err) 220 assert.Equal(t, serverEncodeData, rspBuf) 221 } 222 223 // TestStreamCodecClose tests stream Close frame codec. 224 func TestStreamCodecClose(t *testing.T) { 225 226 msg := trpc.Message(ctx) 227 assert.NotNil(t, msg) 228 229 ctx := trpc.BackgroundContext() 230 assert.NotNil(t, ctx) 231 serverCodec := codec.GetServer("trpc") 232 clientCodec := codec.GetClient("trpc") 233 234 // init first, ensuring that init frame is saved 235 initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x59, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0xa, 236 0x47, 0xa, 0x1d, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x74, 0x72, 0x70, 0x63, 0x2d, 0x67, 237 0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x73, 0x65, 238 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 239 0x74, 0x72, 0x70, 0x63, 0x2e, 0x74, 0x65, 0x73, 0x74, 240 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x47, 0x72, 241 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f} 242 // server Decode 243 serverCtx := context.Background() 244 _, serverMsg := codec.WithNewMessage(serverCtx) 245 laddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10000") 246 assert.Nil(t, err) 247 raddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10001") 248 assert.Nil(t, err) 249 serverMsg.WithLocalAddr(laddr) 250 serverMsg.WithRemoteAddr(raddr) 251 init, err := serverCodec.Decode(serverMsg, initResult) 252 assert.Nil(t, err) 253 assert.Nil(t, init) 254 assert.Equal(t, uint32(100), serverMsg.StreamID()) 255 assert.NotNil(t, serverMsg.CallerService()) 256 257 // client encode Close 258 frameHead := &trpc.FrameHead{ 259 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 260 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE), 261 StreamID: 100, 262 } 263 msg.WithFrameHead(frameHead) 264 msg.WithStreamID(100) 265 close := &trpcpb.TrpcStreamCloseMeta{} 266 close.CloseType = int32(trpcpb.TrpcStreamCloseType_TRPC_STREAM_CLOSE) 267 close.Ret = int32(0) 268 msg.WithStreamFrame(close) 269 closeResult := []byte{0x9, 0x30, 0x1, 0x4, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0} 270 closeBuf, err := clientCodec.Encode(msg, nil) 271 assert.Nil(t, err) 272 assert.Equal(t, closeResult, closeBuf) 273 274 // server Decode Close 275 serverCtx = context.Background() 276 _, serverMsg = codec.WithNewMessage(serverCtx) 277 serverMsg.WithLocalAddr(laddr) 278 serverMsg.WithRemoteAddr(raddr) 279 closeDecode, err := serverCodec.Decode(serverMsg, closeResult) 280 assert.Nil(t, err) 281 assert.Nil(t, closeDecode) 282 assert.Equal(t, uint32(100), serverMsg.StreamID()) 283 284 // server encode Close 285 ctx = context.Background() 286 _, encodeMsg := codec.WithNewMessage(ctx) 287 encodeMsg.WithLocalAddr(laddr) 288 encodeMsg.WithRemoteAddr(raddr) 289 serverFrameHead := &trpc.FrameHead{ 290 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 291 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE), 292 StreamID: 100, 293 } 294 close = &trpcpb.TrpcStreamCloseMeta{} 295 close.CloseType = int32(trpcpb.TrpcStreamCloseType_TRPC_STREAM_CLOSE) 296 close.Ret = int32(0) 297 encodeMsg.WithFrameHead(serverFrameHead) 298 encodeMsg.WithStreamFrame(close) 299 encodeMsg.WithStreamID(100) 300 serverEncodeData := []byte{0x9, 0x30, 0x1, 0x4, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0} 301 rspBuf, err := serverCodec.Encode(encodeMsg, nil) 302 assert.Equal(t, serverEncodeData, rspBuf) 303 assert.Nil(t, err) 304 assert.Equal(t, msg.StreamID(), uint32(100)) 305 306 // Server decode error after encode close 307 serverCtx = context.Background() 308 _, serverMsg = codec.WithNewMessage(serverCtx) 309 serverMsg.WithLocalAddr(laddr) 310 serverMsg.WithRemoteAddr(raddr) 311 closeDecode, err = serverCodec.Decode(serverMsg, closeResult) 312 assert.NotNil(t, err) 313 assert.Nil(t, closeDecode) 314 315 // Client decode close 316 clientCtx := context.Background() 317 _, clientMsg := codec.WithNewMessage(clientCtx) 318 clientMsg.WithLocalAddr(laddr) 319 clientMsg.WithRemoteAddr(raddr) 320 CloseRsp, err := clientCodec.Decode(clientMsg, serverEncodeData) 321 assert.Nil(t, err) 322 assert.Nil(t, CloseRsp) 323 assert.Equal(t, uint32(100), clientMsg.StreamID()) 324 assert.Nil(t, clientMsg.ClientRspErr()) 325 326 } 327 328 // TestStreamCodecReset tests stream Reset/Close frame codec. 329 func TestStreamCodecReset(t *testing.T) { 330 // initMeta will be deleted from cache after TestStreamCodecClose. 331 // TestStreamCodecInit should be called again to recreate initMeta. 332 TestStreamCodecInit(t) 333 334 msg := trpc.Message(ctx) 335 assert.NotNil(t, msg) 336 337 ctx := trpc.BackgroundContext() 338 assert.NotNil(t, ctx) 339 serverCodec := codec.GetServer("trpc") 340 clientCodec := codec.GetClient("trpc") 341 342 // Client encode Reset 343 frameHead := &trpc.FrameHead{ 344 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 345 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE), 346 StreamID: 100, 347 } 348 msg.WithFrameHead(frameHead) 349 msg.WithStreamID(100) 350 reset := &trpcpb.TrpcStreamCloseMeta{} 351 reset.CloseType = int32(trpcpb.TrpcStreamCloseType_TRPC_STREAM_RESET) 352 reset.Ret = int32(1) 353 reset.Msg = []byte("reset after error") 354 msg.WithStreamFrame(reset) 355 resetResult := []byte{0x9, 0x30, 0x1, 0x4, 0x0, 0x0, 0x0, 0x27, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 356 0x8, 0x1, 0x10, 0x1, 0x1a, 0x11, 0x72, 0x65, 0x73, 0x65, 0x74, 0x20, 0x61, 0x66, 0x74, 0x65, 0x72, 0x20, 357 0x65, 0x72, 0x72, 0x6f, 0x72} 358 resetBuf, err := clientCodec.Encode(msg, nil) 359 assert.Nil(t, err) 360 assert.Equal(t, resetResult, resetBuf) 361 362 // Server decode Reset 363 serverCtx := context.Background() 364 _, serverMsg := codec.WithNewMessage(serverCtx) 365 laddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10000") 366 assert.Nil(t, err) 367 raddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10001") 368 assert.Nil(t, err) 369 serverMsg.WithLocalAddr(laddr) 370 serverMsg.WithRemoteAddr(raddr) 371 resetDecode, err := serverCodec.Decode(serverMsg, resetResult) 372 assert.Nil(t, err) 373 assert.Nil(t, resetDecode) 374 assert.Equal(t, uint32(100), serverMsg.StreamID()) 375 assert.Nil(t, err) 376 assert.NotNil(t, serverMsg.ServerRspErr()) 377 378 // server encode Close 379 ctx = context.Background() 380 _, encodeMsg := codec.WithNewMessage(ctx) 381 encodeMsg.WithLocalAddr(laddr) 382 encodeMsg.WithRemoteAddr(raddr) 383 serverFrameHead := &trpc.FrameHead{ 384 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 385 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE), 386 StreamID: 100, 387 } 388 reset = &trpcpb.TrpcStreamCloseMeta{} 389 reset.CloseType = int32(trpcpb.TrpcStreamCloseType_TRPC_STREAM_RESET) 390 reset.Ret = int32(1) 391 reset.Msg = []byte("Server Side Close error") 392 encodeMsg.WithFrameHead(serverFrameHead) 393 encodeMsg.WithStreamFrame(reset) 394 encodeMsg.WithStreamID(100) 395 serverEncodeData := []byte{0x9, 0x30, 0x1, 0x4, 0x0, 0x0, 0x0, 0x2d, 0x0, 0x0, 0x0, 0x0, 0x0, 396 0x64, 0x0, 0x0, 0x8, 0x1, 0x10, 0x1, 0x1a, 0x17, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x53, 0x69, 0x64, 397 0x65, 0x20, 0x43, 0x6c, 0x6f, 0x73, 0x65, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72} 398 rspBuf, err := serverCodec.Encode(encodeMsg, nil) 399 assert.Equal(t, serverEncodeData, rspBuf) 400 assert.Nil(t, err) 401 402 // client Decode reset 403 clientCtx := context.Background() 404 _, clientMsg := codec.WithNewMessage(clientCtx) 405 encodeMsg.WithLocalAddr(laddr) 406 encodeMsg.WithRemoteAddr(raddr) 407 resetRsp, err := clientCodec.Decode(clientMsg, serverEncodeData) 408 assert.Nil(t, err) 409 assert.Nil(t, resetRsp) 410 assert.Equal(t, uint32(100), clientMsg.StreamID()) 411 assert.NotNil(t, clientMsg.ClientRspErr()) 412 413 } 414 415 // TestUnknownFrameType tests stream frame with unknown type. 416 func TestUnknownFrameType(t *testing.T) { 417 msg := trpc.Message(ctx) 418 assert.NotNil(t, msg) 419 ctx := trpc.BackgroundContext() 420 assert.NotNil(t, ctx) 421 clientCodec := codec.GetClient("trpc") 422 serverCodec := codec.GetServer("trpc") 423 424 serverEncodeData := []byte{0x9, 0x30, 0x1, 0x7, 0x0, 0x0, 0x0, 0x2d, 0x0, 0x0, 0x0, 0x0, 425 0x0, 0x64, 0x0, 0x0, 0x12, 0x1b, 0x8, 0x2, 426 0x12, 0x17, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x65, 0x6e, 0x63, 427 0x6f, 0x64, 0x65, 0x20, 0x66, 0x61, 0x69, 0x6c} 428 // Client decode 429 clientCtx := context.Background() 430 _, clientMsg := codec.WithNewMessage(clientCtx) 431 initRsp, err := clientCodec.Decode(clientMsg, serverEncodeData) 432 assert.NotNil(t, err) 433 assert.Nil(t, initRsp) 434 435 // client Encode unknown frame type 436 frameHead := &trpc.FrameHead{ 437 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 438 StreamFrameType: uint8(8), 439 StreamID: 100, 440 } 441 msg.WithFrameHead(frameHead) 442 msg.WithStreamID(100) 443 data := []byte("hello world") 444 dataBuf, err := clientCodec.Encode(msg, data) 445 // Unknown stream frame type 446 assert.NotNil(t, err) 447 assert.Nil(t, dataBuf) 448 449 // server Decode unknown Frame Type 450 dataResult := []byte{0x9, 0x30, 0x1, 0x8, 0x0, 0x0, 0x0, 0x1b, 0x0, 0x0, 0x0, 451 0x0, 0x0, 0x64, 0x0, 0x0, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64} 452 serverCtx := context.Background() 453 _, serverMsg := codec.WithNewMessage(serverCtx) 454 dataDecode, err := serverCodec.Decode(serverMsg, dataResult) 455 assert.NotNil(t, err) 456 assert.Nil(t, dataDecode) 457 458 // server Encode unknown frame type 459 ctx = context.Background() 460 _, encodeMsg := codec.WithNewMessage(ctx) 461 serverFrameHead := &trpc.FrameHead{ 462 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 463 StreamFrameType: uint8(8), 464 StreamID: 100, 465 } 466 encodeMsg.WithFrameHead(serverFrameHead) 467 encodeMsg.WithStreamID(100) 468 encodeData := []byte("hi there") 469 rspBuf, err := serverCodec.Encode(encodeMsg, encodeData) 470 assert.NotNil(t, err) 471 assert.Nil(t, rspBuf) 472 } 473 474 // TestFeedbackFrameType tests feedback frame type. 475 func TestFeedbackFrameType(t *testing.T) { 476 msg := trpc.Message(ctx) 477 assert.NotNil(t, msg) 478 ctx := trpc.BackgroundContext() 479 assert.NotNil(t, ctx) 480 clientCodec := codec.GetClient("trpc") 481 serverCodec := codec.GetServer("trpc") 482 483 // init first, ensuring that init frame is saved 484 initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x59, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0xa, 485 0x47, 0xa, 0x1d, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x74, 0x72, 0x70, 0x63, 0x2d, 0x67, 486 0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x73, 0x65, 487 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 488 0x74, 0x72, 0x70, 0x63, 0x2e, 0x74, 0x65, 0x73, 0x74, 489 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x47, 0x72, 490 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f} 491 // server Decode 492 serverCtx := context.Background() 493 _, serverMsg := codec.WithNewMessage(serverCtx) 494 laddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10000") 495 assert.Nil(t, err) 496 raddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10001") 497 assert.Nil(t, err) 498 serverMsg.WithLocalAddr(laddr) 499 serverMsg.WithRemoteAddr(raddr) 500 init, err := serverCodec.Decode(serverMsg, initResult) 501 assert.Nil(t, err) 502 assert.Nil(t, init) 503 assert.Equal(t, uint32(100), serverMsg.StreamID()) 504 assert.NotNil(t, serverMsg.CallerService()) 505 506 encodeData := []byte{0x9, 0x30, 0x1, 0x3, 0x0, 0x0, 0x0, 0x13, 0x0, 0x0, 507 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0x8, 0x90, 0x4e} 508 // Client decode feedback 509 clientCtx := context.Background() 510 _, clientMsg := codec.WithNewMessage(clientCtx) 511 res, err := clientCodec.Decode(clientMsg, encodeData) 512 assert.Nil(t, err) 513 assert.Nil(t, res) 514 feedback, ok := clientMsg.StreamFrame().(*trpcpb.TrpcStreamFeedBackMeta) 515 assert.True(t, ok) 516 assert.Equal(t, uint32(10000), feedback.WindowSizeIncrement) 517 518 // client Encode feedback type 519 frameHead := &trpc.FrameHead{ 520 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 521 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_FEEDBACK), 522 StreamID: 100, 523 } 524 msg.WithFrameHead(frameHead) 525 msg.WithStreamID(100) 526 var data []byte 527 feedbackMeta := &trpcpb.TrpcStreamFeedBackMeta{} 528 msg.WithStreamFrame(feedbackMeta) 529 feedbackMeta.WindowSizeIncrement = 10000 530 dataBuf, err := clientCodec.Encode(msg, data) 531 assert.Nil(t, err) 532 assert.Equal(t, encodeData, dataBuf) 533 534 // server Decode feedback frame 535 serverCtx = context.Background() 536 _, serverMsg = codec.WithNewMessage(serverCtx) 537 serverMsg.WithLocalAddr(laddr) 538 serverMsg.WithRemoteAddr(raddr) 539 dataDecode, err := serverCodec.Decode(serverMsg, encodeData) 540 assert.Nil(t, dataDecode) 541 assert.Nil(t, err) 542 feedback, ok = clientMsg.StreamFrame().(*trpcpb.TrpcStreamFeedBackMeta) 543 assert.True(t, ok) 544 assert.Equal(t, uint32(10000), feedback.WindowSizeIncrement) 545 546 // server Encode feedbackframe 547 ctx = context.Background() 548 _, encodeMsg := codec.WithNewMessage(ctx) 549 serverFrameHead := &trpc.FrameHead{ 550 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 551 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_FEEDBACK), 552 StreamID: 100, 553 } 554 encodeMsg.WithFrameHead(serverFrameHead) 555 encodeMsg.WithStreamID(100) 556 feedbackMeta = &trpcpb.TrpcStreamFeedBackMeta{} 557 encodeMsg.WithStreamFrame(feedbackMeta) 558 feedbackMeta.WindowSizeIncrement = 10000 559 rspBuf, err := serverCodec.Encode(encodeMsg, nil) 560 assert.Nil(t, err) 561 assert.Equal(t, rspBuf, encodeData) 562 563 } 564 565 // TestDecodeEncodeFail tests codec error. 566 func TestDecodeEncodeFail(t *testing.T) { 567 ctx := trpc.BackgroundContext() 568 assert.NotNil(t, ctx) 569 clientCodec := codec.GetClient("trpc") 570 serverCodec := codec.GetServer("trpc") 571 572 // init first, ensuring that init frame is saved 573 initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x59, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0xa, 574 0x47, 0xa, 0x1d, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x74, 0x72, 0x70, 0x63, 0x2d, 0x67, 575 0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x73, 0x65, 576 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 577 0x74, 0x72, 0x70, 0x63, 0x2e, 0x74, 0x65, 0x73, 0x74, 578 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x47, 0x72, 579 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f} 580 // server Decode 581 serverCtx := context.Background() 582 _, serverMsg := codec.WithNewMessage(serverCtx) 583 laddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10000") 584 assert.Nil(t, err) 585 raddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10001") 586 assert.Nil(t, err) 587 serverMsg.WithLocalAddr(laddr) 588 serverMsg.WithRemoteAddr(raddr) 589 init, err := serverCodec.Decode(serverMsg, initResult) 590 assert.Nil(t, err) 591 assert.Nil(t, init) 592 assert.Equal(t, uint32(100), serverMsg.StreamID()) 593 assert.NotNil(t, serverMsg.CallerService()) 594 595 encodeData := []byte{0x9, 0x30, 0x1, 0x3, 0x0, 0x0, 0x0, 0x01, 0x0, 0x10, 596 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0x8, 0x90, 0x4e} 597 // client decode total length error 598 clientCtx := context.Background() 599 _, clientMsg := codec.WithNewMessage(clientCtx) 600 rsp, err := clientCodec.Decode(clientMsg, encodeData) 601 assert.Nil(t, rsp) 602 assert.NotNil(t, err) 603 604 // client init unmarshal error 605 serverInitData := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x2d, 0x0, 0x0, 0x0, 0x0, 0x1, 0x64, 606 0x0, 0x0, 0x12, 0x1b, 0x90, 0x2, 0x12, 0x17, 0x73, 0x65, 0x54, 0x76, 0x65, 0x72, 0x20, 0x74, 0x65, 607 0x73, 0x74, 0x20, 0x61, 0x6e, 0x62, 608 0x6f, 0x64, 0x65, 0x20, 0x66, 0x61, 0x69, 0x6c} 609 initRsp, err := clientCodec.Decode(clientMsg, serverInitData) 610 assert.Nil(t, initRsp) 611 assert.NotNil(t, err) 612 613 t.Run("ClientStreamCodec unknown stream frame type error from message", func(t *testing.T) { 614 cc := trpc.NewClientStreamCodec() 615 616 _, err := cc.Encode(trpc.Message(context.Background()), nil) 617 require.Contains(t, err.Error(), "unknown stream frame type") 618 619 _, err = cc.Decode(trpc.Message(context.Background()), nil) 620 require.Contains(t, err.Error(), "unknown stream frame type") 621 }) 622 t.Run("ServerStreamCodec unknown stream frame type error from message", func(t *testing.T) { 623 cc := trpc.NewServerStreamCodec() 624 625 _, err := cc.Encode(trpc.Message(context.Background()), nil) 626 require.Contains(t, err.Error(), "unknown stream frame type") 627 628 _, err = cc.Decode(trpc.Message(context.Background()), nil) 629 require.Contains(t, err.Error(), "unknown stream frame type") 630 }) 631 632 } 633 634 // TestEncodeWithMetadata tests encode with metadata. 635 func TestEncodeWithMetadata(t *testing.T) { 636 msg := trpc.Message(ctx) 637 assert.NotNil(t, msg) 638 ctx := trpc.BackgroundContext() 639 assert.NotNil(t, ctx) 640 serverCodec := codec.GetServer("trpc") 641 clientCodec := codec.GetClient("trpc") 642 // Client encode 643 frameHead := &trpc.FrameHead{ 644 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 645 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT), 646 StreamID: 100, 647 } 648 initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x61, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 649 0xa, 0x4f, 0xa, 0x17, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x65, 0x72, 0x76, 650 0x65, 0x72, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 0x74, 0x72, 0x70, 0x63, 651 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 652 0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2a, 653 0xc, 0xa, 0x3, 0x6b, 0x65, 0x79, 0x12, 0x5, 0x76, 0x61, 0x6c, 0x75, 0x65} 654 msg.WithFrameHead(frameHead) 655 msg.WithClientMetaData(codec.MetaData{"key": []byte("value")}) 656 msg.WithStreamID(100) 657 msg.WithCallerServiceName("trpc.app.server.service") 658 msg.WithClientRPCName("/trpc.test.helloworld.Greeter/SayHello") 659 initBuf, err := clientCodec.Encode(msg, nil) 660 assert.Nil(t, err) 661 assert.Equal(t, initResult, initBuf) 662 663 // Server Decode 664 serverCtx, serverMsg := codec.WithNewMessage(context.Background()) 665 laddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10000") 666 assert.Nil(t, err) 667 raddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10001") 668 assert.Nil(t, err) 669 serverMsg.WithLocalAddr(laddr) 670 serverMsg.WithRemoteAddr(raddr) 671 initRsp, err := serverCodec.Decode(serverMsg, initResult) 672 assert.Nil(t, err) 673 assert.Nil(t, initRsp) 674 assert.Equal(t, trpc.GetMetaData(serverCtx, "key"), []byte("value")) 675 } 676 677 // TestEncodeWithDyeing tests encode with dyeing key. 678 func TestEncodeWithDyeing(t *testing.T) { 679 msg := trpc.Message(ctx) 680 assert.NotNil(t, msg) 681 ctx := trpc.BackgroundContext() 682 assert.NotNil(t, ctx) 683 serverCodec := codec.GetServer("trpc") 684 clientCodec := codec.GetClient("trpc") 685 // Client encode 686 frameHead := &trpc.FrameHead{ 687 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 688 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT), 689 StreamID: 100, 690 } 691 initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x74, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 692 0xa, 0x62, 0xa, 0x17, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x65, 0x72, 0x76, 693 0x65, 0x72, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 0x74, 0x72, 0x70, 0x63, 694 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 695 0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 696 0x1, 0x2a, 0x1d, 0xa, 0xf, 0x74, 0x72, 0x70, 0x63, 0x2d, 0x64, 0x79, 0x65, 0x69, 0x6e, 0x67, 0x2d, 0x6b, 697 0x65, 0x79, 0x12, 0xa, 0x64, 0x79, 0x65, 0x69, 0x6e, 0x67, 0x2d, 0x6b, 0x65, 0x79} 698 msg.WithFrameHead(frameHead) 699 msg.WithDyeing(true) 700 msg.WithDyeingKey("dyeing-key") 701 msg.WithStreamID(100) 702 msg.WithCallerServiceName("trpc.app.server.service") 703 msg.WithClientRPCName("/trpc.test.helloworld.Greeter/SayHello") 704 initBuf, err := clientCodec.Encode(msg, nil) 705 assert.Nil(t, err) 706 assert.Equal(t, initResult, initBuf) 707 708 // Server Decode 709 serverCtx, serverMsg := codec.WithNewMessage(context.Background()) 710 laddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10000") 711 assert.Nil(t, err) 712 raddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10001") 713 assert.Nil(t, err) 714 serverMsg.WithLocalAddr(laddr) 715 serverMsg.WithRemoteAddr(raddr) 716 initRsp, err := serverCodec.Decode(serverMsg, initResult) 717 assert.Nil(t, err) 718 assert.Nil(t, initRsp) 719 assert.Equal(t, trpc.GetMetaData(serverCtx, trpc.DyeingKey), []byte("dyeing-key")) 720 } 721 722 // TestEncodeWithEnvTransfer tests encode with envtransfor. 723 func TestEncodeWithEnvTransfer(t *testing.T) { 724 msg := trpc.Message(ctx) 725 assert.NotNil(t, msg) 726 ctx := trpc.BackgroundContext() 727 assert.NotNil(t, ctx) 728 serverCodec := codec.GetServer("trpc") 729 clientCodec := codec.GetClient("trpc") 730 // Client encode 731 frameHead := &trpc.FrameHead{ 732 FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME), 733 StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT), 734 StreamID: 100, 735 } 736 initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x6a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 737 0xa, 0x58, 0xa, 0x17, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x65, 0x72, 0x76, 738 0x65, 0x72, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 0x74, 0x72, 0x70, 0x63, 739 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 740 0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2a, 741 0x15, 0xa, 0x8, 0x74, 0x72, 0x70, 0x63, 0x2d, 0x65, 0x6e, 0x76, 0x12, 0x9, 0x65, 0x6e, 0x76, 0x2d, 742 0x74, 0x72, 0x61, 0x6e, 0x73} 743 msg.WithFrameHead(frameHead) 744 msg.WithEnvTransfer("env-trans") 745 msg.WithStreamID(100) 746 msg.WithCallerServiceName("trpc.app.server.service") 747 msg.WithClientRPCName("/trpc.test.helloworld.Greeter/SayHello") 748 initBuf, err := clientCodec.Encode(msg, nil) 749 assert.Nil(t, err) 750 assert.Equal(t, initResult, initBuf) 751 752 // Server Decode 753 serverCtx, serverMsg := codec.WithNewMessage(context.Background()) 754 laddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10000") 755 assert.Nil(t, err) 756 raddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:10001") 757 assert.Nil(t, err) 758 serverMsg.WithLocalAddr(laddr) 759 serverMsg.WithRemoteAddr(raddr) 760 initRsp, err := serverCodec.Decode(serverMsg, initResult) 761 assert.Nil(t, err) 762 assert.Nil(t, initRsp) 763 assert.Equal(t, trpc.GetMetaData(serverCtx, trpc.EnvTransfer), []byte("env-trans")) 764 }