Skip to content

Commit

Permalink
feat: add thrift default value check (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
joway authored Jun 6, 2024
1 parent c6ae2a6 commit 64a6da0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 29 deletions.
1 change: 1 addition & 0 deletions idl/instparam.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace go thrift.instparam
struct SubMessage {
1: optional i64 id;
2: optional string value;
3: optional string defaultValue = "default";
}

struct Message {
Expand Down
2 changes: 2 additions & 0 deletions idl/stability.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct STRequest {
14: required string flagMsg
15: required string framework = "kitex",
16: optional string mockCost,
17: optional string defaultValue = "default";

255: optional base.Base Base,
}
Expand All @@ -55,6 +56,7 @@ struct STResponse {
1: string str,
2: map<string, string> mp,
3: required string flagMsg
4: optional string defaultValue = "default";
255: optional base.BaseResp BaseResp,
}

Expand Down
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ if [[ -n $LOCAL_REPO ]]; then
go_install ${LOCAL_REPO}/tool/cmd/kitex
cd -
else
go_install github.com/cloudwego/kitex/tool/cmd/kitex@latest
go_install github.com/cloudwego/kitex/tool/cmd/kitex@develop
fi

go mod init github.com/cloudwego/kitex-tests
Expand Down
4 changes: 4 additions & 0 deletions thriftrpc/abctest/abc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/bytedance/gopkg/cloud/metainfo"

"github.com/cloudwego/kitex-tests/kitex_gen/thrift/instparam"
"github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability"
"github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice"
Expand Down Expand Up @@ -89,6 +90,9 @@ type stServiceHandler struct {

// TestSTReq .
func (h *stServiceHandler) TestSTReq(ctx context.Context, req *stability.STRequest) (r *stability.STResponse, err error) {
if req.DefaultValue != "default" {
return nil, fmt.Errorf("req DefaultValue is not right, expect=default, actual=%s", req.DefaultValue)
}
if h.cli != nil {
// it is service b, both has transient and persist key
val, ok := metainfo.GetValue(ctx, transientKV)
Expand Down
15 changes: 7 additions & 8 deletions thriftrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/apache/thrift/lib/go/thrift"
"github.com/bytedance/gopkg/cloud/metainfo"

"github.com/cloudwego/kitex-tests/kitex_gen/thrift/instparam"
"github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability"
"github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice"
Expand Down Expand Up @@ -210,14 +211,12 @@ func CreateNoDefSerdesSTRequest(ctx context.Context) (context.Context, *stabilit
// CreateObjReq .
func CreateObjReq(ctx context.Context) (context.Context, *instparam.ObjReq) {
id := thrift.Int64Ptr(int64(rand.Intn(100)))
subMsg1 := &instparam.SubMessage{
Id: id,
Value: thrift.StringPtr(utils.RandomString(100)),
}
subMsg2 := &instparam.SubMessage{
Id: thrift.Int64Ptr(math.MaxInt64),
Value: thrift.StringPtr(utils.RandomString(10)),
}
subMsg1 := instparam.NewSubMessage()
subMsg1.Id = id
subMsg1.Value = thrift.StringPtr(utils.RandomString(100))
subMsg2 := instparam.NewSubMessage()
subMsg2.Id = thrift.Int64Ptr(math.MaxInt64)
subMsg2.Value = thrift.StringPtr(utils.RandomString(100))
subMsgList := []*instparam.SubMessage{subMsg1, subMsg2}

msg := instparam.NewMessage()
Expand Down
36 changes: 27 additions & 9 deletions thriftrpc/normalcall/normalcall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"testing"
"time"

"github.com/cloudwego/kitex-tests/kitex_gen/thrift/instparam"
"github.com/cloudwego/kitex-tests/pkg/test"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/client/callopt"
"github.com/cloudwego/kitex/pkg/circuitbreak"
Expand All @@ -36,12 +38,12 @@ import (
"github.com/cloudwego/kitex/server"
"github.com/cloudwego/kitex/transport"

stservice_noDefSerdes "github.com/cloudwego/kitex-tests/kitex_gen_noDefSerdes/thrift/stability/stservice"

"github.com/cloudwego/kitex-tests/common"
"github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability"
"github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice"
stservice_noDefSerdes "github.com/cloudwego/kitex-tests/kitex_gen_noDefSerdes/thrift/stability/stservice"
stservice_slim "github.com/cloudwego/kitex-tests/kitex_gen_slim/thrift/stability/stservice"
"github.com/cloudwego/kitex-tests/pkg/test"
"github.com/cloudwego/kitex-tests/thriftrpc"
)

Expand Down Expand Up @@ -128,21 +130,37 @@ func getNoDefSerdesKitexClient(p transport.Protocol, hostPorts []string, opts ..
}, opts...)
}

func testSTArgs(t *testing.T, req *stability.STRequest, resp *stability.STResponse) {
test.Assert(t, req.Str == resp.Str)
test.Assert(t, req.DefaultValue == stability.STRequest_DefaultValue_DEFAULT)
test.Assert(t, resp.DefaultValue == stability.STResponse_DefaultValue_DEFAULT)
}

func testObjArgs(t *testing.T, req *instparam.ObjReq, resp *instparam.ObjResp) {
test.Assert(t, req.FlagMsg == resp.FlagMsg)
for _, v := range resp.MsgMap {
test.Assert(t, v.DefaultValue == instparam.SubMessage_DefaultValue_DEFAULT)
}
for _, v := range resp.SubMsgs {
test.Assert(t, v.DefaultValue == instparam.SubMessage_DefaultValue_DEFAULT)
}
}

func TestStTReq(t *testing.T) {
cli = getKitexClient(transport.PurePayload)

ctx, stReq := thriftrpc.CreateSTRequest(context.Background())
stResp, err := cli.TestSTReq(ctx, stReq)
test.Assert(t, err == nil, err)
test.Assert(t, stReq.Str == stResp.Str)
testSTArgs(t, stReq, stResp)

stResp, err = cli.TestSTReq(ctx, stReq)
test.Assert(t, err == nil, err)
test.Assert(t, stReq.Str == stResp.Str)
testSTArgs(t, stReq, stResp)

stResp, err = cli.TestSTReq(ctx, stReq)
test.Assert(t, err == nil, err)
test.Assert(t, stReq.Str == stResp.Str)
testSTArgs(t, stReq, stResp)
}

func TestStTReqWithTTHeader(t *testing.T) {
Expand All @@ -151,7 +169,7 @@ func TestStTReqWithTTHeader(t *testing.T) {
ctx, stReq := thriftrpc.CreateSTRequest(context.Background())
stResp, err := cli.TestSTReq(ctx, stReq)
test.Assert(t, err == nil, err)
test.Assert(t, stReq.Str == stResp.Str)
testSTArgs(t, stReq, stResp)
}

func TestStTReqWithFramed(t *testing.T) {
Expand All @@ -160,7 +178,7 @@ func TestStTReqWithFramed(t *testing.T) {
ctx, stReq := thriftrpc.CreateSTRequest(context.Background())
stResp, err := cli.TestSTReq(ctx, stReq, callopt.WithRPCTimeout(1*time.Second))
test.Assert(t, err == nil, err)
test.Assert(t, stReq.Str == stResp.Str)
testSTArgs(t, stReq, stResp)
}

func TestStTReqWithTTHeaderFramed(t *testing.T) {
Expand All @@ -169,7 +187,7 @@ func TestStTReqWithTTHeaderFramed(t *testing.T) {
ctx, stReq := thriftrpc.CreateSTRequest(context.Background())
stResp, err := cli.TestSTReq(ctx, stReq, callopt.WithRPCTimeout(1*time.Second))
test.Assert(t, err == nil, err)
test.Assert(t, stReq.Str == stResp.Str)
testSTArgs(t, stReq, stResp)
}

func TestObjReq(t *testing.T) {
Expand All @@ -179,7 +197,7 @@ func TestObjReq(t *testing.T) {
objReq.FlagMsg = "ObjReq"
objResp, err := cli.TestObjReq(ctx, objReq)
test.Assert(t, err == nil, err)
test.Assert(t, objReq.FlagMsg == objResp.FlagMsg)
testObjArgs(t, objReq, objResp)
}

func TestException(t *testing.T) {
Expand Down
21 changes: 10 additions & 11 deletions thriftrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ type STServiceHandler struct{}

// TestSTReq .
func (*STServiceHandler) TestSTReq(ctx context.Context, req *stability.STRequest) (r *stability.STResponse, err error) {
resp := &stability.STResponse{
Str: req.Str,
Mp: req.StringMap,
FlagMsg: req.FlagMsg,
}
resp := stability.NewSTResponse()
resp.Str = req.Str
resp.Mp = req.StringMap
resp.FlagMsg = req.FlagMsg
resp.DefaultValue = req.DefaultValue
if req.MockCost != nil {
if mockSleep, err := time.ParseDuration(*req.MockCost); err != nil {
return nil, err
Expand All @@ -145,12 +145,11 @@ func (*STServiceHandler) TestSTReq(ctx context.Context, req *stability.STRequest

// TestObjReq .
func (*STServiceHandler) TestObjReq(ctx context.Context, req *instparam.ObjReq) (r *instparam.ObjResp, err error) {
resp := &instparam.ObjResp{
Msg: req.Msg,
MsgSet: req.MsgSet,
MsgMap: req.MsgMap,
FlagMsg: req.FlagMsg,
}
resp := instparam.NewObjResp()
resp.Msg = req.Msg
resp.MsgSet = req.MsgSet
resp.MsgMap = req.MsgMap
resp.FlagMsg = req.FlagMsg
if req.MockCost != nil {
if mockSleep, err := time.ParseDuration(*req.MockCost); err != nil {
return nil, err
Expand Down

0 comments on commit 64a6da0

Please sign in to comment.