Skip to content

Commit

Permalink
rft(getty): refactor getty unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dongjianhui03 committed Aug 16, 2021
1 parent d07457d commit b065630
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 156 deletions.
146 changes: 38 additions & 108 deletions remoting/getty/getty_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,39 @@ import (
hessian "github.com/apache/dubbo-go-hessian2"
"github.com/apache/dubbo-go/common"
. "github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/proxy/proxy_factory"
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/protocol"
"github.com/apache/dubbo-go/protocol/invocation"
"github.com/apache/dubbo-go/remoting"
)

func TestRunSuite(t *testing.T) {
svr, url := InitTest(t)
client := getClient(url)
initTestEnvironment(t)
userUrl := initUserUrl(t)
svr := getServer(userUrl)
client := getClient(userUrl)
assert.NotNil(t, client)
testRequestOneWay(t, client)
testClient_Call(t, client)
testClient_AsyncCall(t, client)
testClientCall(t, client)
testClientAsyncCall(t, client)
svr.Stop()
}

func testRequestOneWay(t *testing.T, client *Client) {
func initUserUrl(t *testing.T) *common.URL {
hessian.RegisterPOJO(&User{})
remoting.RegistryCodec("dubbo", &DubboTestCodec{})

request := remoting.NewRequest("2.0.2")
invocation := createInvocation("GetUser", nil, nil, []interface{}{"1", "username"},
[]reflect.Value{reflect.ValueOf("1"), reflect.ValueOf("username")})
attachment := map[string]string{INTERFACE_KEY: "com.ikurento.user.UserProvider"}
setAttachment(invocation, attachment)
request.Data = invocation
request.Event = false
request.TwoWay = false
err := client.Request(request, 3*time.Second, nil)
methods, err := common.ServiceMap.Register("com.ikurento.user.UserProvider", "dubbo", "", "", &UserProvider{})
assert.NoError(t, err)
assert.Equal(t, "GetBigPkg,GetUser,GetUser0,GetUser1,GetUser2,GetUser3,GetUser4,GetUser5,GetUser6", methods)

url, err := common.NewURL("dubbo://127.0.0.1:20060/com.ikurento.user.UserProvider?anyhost=true&" +
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
"environment=dev&interface=com.ikurento.user.UserProvider&ip=127.0.0.1&methods=GetUser%2C&" +
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
"side=provider&timeout=3000&timestamp=1556509797245&bean.name=UserProvider")
assert.NoError(t, err)

return url
}

func createInvocation(methodName string, callback interface{}, reply interface{}, arguments []interface{},
Expand All @@ -80,19 +84,25 @@ func setAttachment(invocation *invocation.RPCInvocation, attachments map[string]
}
}

func getClient(url *common.URL) *Client {
client := NewClient(Options{
ConnectTimeout: config.GetConsumerConfig().ConnectTimeout,
})
//////////////////////////////////
// test cases
//////////////////////////////////

if err := client.Connect(url); err != nil {
return nil
}
return client
}
func testRequestOneWay(t *testing.T, client *Client) {

func testClient_Call(t *testing.T, c *Client) {
request := remoting.NewRequest("2.0.2")
invocation := createInvocation("GetUser", nil, nil, []interface{}{"1", "username"},
[]reflect.Value{reflect.ValueOf("1"), reflect.ValueOf("username")})
attachment := map[string]string{INTERFACE_KEY: "com.ikurento.user.UserProvider"}
setAttachment(invocation, attachment)
request.Data = invocation
request.Event = false
request.TwoWay = false
err := client.Request(request, 3*time.Second, nil)
assert.NoError(t, err)
}

func testClientCall(t *testing.T, c *Client) {
testGetBigPkg(t, c)
testGetUser(t, c)
testGetUser0(t, c)
Expand Down Expand Up @@ -309,7 +319,7 @@ func testGetUser61(t *testing.T, c *Client) {
assert.Equal(t, User{Id: "1", Name: ""}, *user)
}

func testClient_AsyncCall(t *testing.T, client *Client) {
func testClientAsyncCall(t *testing.T, client *Client) {
user := &User{}
wg := sync.WaitGroup{}
request := remoting.NewRequest("2.0.2")
Expand Down Expand Up @@ -337,86 +347,6 @@ func testClient_AsyncCall(t *testing.T, client *Client) {
wg.Wait()
}

func InitTest(t *testing.T) (*Server, *common.URL) {

hessian.RegisterPOJO(&User{})
remoting.RegistryCodec("dubbo", &DubboTestCodec{})

methods, err := common.ServiceMap.Register("com.ikurento.user.UserProvider", "dubbo", "", "", &UserProvider{})
assert.NoError(t, err)
assert.Equal(t, "GetBigPkg,GetUser,GetUser0,GetUser1,GetUser2,GetUser3,GetUser4,GetUser5,GetUser6", methods)

// config
SetClientConf(ClientConfig{
ConnectionNum: 2,
HeartbeatPeriod: "5s",
SessionTimeout: "20s",
GettySessionParam: GettySessionParam{
CompressEncoding: false,
TcpNoDelay: true,
TcpKeepAlive: true,
KeepAlivePeriod: "120s",
TcpRBufSize: 262144,
TcpWBufSize: 65536,
PkgWQSize: 512,
TcpReadTimeout: "4s",
TcpWriteTimeout: "5s",
WaitTimeout: "1s",
MaxMsgLen: 10240000000,
SessionName: "client",
},
})
assert.NoError(t, clientConf.CheckValidity())
SetServerConfig(ServerConfig{
SessionNumber: 700,
SessionTimeout: "20s",
GettySessionParam: GettySessionParam{
CompressEncoding: false,
TcpNoDelay: true,
TcpKeepAlive: true,
KeepAlivePeriod: "120s",
TcpRBufSize: 262144,
TcpWBufSize: 65536,
PkgWQSize: 512,
TcpReadTimeout: "1s",
TcpWriteTimeout: "5s",
WaitTimeout: "1s",
MaxMsgLen: 10240000000,
SessionName: "server",
}})
assert.NoError(t, srvConf.CheckValidity())

url, err := common.NewURL("dubbo://127.0.0.1:20060/com.ikurento.user.UserProvider?anyhost=true&" +
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
"environment=dev&interface=com.ikurento.user.UserProvider&ip=127.0.0.1&methods=GetUser%2C&" +
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
"side=provider&timeout=3000&timestamp=1556509797245&bean.name=UserProvider")
assert.NoError(t, err)
// init server
userProvider := &UserProvider{}
_, err = common.ServiceMap.Register("", url.Protocol, "", "0.0.1", userProvider)
assert.NoError(t, err)
invoker := &proxy_factory.ProxyInvoker{
BaseInvoker: *protocol.NewBaseInvoker(url),
}
handler := func(invocation *invocation.RPCInvocation) protocol.RPCResult {
//result := protocol.RPCResult{}
r := invoker.Invoke(context.Background(), invocation)
result := protocol.RPCResult{
Err: r.Error(),
Rest: r.Result(),
Attrs: r.Attachments(),
}
return result
}
server := NewServer(url, handler)
server.Start()

time.Sleep(time.Second * 2)

return server, url
}

//////////////////////////////////
// provider
//////////////////////////////////
Expand Down Expand Up @@ -451,7 +381,7 @@ func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User
}

func (u *UserProvider) GetUser0(id string, k *User, name string) (User, error) {
// fix testClient_AsyncCall assertion
// fix testClientAsyncCall assertion
time.Sleep(1 * time.Second)
return User{Id: id, Name: name}, nil
}
Expand Down
128 changes: 80 additions & 48 deletions remoting/getty/readwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,61 +32,31 @@ import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/proxy/proxy_factory"
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/protocol"
"github.com/apache/dubbo-go/protocol/dubbo/impl"
"github.com/apache/dubbo-go/protocol/invocation"
"github.com/apache/dubbo-go/remoting"
)

func TestTCPPackageHandle(t *testing.T) {
svr, url := getServer(t)
client := getClient(url)
initTestEnvironment(t)
adminUrl := initAdminUrl(t)
svr := getServer(adminUrl)
client := getClient(adminUrl)
testDecodeTCPPackage(t, svr, client)
svr.Stop()
}

func testDecodeTCPPackage(t *testing.T, svr *Server, client *Client) {
request := remoting.NewRequest("2.0.2")
ap := &AdminProvider{}
rpcInvocation := createInvocation("GetAdmin", nil, nil, []interface{}{[]interface{}{"1", "username"}},
[]reflect.Value{reflect.ValueOf([]interface{}{"1", "username"}), reflect.ValueOf(ap)})
attachment := map[string]string{
constant.INTERFACE_KEY: "com.ikurento.user.AdminProvider",
constant.PATH_KEY: "AdminProvider",
constant.VERSION_KEY: "1.0.0",
}
setAttachment(rpcInvocation, attachment)
request.Data = rpcInvocation
request.Event = false
request.TwoWay = false

pkgWriteHandler := NewRpcClientPackageHandler(client)
pkgBytes, err := pkgWriteHandler.Write(nil, request)
assert.NoError(t, err)
pkgReadHandler := NewRpcServerPackageHandler(svr)
_, pkgLen, err := pkgReadHandler.Read(nil, pkgBytes)
assert.NoError(t, err)
assert.Equal(t, pkgLen, len(pkgBytes))
//////////////////////////////////
// before execute getty_test
// 1. init config
// 2. init url
// 3. init server
// 4. init client
//////////////////////////////////

// simulate incomplete tcp package
incompletePkgLen := len(pkgBytes) - 10
assert.True(t, incompletePkgLen >= impl.HEADER_LENGTH, "header buffer too short")
incompletePkg := pkgBytes[0 : incompletePkgLen-1]
pkg, pkgLen, err := pkgReadHandler.Read(nil, incompletePkg)
assert.NoError(t, err)
assert.Equal(t, pkg, nil)
assert.Equal(t, pkgLen, 0)
}

func getServer(t *testing.T) (*Server, *common.URL) {
hessian.RegisterPOJO(&User{})
remoting.RegistryCodec("dubbo", &DubboTestCodec{})

methods, err := common.ServiceMap.Register("com.ikurento.user.AdminProvider", "dubbo", "", "", &AdminProvider{})
assert.NoError(t, err)
assert.Equal(t, "GetAdmin", methods)

// config
func initTestEnvironment(t *testing.T) {
SetClientConf(ClientConfig{
ConnectionNum: 2,
HeartbeatPeriod: "5s",
Expand Down Expand Up @@ -125,17 +95,27 @@ func getServer(t *testing.T) (*Server, *common.URL) {
SessionName: "server",
}})
assert.NoError(t, srvConf.CheckValidity())
}

url, err := common.NewURL("dubbo://127.0.0.1:20061/com.ikurento.user.AdminProvider?anyhost=true&" +
func initAdminUrl(t *testing.T) *common.URL {
hessian.RegisterPOJO(&User{})
remoting.RegistryCodec("dubbo", &DubboTestCodec{})

url, err := common.NewURL("dubbo://127.0.0.1:20061/?anyhost=true&" +
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
"environment=dev&interface=com.ikurento.user.AdminProvider&ip=127.0.0.1&methods=GetAdmin%2C&" +
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
"side=provider&timeout=3000&timestamp=1556509797245&bean.name=AdminProvider")
assert.NoError(t, err)
// init server
adminProvider := &AdminProvider{}
_, err = common.ServiceMap.Register("com.ikurento.user.AdminProvider", url.Protocol, "", "0.0.1", adminProvider)

methods, err := common.ServiceMap.Register("com.ikurento.user.AdminProvider", url.Protocol, "", "", &AdminProvider{})
assert.NoError(t, err)
assert.Equal(t, "GetAdmin", methods)

return url
}

func getServer(url *common.URL) *Server {
invoker := &proxy_factory.ProxyInvoker{
BaseInvoker: *protocol.NewBaseInvoker(url),
}
Expand All @@ -154,9 +134,61 @@ func getServer(t *testing.T) (*Server, *common.URL) {

time.Sleep(time.Second * 2)

return server, url
return server
}

func getClient(url *common.URL) *Client {
client := NewClient(Options{
ConnectTimeout: config.GetConsumerConfig().ConnectTimeout,
})

if err := client.Connect(url); err != nil {
return nil
}
return client
}

//////////////////////////////////
// test cases
//////////////////////////////////

func testDecodeTCPPackage(t *testing.T, svr *Server, client *Client) {
request := remoting.NewRequest("2.0.2")
ap := &AdminProvider{}
rpcInvocation := createInvocation("GetAdmin", nil, nil, []interface{}{[]interface{}{"1", "username"}},
[]reflect.Value{reflect.ValueOf([]interface{}{"1", "username"}), reflect.ValueOf(ap)})
attachment := map[string]string{
constant.INTERFACE_KEY: "com.ikurento.user.AdminProvider",
constant.PATH_KEY: "AdminProvider",
constant.VERSION_KEY: "1.0.0",
}
setAttachment(rpcInvocation, attachment)
request.Data = rpcInvocation
request.Event = false
request.TwoWay = false

pkgWriteHandler := NewRpcClientPackageHandler(client)
pkgBytes, err := pkgWriteHandler.Write(nil, request)
assert.NoError(t, err)
pkgReadHandler := NewRpcServerPackageHandler(svr)
_, pkgLen, err := pkgReadHandler.Read(nil, pkgBytes)
assert.NoError(t, err)
assert.Equal(t, pkgLen, len(pkgBytes))

// simulate incomplete tcp package
incompletePkgLen := len(pkgBytes) - 10
assert.True(t, incompletePkgLen >= impl.HEADER_LENGTH, "header buffer too short")
incompletePkg := pkgBytes[0 : incompletePkgLen-1]
pkg, pkgLen, err := pkgReadHandler.Read(nil, incompletePkg)
assert.NoError(t, err)
assert.Equal(t, pkg, nil)
assert.Equal(t, pkgLen, 0)
}

//////////////////////////////////
// provider
//////////////////////////////////

type AdminProvider struct {
}

Expand Down

0 comments on commit b065630

Please sign in to comment.