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 1d112da
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 166 deletions.
4 changes: 2 additions & 2 deletions remoting/getty/dubbo_codec_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type DubboTestCodec struct {
// encode request for transport
func (c *DubboTestCodec) EncodeRequest(request *remoting.Request) (*bytes.Buffer, error) {
if request.Event {
return c.encodeHeartbeartReqeust(request)
return c.encodeHeartbeatRequest(request)
}

invoc, ok := request.Data.(*invocation.RPCInvocation)
Expand Down Expand Up @@ -100,7 +100,7 @@ func (c *DubboTestCodec) EncodeRequest(request *remoting.Request) (*bytes.Buffer
}

// encode heartbeat request
func (c *DubboTestCodec) encodeHeartbeartReqeust(request *remoting.Request) (*bytes.Buffer, error) {
func (c *DubboTestCodec) encodeHeartbeatRequest(request *remoting.Request) (*bytes.Buffer, error) {
header := impl.DubboHeader{
Type: impl.PackageHeartbeat,
SerialID: constant.S_Hessian2,
Expand Down
152 changes: 37 additions & 115 deletions remoting/getty/getty_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,50 @@ 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)
server := getServer(userUrl)
client := getClient(userUrl)
assert.NotNil(t, client)

testRequestOneWay(t, client)
testClient_Call(t, client)
testClient_AsyncCall(t, client)
svr.Stop()
testClientCall(t, client)
testClientAsyncCall(t, client)
server.Stop()
}

//////////////////////////////////
// init special url
//////////////////////////////////

func initUserUrl(t *testing.T) *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)

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
}

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

func testRequestOneWay(t *testing.T, client *Client) {

request := remoting.NewRequest("2.0.2")
Expand All @@ -67,32 +94,7 @@ func testRequestOneWay(t *testing.T, client *Client) {
assert.NoError(t, err)
}

func createInvocation(methodName string, callback interface{}, reply interface{}, arguments []interface{},
parameterValues []reflect.Value) *invocation.RPCInvocation {
return invocation.NewRPCInvocationWithOptions(invocation.WithMethodName(methodName),
invocation.WithArguments(arguments), invocation.WithReply(reply),
invocation.WithCallBack(callback), invocation.WithParameterValues(parameterValues))
}

func setAttachment(invocation *invocation.RPCInvocation, attachments map[string]string) {
for key, value := range attachments {
invocation.SetAttachments(key, value)
}
}

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

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

func testClient_Call(t *testing.T, c *Client) {

func testClientCall(t *testing.T, c *Client) {
testGetBigPkg(t, c)
testGetUser(t, c)
testGetUser0(t, c)
Expand Down Expand Up @@ -309,7 +311,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 +339,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 +373,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

0 comments on commit 1d112da

Please sign in to comment.