1 Star 0 Fork 0

z1312655689/trpc-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
codec_stream_test.go 26.94 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
// Tencent is pleased to support the open source community by making tRPC available.
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License that can be found in the LICENSE file.
package trpc_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
trpcpb "trpc.group/trpc/trpc-protocol/pb/go/trpc"
trpc "trpc.group/trpc-go/trpc-go"
"trpc.group/trpc-go/trpc-go/codec"
"trpc.group/trpc-go/trpc-go/errs"
)
// TestStreamCodecInit tests stream Init frame codec.
func TestStreamCodecInit(t *testing.T) {
msg := trpc.Message(ctx)
assert.NotNil(t, msg)
ctx := trpc.BackgroundContext()
assert.NotNil(t, ctx)
serverCodec := codec.GetServer("trpc")
clientCodec := codec.GetClient("trpc")
// Client encode
frameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT),
StreamID: 100,
}
initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x53, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0,
0xa, 0x41, 0xa, 0x17, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x65, 0x72, 0x76,
0x65, 0x72, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 0x74, 0x72, 0x70, 0x63,
0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e,
0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f}
msg.WithFrameHead(frameHead)
msg.WithStreamID(100)
msg.WithCallerServiceName("trpc.app.server.service")
msg.WithClientRPCName("/trpc.test.helloworld.Greeter/SayHello")
initBuf, err := clientCodec.Encode(msg, nil)
assert.Nil(t, err)
assert.Equal(t, initResult, initBuf)
// Client Encode With MetaData
initMetaResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x63, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0,
0x0, 0xa, 0x51, 0xa, 0x17, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x65, 0x72,
0x76, 0x65, 0x72, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 0x74, 0x72,
0x70, 0x63, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72,
0x6c, 0x64, 0x2e, 0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65,
0x6c, 0x6c, 0x6f, 0x2a, 0xe, 0xa, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x12, 0x5, 0x77, 0x6f, 0x72,
0x6c, 0x64}
msg.WithFrameHead(frameHead)
msg.WithStreamID(100)
meta := codec.MetaData{"hello": []byte("world")}
msg.WithClientMetaData(meta)
msg.WithCallerServiceName("trpc.app.server.service")
msg.WithClientRPCName("/trpc.test.helloworld.Greeter/SayHello")
initBuf, err = clientCodec.Encode(msg, nil)
assert.Nil(t, err)
assert.Equal(t, initMetaResult, initBuf)
// server Decode
serverCtx := context.Background()
_, serverMsg := codec.WithNewMessage(serverCtx)
init, err := serverCodec.Decode(serverMsg, initResult)
assert.Nil(t, err)
assert.Nil(t, init)
assert.Equal(t, uint32(100), serverMsg.StreamID())
assert.NotNil(t, serverMsg.CallerService())
// server encode
ctx = context.Background()
_, encodeMsg := codec.WithNewMessage(ctx)
serverFrameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT),
StreamID: 100,
}
encodeMsg.WithFrameHead(serverFrameHead)
encodeMsg.WithStreamID(100)
errRsp := errs.NewFrameError(errs.RetServerEncodeFail, "server test encode fail")
encodeMsg.WithServerRspErr(errRsp)
serverEncodeData := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x2d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64,
0x0, 0x0, 0x12, 0x1b, 0x8, 0x2, 0x12, 0x17, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x74, 0x65,
0x73, 0x74, 0x20, 0x65, 0x6e, 0x63,
0x6f, 0x64, 0x65, 0x20, 0x66, 0x61, 0x69, 0x6c}
rspBuf, err := serverCodec.Encode(encodeMsg, nil)
assert.Nil(t, err)
assert.Equal(t, serverEncodeData, rspBuf)
// Client decode
clientCtx := context.Background()
_, clientMsg := codec.WithNewMessage(clientCtx)
initRsp, err := clientCodec.Decode(clientMsg, serverEncodeData)
assert.Nil(t, err)
assert.Nil(t, initRsp)
assert.Equal(t, uint32(100), clientMsg.StreamID())
assert.NotNil(t, clientMsg.ClientRspErr())
}
// TestStreamCodecData tests stream Data frame codec.
func TestStreamCodecData(t *testing.T) {
msg := trpc.Message(ctx)
assert.NotNil(t, msg)
ctx := trpc.BackgroundContext()
assert.NotNil(t, ctx)
serverCodec := codec.GetServer("trpc")
clientCodec := codec.GetClient("trpc")
// init first, ensuring that init frame is saved
initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x59, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0xa,
0x47, 0xa, 0x1d, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x74, 0x72, 0x70, 0x63, 0x2d, 0x67,
0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x73, 0x65,
0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f,
0x74, 0x72, 0x70, 0x63, 0x2e, 0x74, 0x65, 0x73, 0x74,
0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x47, 0x72,
0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f}
// server Decode
serverCtx := context.Background()
_, serverMsg := codec.WithNewMessage(serverCtx)
init, err := serverCodec.Decode(serverMsg, initResult)
assert.Nil(t, err)
assert.Nil(t, init)
assert.Equal(t, uint32(100), serverMsg.StreamID())
assert.NotNil(t, serverMsg.CallerService())
// client Encode
frameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_DATA),
StreamID: 100,
}
msg.WithFrameHead(frameHead)
msg.WithStreamID(100)
dataResult := []byte{0x9, 0x30, 0x1, 0x2, 0x0, 0x0, 0x0, 0x1b, 0x0, 0x0, 0x0,
0x0, 0x0, 0x64, 0x0, 0x0, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64}
data := []byte("hello world")
dataBuf, err := clientCodec.Encode(msg, data)
assert.Nil(t, err)
assert.Equal(t, dataBuf, dataResult)
// Server Decode
serverCtx = context.Background()
_, serverMsg = codec.WithNewMessage(serverCtx)
dataDecode, err := serverCodec.Decode(serverMsg, dataResult)
assert.Nil(t, err)
assert.Equal(t, dataDecode, data)
assert.Equal(t, uint32(100), serverMsg.StreamID())
// server Encode
ctx = context.Background()
_, encodeMsg := codec.WithNewMessage(ctx)
serverFrameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_DATA),
StreamID: 100,
}
encodeMsg.WithFrameHead(serverFrameHead)
encodeMsg.WithStreamID(100)
encodeData := []byte("hi there")
serverEncodeData := []byte{0x9, 0x30, 0x1, 0x2, 0x0, 0x0, 0x0, 0x18, 0x0, 0x0, 0x0, 0x0, 0x0,
0x64, 0x0, 0x0, 0x68, 0x69, 0x20, 0x74, 0x68, 0x65, 0x72, 0x65}
rspBuf, err := serverCodec.Encode(encodeMsg, encodeData)
assert.Nil(t, err)
assert.Equal(t, serverEncodeData, rspBuf)
// Client Decode
clientCtx := context.Background()
_, clientMsg := codec.WithNewMessage(clientCtx)
rspData, err := clientCodec.Decode(clientMsg, serverEncodeData)
assert.Nil(t, err)
assert.Equal(t, uint32(100), clientMsg.StreamID())
assert.Equal(t, string(rspData), "hi there")
// server Encode with ServerRspErr
encodeMsg.WithServerRspErr(errs.NewFrameError(1, "test error"))
rspBuf, err = serverCodec.Encode(encodeMsg, encodeData)
serverEncodeData = []byte{0x9, 0x30, 0x1, 0x4, 0x0, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64,
0x0, 0x0, 0x8, 0x1, 0x10, 0x1, 0x1a, 0xa, 0x74, 0x65, 0x73, 0x74, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72}
assert.Nil(t, err)
assert.Equal(t, serverEncodeData, rspBuf)
}
// TestStreamCodecClose tests stream Close frame codec.
func TestStreamCodecClose(t *testing.T) {
msg := trpc.Message(ctx)
assert.NotNil(t, msg)
ctx := trpc.BackgroundContext()
assert.NotNil(t, ctx)
serverCodec := codec.GetServer("trpc")
clientCodec := codec.GetClient("trpc")
// init first, ensuring that init frame is saved
initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x59, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0xa,
0x47, 0xa, 0x1d, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x74, 0x72, 0x70, 0x63, 0x2d, 0x67,
0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x73, 0x65,
0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f,
0x74, 0x72, 0x70, 0x63, 0x2e, 0x74, 0x65, 0x73, 0x74,
0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x47, 0x72,
0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f}
// server Decode
serverCtx := context.Background()
_, serverMsg := codec.WithNewMessage(serverCtx)
init, err := serverCodec.Decode(serverMsg, initResult)
assert.Nil(t, err)
assert.Nil(t, init)
assert.Equal(t, uint32(100), serverMsg.StreamID())
assert.NotNil(t, serverMsg.CallerService())
// client encode Close
frameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE),
StreamID: 100,
}
msg.WithFrameHead(frameHead)
msg.WithStreamID(100)
close := &trpcpb.TrpcStreamCloseMeta{}
close.CloseType = int32(trpcpb.TrpcStreamCloseType_TRPC_STREAM_CLOSE)
close.Ret = int32(0)
msg.WithStreamFrame(close)
closeResult := []byte{0x9, 0x30, 0x1, 0x4, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0}
closeBuf, err := clientCodec.Encode(msg, nil)
assert.Nil(t, err)
assert.Equal(t, closeResult, closeBuf)
// server Decode Close
serverCtx = context.Background()
_, serverMsg = codec.WithNewMessage(serverCtx)
closeDecode, err := serverCodec.Decode(serverMsg, closeResult)
assert.Nil(t, err)
assert.Nil(t, closeDecode)
assert.Equal(t, uint32(100), serverMsg.StreamID())
// server encode Close
ctx = context.Background()
_, encodeMsg := codec.WithNewMessage(ctx)
serverFrameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE),
StreamID: 100,
}
close = &trpcpb.TrpcStreamCloseMeta{}
close.CloseType = int32(trpcpb.TrpcStreamCloseType_TRPC_STREAM_CLOSE)
close.Ret = int32(0)
encodeMsg.WithFrameHead(serverFrameHead)
encodeMsg.WithStreamFrame(close)
encodeMsg.WithStreamID(100)
serverEncodeData := []byte{0x9, 0x30, 0x1, 0x4, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0}
rspBuf, err := serverCodec.Encode(encodeMsg, nil)
assert.Equal(t, serverEncodeData, rspBuf)
assert.Nil(t, err)
assert.Equal(t, msg.StreamID(), uint32(100))
// Server decode error after encode close
serverCtx = context.Background()
_, serverMsg = codec.WithNewMessage(serverCtx)
closeDecode, err = serverCodec.Decode(serverMsg, closeResult)
assert.NotNil(t, err)
assert.Nil(t, closeDecode)
// Client decode close
clientCtx := context.Background()
_, clientMsg := codec.WithNewMessage(clientCtx)
CloseRsp, err := clientCodec.Decode(clientMsg, serverEncodeData)
assert.Nil(t, err)
assert.Nil(t, CloseRsp)
assert.Equal(t, uint32(100), clientMsg.StreamID())
assert.Nil(t, clientMsg.ClientRspErr())
}
// TestStreamCodecReset tests stream Reset/Close frame codec.
func TestStreamCodecReset(t *testing.T) {
// initMeta will be deleted from cache after TestStreamCodecClose.
// TestStreamCodecInit should be called again to recreate initMeta.
TestStreamCodecInit(t)
msg := trpc.Message(ctx)
assert.NotNil(t, msg)
ctx := trpc.BackgroundContext()
assert.NotNil(t, ctx)
serverCodec := codec.GetServer("trpc")
clientCodec := codec.GetClient("trpc")
// Client encode Reset
frameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE),
StreamID: 100,
}
msg.WithFrameHead(frameHead)
msg.WithStreamID(100)
reset := &trpcpb.TrpcStreamCloseMeta{}
reset.CloseType = int32(trpcpb.TrpcStreamCloseType_TRPC_STREAM_RESET)
reset.Ret = int32(1)
reset.Msg = []byte("reset after error")
msg.WithStreamFrame(reset)
resetResult := []byte{0x9, 0x30, 0x1, 0x4, 0x0, 0x0, 0x0, 0x27, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0,
0x8, 0x1, 0x10, 0x1, 0x1a, 0x11, 0x72, 0x65, 0x73, 0x65, 0x74, 0x20, 0x61, 0x66, 0x74, 0x65, 0x72, 0x20,
0x65, 0x72, 0x72, 0x6f, 0x72}
resetBuf, err := clientCodec.Encode(msg, nil)
assert.Nil(t, err)
assert.Equal(t, resetResult, resetBuf)
// Server decode Reset
serverCtx := context.Background()
_, serverMsg := codec.WithNewMessage(serverCtx)
resetDecode, err := serverCodec.Decode(serverMsg, resetResult)
assert.Nil(t, err)
assert.Nil(t, resetDecode)
assert.Equal(t, uint32(100), serverMsg.StreamID())
assert.Nil(t, err)
assert.NotNil(t, serverMsg.ServerRspErr())
// server encode Close
ctx = context.Background()
_, encodeMsg := codec.WithNewMessage(ctx)
serverFrameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_CLOSE),
StreamID: 100,
}
reset = &trpcpb.TrpcStreamCloseMeta{}
reset.CloseType = int32(trpcpb.TrpcStreamCloseType_TRPC_STREAM_RESET)
reset.Ret = int32(1)
reset.Msg = []byte("Server Side Close error")
encodeMsg.WithFrameHead(serverFrameHead)
encodeMsg.WithStreamFrame(reset)
encodeMsg.WithStreamID(100)
serverEncodeData := []byte{0x9, 0x30, 0x1, 0x4, 0x0, 0x0, 0x0, 0x2d, 0x0, 0x0, 0x0, 0x0, 0x0,
0x64, 0x0, 0x0, 0x8, 0x1, 0x10, 0x1, 0x1a, 0x17, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x53, 0x69, 0x64,
0x65, 0x20, 0x43, 0x6c, 0x6f, 0x73, 0x65, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72}
rspBuf, err := serverCodec.Encode(encodeMsg, nil)
assert.Equal(t, serverEncodeData, rspBuf)
assert.Nil(t, err)
// client Decode reset
clientCtx := context.Background()
_, clientMsg := codec.WithNewMessage(clientCtx)
resetRsp, err := clientCodec.Decode(clientMsg, serverEncodeData)
assert.Nil(t, err)
assert.Nil(t, resetRsp)
assert.Equal(t, uint32(100), clientMsg.StreamID())
assert.NotNil(t, clientMsg.ClientRspErr())
}
// TestUnknownFrameType tests stream frame with unknown type.
func TestUnknownFrameType(t *testing.T) {
msg := trpc.Message(ctx)
assert.NotNil(t, msg)
ctx := trpc.BackgroundContext()
assert.NotNil(t, ctx)
clientCodec := codec.GetClient("trpc")
serverCodec := codec.GetServer("trpc")
serverEncodeData := []byte{0x9, 0x30, 0x1, 0x7, 0x0, 0x0, 0x0, 0x2d, 0x0, 0x0, 0x0, 0x0,
0x0, 0x64, 0x0, 0x0, 0x12, 0x1b, 0x8, 0x2,
0x12, 0x17, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x65, 0x6e, 0x63,
0x6f, 0x64, 0x65, 0x20, 0x66, 0x61, 0x69, 0x6c}
// Client decode
clientCtx := context.Background()
_, clientMsg := codec.WithNewMessage(clientCtx)
initRsp, err := clientCodec.Decode(clientMsg, serverEncodeData)
assert.NotNil(t, err)
assert.Nil(t, initRsp)
// client Encode unknown frame type
frameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(8),
StreamID: 100,
}
msg.WithFrameHead(frameHead)
msg.WithStreamID(100)
data := []byte("hello world")
dataBuf, err := clientCodec.Encode(msg, data)
// Unknown stream frame type
assert.NotNil(t, err)
assert.Nil(t, dataBuf)
// server Decode unknown Frame Type
dataResult := []byte{0x9, 0x30, 0x1, 0x8, 0x0, 0x0, 0x0, 0x1b, 0x0, 0x0, 0x0,
0x0, 0x0, 0x64, 0x0, 0x0, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64}
serverCtx := context.Background()
_, serverMsg := codec.WithNewMessage(serverCtx)
dataDecode, err := serverCodec.Decode(serverMsg, dataResult)
assert.NotNil(t, err)
assert.Nil(t, dataDecode)
// server Encode unknown frame type
ctx = context.Background()
_, encodeMsg := codec.WithNewMessage(ctx)
serverFrameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(8),
StreamID: 100,
}
encodeMsg.WithFrameHead(serverFrameHead)
encodeMsg.WithStreamID(100)
encodeData := []byte("hi there")
rspBuf, err := serverCodec.Encode(encodeMsg, encodeData)
assert.NotNil(t, err)
assert.Nil(t, rspBuf)
}
// TestFeedbackFrameType tests feedback frame type.
func TestFeedbackFrameType(t *testing.T) {
msg := trpc.Message(ctx)
assert.NotNil(t, msg)
ctx := trpc.BackgroundContext()
assert.NotNil(t, ctx)
clientCodec := codec.GetClient("trpc")
serverCodec := codec.GetServer("trpc")
// init first, ensuring that init frame is saved
initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x59, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0xa,
0x47, 0xa, 0x1d, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x74, 0x72, 0x70, 0x63, 0x2d, 0x67,
0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x73, 0x65,
0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f,
0x74, 0x72, 0x70, 0x63, 0x2e, 0x74, 0x65, 0x73, 0x74,
0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x47, 0x72,
0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f}
// server Decode
serverCtx := context.Background()
_, serverMsg := codec.WithNewMessage(serverCtx)
init, err := serverCodec.Decode(serverMsg, initResult)
assert.Nil(t, err)
assert.Nil(t, init)
assert.Equal(t, uint32(100), serverMsg.StreamID())
assert.NotNil(t, serverMsg.CallerService())
encodeData := []byte{0x9, 0x30, 0x1, 0x3, 0x0, 0x0, 0x0, 0x13, 0x0, 0x0,
0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0x8, 0x90, 0x4e}
// Client decode feedback
clientCtx := context.Background()
_, clientMsg := codec.WithNewMessage(clientCtx)
res, err := clientCodec.Decode(clientMsg, encodeData)
assert.Nil(t, err)
assert.Nil(t, res)
feedback, ok := clientMsg.StreamFrame().(*trpcpb.TrpcStreamFeedBackMeta)
assert.True(t, ok)
assert.Equal(t, uint32(10000), feedback.WindowSizeIncrement)
// client Encode feedback type
frameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_FEEDBACK),
StreamID: 100,
}
msg.WithFrameHead(frameHead)
msg.WithStreamID(100)
var data []byte
feedbackMeta := &trpcpb.TrpcStreamFeedBackMeta{}
msg.WithStreamFrame(feedbackMeta)
feedbackMeta.WindowSizeIncrement = 10000
dataBuf, err := clientCodec.Encode(msg, data)
assert.Nil(t, err)
assert.Equal(t, encodeData, dataBuf)
// server Decode feedback frame
serverCtx = context.Background()
_, serverMsg = codec.WithNewMessage(serverCtx)
dataDecode, err := serverCodec.Decode(serverMsg, encodeData)
assert.Nil(t, dataDecode)
assert.Nil(t, err)
feedback, ok = clientMsg.StreamFrame().(*trpcpb.TrpcStreamFeedBackMeta)
assert.True(t, ok)
assert.Equal(t, uint32(10000), feedback.WindowSizeIncrement)
// server Encode feedbackframe
ctx = context.Background()
_, encodeMsg := codec.WithNewMessage(ctx)
serverFrameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_FEEDBACK),
StreamID: 100,
}
encodeMsg.WithFrameHead(serverFrameHead)
encodeMsg.WithStreamID(100)
feedbackMeta = &trpcpb.TrpcStreamFeedBackMeta{}
encodeMsg.WithStreamFrame(feedbackMeta)
feedbackMeta.WindowSizeIncrement = 10000
rspBuf, err := serverCodec.Encode(encodeMsg, nil)
assert.Nil(t, err)
assert.Equal(t, rspBuf, encodeData)
}
// TestDecodeEncodeFail tests codec error.
func TestDecodeEncodeFail(t *testing.T) {
ctx := trpc.BackgroundContext()
assert.NotNil(t, ctx)
clientCodec := codec.GetClient("trpc")
serverCodec := codec.GetServer("trpc")
// init first, ensuring that init frame is saved
initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x59, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0xa,
0x47, 0xa, 0x1d, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x74, 0x72, 0x70, 0x63, 0x2d, 0x67,
0x6f, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x73, 0x65,
0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f,
0x74, 0x72, 0x70, 0x63, 0x2e, 0x74, 0x65, 0x73, 0x74,
0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x47, 0x72,
0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f}
// server Decode
serverCtx := context.Background()
_, serverMsg := codec.WithNewMessage(serverCtx)
init, err := serverCodec.Decode(serverMsg, initResult)
assert.Nil(t, err)
assert.Nil(t, init)
assert.Equal(t, uint32(100), serverMsg.StreamID())
assert.NotNil(t, serverMsg.CallerService())
encodeData := []byte{0x9, 0x30, 0x1, 0x3, 0x0, 0x0, 0x0, 0x01, 0x0, 0x10,
0x0, 0x0, 0x0, 0x64, 0x0, 0x0, 0x8, 0x90, 0x4e}
// client decode total length error
clientCtx := context.Background()
_, clientMsg := codec.WithNewMessage(clientCtx)
rsp, err := clientCodec.Decode(clientMsg, encodeData)
assert.Nil(t, rsp)
assert.NotNil(t, err)
// client init unmarshal error
serverInitData := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x2d, 0x0, 0x0, 0x0, 0x0, 0x1, 0x64,
0x0, 0x0, 0x12, 0x1b, 0x90, 0x2, 0x12, 0x17, 0x73, 0x65, 0x54, 0x76, 0x65, 0x72, 0x20, 0x74, 0x65,
0x73, 0x74, 0x20, 0x61, 0x6e, 0x62,
0x6f, 0x64, 0x65, 0x20, 0x66, 0x61, 0x69, 0x6c}
initRsp, err := clientCodec.Decode(clientMsg, serverInitData)
assert.Nil(t, initRsp)
assert.NotNil(t, err)
t.Run("ClientStreamCodec unknown stream frame type error from message", func(t *testing.T) {
cc := trpc.NewClientStreamCodec()
_, err := cc.Encode(trpc.Message(context.Background()), nil)
require.Contains(t, err.Error(), "unknown stream frame type")
_, err = cc.Decode(trpc.Message(context.Background()), nil)
require.Contains(t, err.Error(), "unknown stream frame type")
})
t.Run("ServerStreamCodec unknown stream frame type error from message", func(t *testing.T) {
cc := trpc.NewServerStreamCodec()
_, err := cc.Encode(trpc.Message(context.Background()), nil)
require.Contains(t, err.Error(), "unknown stream frame type")
_, err = cc.Decode(trpc.Message(context.Background()), nil)
require.Contains(t, err.Error(), "unknown stream frame type")
})
}
// TestEncodeWithMetadata tests encode with metadata.
func TestEncodeWithMetadata(t *testing.T) {
msg := trpc.Message(ctx)
assert.NotNil(t, msg)
ctx := trpc.BackgroundContext()
assert.NotNil(t, ctx)
serverCodec := codec.GetServer("trpc")
clientCodec := codec.GetClient("trpc")
// Client encode
frameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT),
StreamID: 100,
}
initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x61, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0,
0xa, 0x4f, 0xa, 0x17, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x65, 0x72, 0x76,
0x65, 0x72, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 0x74, 0x72, 0x70, 0x63,
0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e,
0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2a,
0xc, 0xa, 0x3, 0x6b, 0x65, 0x79, 0x12, 0x5, 0x76, 0x61, 0x6c, 0x75, 0x65}
msg.WithFrameHead(frameHead)
msg.WithClientMetaData(codec.MetaData{"key": []byte("value")})
msg.WithStreamID(100)
msg.WithCallerServiceName("trpc.app.server.service")
msg.WithClientRPCName("/trpc.test.helloworld.Greeter/SayHello")
initBuf, err := clientCodec.Encode(msg, nil)
assert.Nil(t, err)
assert.Equal(t, initResult, initBuf)
// Server Decode
serverCtx, serverMsg := codec.WithNewMessage(context.Background())
initRsp, err := serverCodec.Decode(serverMsg, initResult)
assert.Nil(t, err)
assert.Nil(t, initRsp)
assert.Equal(t, trpc.GetMetaData(serverCtx, "key"), []byte("value"))
}
// TestEncodeWithDyeing tests encode with dyeing key.
func TestEncodeWithDyeing(t *testing.T) {
msg := trpc.Message(ctx)
assert.NotNil(t, msg)
ctx := trpc.BackgroundContext()
assert.NotNil(t, ctx)
serverCodec := codec.GetServer("trpc")
clientCodec := codec.GetClient("trpc")
// Client encode
frameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT),
StreamID: 100,
}
initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x74, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0,
0xa, 0x62, 0xa, 0x17, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x65, 0x72, 0x76,
0x65, 0x72, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 0x74, 0x72, 0x70, 0x63,
0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e,
0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
0x1, 0x2a, 0x1d, 0xa, 0xf, 0x74, 0x72, 0x70, 0x63, 0x2d, 0x64, 0x79, 0x65, 0x69, 0x6e, 0x67, 0x2d, 0x6b,
0x65, 0x79, 0x12, 0xa, 0x64, 0x79, 0x65, 0x69, 0x6e, 0x67, 0x2d, 0x6b, 0x65, 0x79}
msg.WithFrameHead(frameHead)
msg.WithDyeing(true)
msg.WithDyeingKey("dyeing-key")
msg.WithStreamID(100)
msg.WithCallerServiceName("trpc.app.server.service")
msg.WithClientRPCName("/trpc.test.helloworld.Greeter/SayHello")
initBuf, err := clientCodec.Encode(msg, nil)
assert.Nil(t, err)
assert.Equal(t, initResult, initBuf)
// Server Decode
serverCtx, serverMsg := codec.WithNewMessage(context.Background())
initRsp, err := serverCodec.Decode(serverMsg, initResult)
assert.Nil(t, err)
assert.Nil(t, initRsp)
assert.Equal(t, trpc.GetMetaData(serverCtx, trpc.DyeingKey), []byte("dyeing-key"))
}
// TestEncodeWithEnvTransfer tests encode with envtransfor.
func TestEncodeWithEnvTransfer(t *testing.T) {
msg := trpc.Message(ctx)
assert.NotNil(t, msg)
ctx := trpc.BackgroundContext()
assert.NotNil(t, ctx)
serverCodec := codec.GetServer("trpc")
clientCodec := codec.GetClient("trpc")
// Client encode
frameHead := &trpc.FrameHead{
FrameType: uint8(trpcpb.TrpcDataFrameType_TRPC_STREAM_FRAME),
StreamFrameType: uint8(trpcpb.TrpcStreamFrameType_TRPC_STREAM_FRAME_INIT),
StreamID: 100,
}
initResult := []byte{0x9, 0x30, 0x1, 0x1, 0x0, 0x0, 0x0, 0x6a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x64, 0x0, 0x0,
0xa, 0x58, 0xa, 0x17, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x73, 0x65, 0x72, 0x76,
0x65, 0x72, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x1a, 0x26, 0x2f, 0x74, 0x72, 0x70, 0x63,
0x2e, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e,
0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2f, 0x53, 0x61, 0x79, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2a,
0x15, 0xa, 0x8, 0x74, 0x72, 0x70, 0x63, 0x2d, 0x65, 0x6e, 0x76, 0x12, 0x9, 0x65, 0x6e, 0x76, 0x2d,
0x74, 0x72, 0x61, 0x6e, 0x73}
msg.WithFrameHead(frameHead)
msg.WithEnvTransfer("env-trans")
msg.WithStreamID(100)
msg.WithCallerServiceName("trpc.app.server.service")
msg.WithClientRPCName("/trpc.test.helloworld.Greeter/SayHello")
initBuf, err := clientCodec.Encode(msg, nil)
assert.Nil(t, err)
assert.Equal(t, initResult, initBuf)
// Server Decode
serverCtx, serverMsg := codec.WithNewMessage(context.Background())
initRsp, err := serverCodec.Decode(serverMsg, initResult)
assert.Nil(t, err)
assert.Nil(t, initRsp)
assert.Equal(t, trpc.GetMetaData(serverCtx, trpc.EnvTransfer), []byte("env-trans"))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/z1312655689/trpc-go.git
git@gitee.com:z1312655689/trpc-go.git
z1312655689
trpc-go
trpc-go
main

搜索帮助