Skip to content

Commit

Permalink
test(generic): add test for generic with CombineServices parse mode (c…
Browse files Browse the repository at this point in the history
  • Loading branch information
Marina-Sakai authored Oct 10, 2024
1 parent 382d8d7 commit c5eb34b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
59 changes: 37 additions & 22 deletions generic/json/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,41 @@ import (
"testing"
"time"

"github.com/cloudwego/kitex-tests/pkg/test"
"github.com/cloudwego/kitex-tests/pkg/utils/serverutils"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/client/genericclient"
"github.com/cloudwego/kitex/pkg/generic"
"github.com/cloudwego/kitex/pkg/generic/thrift"
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/cloudwego/kitex/pkg/transmeta"
"github.com/cloudwego/kitex/transport"

"github.com/cloudwego/kitex-tests/pkg/test"
"github.com/cloudwego/kitex-tests/pkg/utils/serverutils"
)

var (
testaddr string
genericAddr string
req = map[string]interface{}{
"Msg": "hello",
"I8": int8(1),
"I16": int16(1),
"I32": int32(1),
"I64": int64(1),
"Map": map[string]interface{}{
"hello": "world",
},
"Set": []interface{}{"hello", "world"},
"List": []interface{}{"hello", "world"},
"ErrorCode": int32(1),
"Info": map[string]interface{}{
"Map": map[string]interface{}{
"hello": "world",
},
"ID": int64(232324),
},
}
reqStr, _ = json.Marshal(req)
)

func TestMain(m *testing.M) {
Expand All @@ -57,26 +79,6 @@ func TestClient(t *testing.T) {
cli, err := genericclient.NewClient("a.b.c", g, client.WithHostPorts(testaddr))
test.Assert(t, err == nil)

req := map[string]interface{}{
"Msg": "hello",
"I8": int8(1),
"I16": int16(1),
"I32": int32(1),
"I64": int64(1),
"Map": map[string]interface{}{
"hello": "world",
},
"Set": []interface{}{"hello", "world"},
"List": []interface{}{"hello", "world"},
"ErrorCode": int32(1),
"Info": map[string]interface{}{
"Map": map[string]interface{}{
"hello": "world",
},
"ID": int64(232324),
},
}
reqStr, _ := json.Marshal(req)
_, err = cli.GenericCall(context.Background(), "Echo", string(reqStr))
test.Assert(t, err == nil)
}
Expand Down Expand Up @@ -137,3 +139,16 @@ func TestBizErr(t *testing.T) {
test.Assert(t, bizerr.BizStatusCode() == 404)
test.Assert(t, bizerr.BizMessage() == "not found")
}

func TestCombinedServicesParseMode(t *testing.T) {
p, err := generic.NewThriftFileProviderWithOption("../../idl/tenant.thrift", []generic.ThriftIDLProviderOption{generic.WithParseMode(thrift.CombineServices)})
test.Assert(t, err == nil)
g, err := generic.JSONThriftGeneric(p)
test.Assert(t, err == nil)

cli, err := genericclient.NewClient("a.b.c", g, client.WithHostPorts(testaddr))
test.Assert(t, err == nil)

_, err = cli.GenericCall(context.Background(), "Echo", string(reqStr))
test.Assert(t, err == nil)
}
19 changes: 15 additions & 4 deletions generic/json/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ import (
"net"
"reflect"

"github.com/cloudwego/kitex-tests/kitex_gen/thrift/tenant/echoservice"
"github.com/cloudwego/kitex/pkg/generic"
"github.com/cloudwego/kitex/pkg/transmeta"
"github.com/cloudwego/kitex/server"
"github.com/cloudwego/kitex/server/genericserver"

"github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice"
"github.com/cloudwego/kitex-tests/kitex_gen/thrift/tenant/echoservice"
"github.com/cloudwego/kitex-tests/thriftrpc"
)

func assert(expected, actual interface{}) error {
Expand All @@ -36,13 +39,21 @@ func assert(expected, actual interface{}) error {

func runServer(listenaddr string) server.Server {
addr, _ := net.ResolveTCPAddr("tcp", listenaddr)
svc := echoservice.NewServer(new(EchoServiceImpl), server.WithServiceAddr(addr))
svr := server.NewServer(server.WithServiceAddr(addr))
err := echoservice.RegisterService(svr, new(EchoServiceImpl))
if err != nil {
panic(err)
}
err = stservice.RegisterService(svr, new(thriftrpc.STServiceHandler))
if err != nil {
panic(err)
}
go func() {
if err := svc.Run(); err != nil {
if err := svr.Run(); err != nil {
panic(err)
}
}()
return svc
return svr
}

func runGenericServer(listenaddr string) server.Server {
Expand Down
3 changes: 3 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ if [[ -n $LOCAL_REPO ]]; then
go mod edit -replace github.com/cloudwego/kitex=${LOCAL_REPO}
fi

go mod edit -replace github.com/cloudwego/kitex=github.com/Marina-Sakai/[email protected]
go get github.com/cloudwego/dynamicgo@main

go mod tidy

echo -e "\nupdating dependencies ... done\n"
Expand Down

0 comments on commit c5eb34b

Please sign in to comment.