dubbo.apache.org/dubbo-go/v3@v3.1.1/remoting/getty/getty_client_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package getty 19 20 import ( 21 "bytes" 22 "context" 23 "reflect" 24 "sync" 25 "testing" 26 "time" 27 ) 28 29 import ( 30 hessian "github.com/apache/dubbo-go-hessian2" 31 32 perrors "github.com/pkg/errors" 33 34 "github.com/stretchr/testify/assert" 35 ) 36 37 import ( 38 "dubbo.apache.org/dubbo-go/v3/common" 39 . "dubbo.apache.org/dubbo-go/v3/common/constant" 40 "dubbo.apache.org/dubbo-go/v3/config" 41 "dubbo.apache.org/dubbo-go/v3/protocol" 42 "dubbo.apache.org/dubbo-go/v3/protocol/invocation" 43 "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory" 44 "dubbo.apache.org/dubbo-go/v3/remoting" 45 ) 46 47 func TestRunSuite(t *testing.T) { 48 svr, url := InitTest(t) 49 client := getClient(url) 50 assert.NotNil(t, client) 51 testRequestOneWay(t, client) 52 //testClient_Call(t, client) 53 testClient_AsyncCall(t, client) 54 svr.Stop() 55 } 56 57 func testRequestOneWay(t *testing.T, client *Client) { 58 request := remoting.NewRequest("2.0.2") 59 invocation := createInvocation("GetUser", nil, nil, []interface{}{"1", "username"}, 60 []reflect.Value{reflect.ValueOf("1"), reflect.ValueOf("username")}) 61 attachment := map[string]string{InterfaceKey: "com.ikurento.user.UserProvider"} 62 setAttachment(invocation, attachment) 63 request.Data = invocation 64 request.Event = false 65 request.TwoWay = false 66 err := client.Request(request, 3*time.Second, nil) 67 assert.NoError(t, err) 68 } 69 70 func createInvocation(methodName string, callback interface{}, reply interface{}, arguments []interface{}, 71 parameterValues []reflect.Value) *invocation.RPCInvocation { 72 return invocation.NewRPCInvocationWithOptions(invocation.WithMethodName(methodName), 73 invocation.WithArguments(arguments), invocation.WithReply(reply), 74 invocation.WithCallBack(callback), invocation.WithParameterValues(parameterValues)) 75 } 76 77 func setAttachment(invocation *invocation.RPCInvocation, attachments map[string]string) { 78 for key, value := range attachments { 79 invocation.SetAttachment(key, value) 80 } 81 } 82 83 func getClient(url *common.URL) *Client { 84 client := NewClient(Options{ 85 // todo fix timeout 86 ConnectTimeout: 3 * time.Second, // config.GetConsumerConfig().ConnectTimeout, 87 }) 88 if err := client.Connect(url); err != nil { 89 return nil 90 } 91 return client 92 } 93 94 func testClient_Call(t *testing.T, c *Client) { 95 testGetBigPkg(t, c) 96 testGetUser(t, c) 97 testGetUser0(t, c) 98 testGetUser1(t, c) 99 testGetUser2(t, c) 100 testGetUser3(t, c) 101 testGetUser4(t, c) 102 testGetUser5(t, c) 103 testGetUser6(t, c) 104 testGetUser61(t, c) 105 } 106 107 func testGetBigPkg(t *testing.T, c *Client) { 108 user := &User{} 109 request := remoting.NewRequest("2.0.2") 110 invocation := createInvocation("GetBigPkg", nil, nil, []interface{}{[]interface{}{nil}, user}, 111 []reflect.Value{reflect.ValueOf([]interface{}{nil}), reflect.ValueOf(user)}) 112 attachment := map[string]string{InterfaceKey: "com.ikurento.user.UserProvider"} 113 setAttachment(invocation, attachment) 114 request.Data = invocation 115 request.Event = false 116 request.TwoWay = true 117 pendingResponse := remoting.NewPendingResponse(request.ID) 118 pendingResponse.Reply = user 119 remoting.AddPendingResponse(pendingResponse) 120 err := c.Request(request, 8*time.Second, pendingResponse) 121 assert.NoError(t, err) 122 assert.NotEqual(t, "", user.ID) 123 assert.NotEqual(t, "", user.Name) 124 } 125 126 func testGetUser(t *testing.T, c *Client) { 127 user := &User{} 128 request := remoting.NewRequest("2.0.2") 129 invocation := createInvocation("GetUser", nil, nil, []interface{}{"1", "username"}, 130 []reflect.Value{reflect.ValueOf("1"), reflect.ValueOf("username")}) 131 attachment := map[string]string{InterfaceKey: "com.ikurento.user.UserProvider"} 132 setAttachment(invocation, attachment) 133 request.Data = invocation 134 request.Event = false 135 request.TwoWay = true 136 pendingResponse := remoting.NewPendingResponse(request.ID) 137 pendingResponse.Reply = user 138 remoting.AddPendingResponse(pendingResponse) 139 err := c.Request(request, 3*time.Second, pendingResponse) 140 assert.NoError(t, err) 141 assert.Equal(t, User{ID: "1", Name: "username"}, *user) 142 } 143 144 func testGetUser0(t *testing.T, c *Client) { 145 var ( 146 user *User 147 err error 148 ) 149 user = &User{} 150 request := remoting.NewRequest("2.0.2") 151 invocation := createInvocation("GetUser0", nil, nil, []interface{}{"1", nil, "username"}, 152 []reflect.Value{reflect.ValueOf("1"), reflect.ValueOf(nil), reflect.ValueOf("username")}) 153 attachment := map[string]string{InterfaceKey: "com.ikurento.user.UserProvider"} 154 setAttachment(invocation, attachment) 155 request.Data = invocation 156 request.Event = false 157 request.TwoWay = true 158 rsp := remoting.NewPendingResponse(request.ID) 159 rsp.SetResponse(remoting.NewResponse(request.ID, "2.0.2")) 160 remoting.AddPendingResponse(rsp) 161 rsp.Reply = user 162 err = c.Request(request, 3*time.Second, rsp) 163 assert.NoError(t, err) 164 assert.Equal(t, User{ID: "1", Name: "username"}, *user) 165 } 166 167 func testGetUser1(t *testing.T, c *Client) { 168 var err error 169 request := remoting.NewRequest("2.0.2") 170 invocation := createInvocation("GetUser1", nil, nil, []interface{}{}, 171 []reflect.Value{}) 172 attachment := map[string]string{InterfaceKey: "com.ikurento.user.UserProvider"} 173 setAttachment(invocation, attachment) 174 request.Data = invocation 175 request.Event = false 176 request.TwoWay = true 177 pendingResponse := remoting.NewPendingResponse(request.ID) 178 user := &User{} 179 pendingResponse.Reply = user 180 remoting.AddPendingResponse(pendingResponse) 181 err = c.Request(request, 3*time.Second, pendingResponse) 182 assert.NoError(t, err) 183 } 184 185 func testGetUser2(t *testing.T, c *Client) { 186 var err error 187 request := remoting.NewRequest("2.0.2") 188 invocation := createInvocation("GetUser2", nil, nil, []interface{}{}, 189 []reflect.Value{}) 190 attachment := map[string]string{InterfaceKey: "com.ikurento.user.UserProvider"} 191 setAttachment(invocation, attachment) 192 request.Data = invocation 193 request.Event = false 194 request.TwoWay = true 195 pendingResponse := remoting.NewPendingResponse(request.ID) 196 remoting.AddPendingResponse(pendingResponse) 197 err = c.Request(request, 3*time.Second, pendingResponse) 198 assert.EqualError(t, err, "error") 199 } 200 201 func testGetUser3(t *testing.T, c *Client) { 202 var err error 203 request := remoting.NewRequest("2.0.2") 204 invocation := createInvocation("GetUser3", nil, nil, []interface{}{}, 205 []reflect.Value{}) 206 attachment := map[string]string{ 207 InterfaceKey: "com.ikurento.user.UserProvider", 208 } 209 setAttachment(invocation, attachment) 210 request.Data = invocation 211 request.Event = false 212 request.TwoWay = true 213 pendingResponse := remoting.NewPendingResponse(request.ID) 214 user2 := []interface{}{} 215 pendingResponse.Reply = &user2 216 remoting.AddPendingResponse(pendingResponse) 217 err = c.Request(request, 3*time.Second, pendingResponse) 218 assert.NoError(t, err) 219 assert.Equal(t, &User{ID: "1", Name: "username"}, user2[0]) 220 } 221 222 func testGetUser4(t *testing.T, c *Client) { 223 var err error 224 request := remoting.NewRequest("2.0.2") 225 invocation := invocation.NewRPCInvocation("GetUser4", []interface{}{[]interface{}{"1", "username"}}, nil) 226 attachment := map[string]string{InterfaceKey: "com.ikurento.user.UserProvider"} 227 setAttachment(invocation, attachment) 228 request.Data = invocation 229 request.Event = false 230 request.TwoWay = true 231 pendingResponse := remoting.NewPendingResponse(request.ID) 232 user2 := []interface{}{} 233 pendingResponse.Reply = &user2 234 remoting.AddPendingResponse(pendingResponse) 235 err = c.Request(request, 3*time.Second, pendingResponse) 236 assert.NoError(t, err) 237 assert.Equal(t, &User{ID: "1", Name: "username"}, user2[0]) 238 } 239 240 func testGetUser5(t *testing.T, c *Client) { 241 var err error 242 request := remoting.NewRequest("2.0.2") 243 invocation := invocation.NewRPCInvocation("GetUser5", []interface{}{map[interface{}]interface{}{"id": "1", "name": "username"}}, nil) 244 attachment := map[string]string{InterfaceKey: "com.ikurento.user.UserProvider"} 245 setAttachment(invocation, attachment) 246 request.Data = invocation 247 request.Event = false 248 request.TwoWay = true 249 pendingResponse := remoting.NewPendingResponse(request.ID) 250 user3 := map[interface{}]interface{}{} 251 pendingResponse.Reply = &user3 252 remoting.AddPendingResponse(pendingResponse) 253 err = c.Request(request, 3*time.Second, pendingResponse) 254 assert.NoError(t, err) 255 assert.NotNil(t, user3) 256 assert.Equal(t, &User{ID: "1", Name: "username"}, user3["key"]) 257 } 258 259 func testGetUser6(t *testing.T, c *Client) { 260 var ( 261 user *User 262 err error 263 ) 264 user = &User{} 265 request := remoting.NewRequest("2.0.2") 266 invocation := invocation.NewRPCInvocation("GetUser6", []interface{}{0}, nil) 267 attachment := map[string]string{InterfaceKey: "com.ikurento.user.UserProvider"} 268 setAttachment(invocation, attachment) 269 request.Data = invocation 270 request.Event = false 271 request.TwoWay = true 272 pendingResponse := remoting.NewPendingResponse(request.ID) 273 pendingResponse.Reply = user 274 remoting.AddPendingResponse(pendingResponse) 275 err = c.Request(request, 3*time.Second, pendingResponse) 276 assert.NoError(t, err) 277 assert.Equal(t, User{ID: "", Name: ""}, *user) 278 } 279 280 func testGetUser61(t *testing.T, c *Client) { 281 var ( 282 user *User 283 err error 284 ) 285 user = &User{} 286 request := remoting.NewRequest("2.0.2") 287 invocation := invocation.NewRPCInvocation("GetUser6", []interface{}{1}, nil) 288 attachment := map[string]string{InterfaceKey: "com.ikurento.user.UserProvider"} 289 setAttachment(invocation, attachment) 290 request.Data = invocation 291 request.Event = false 292 request.TwoWay = true 293 pendingResponse := remoting.NewPendingResponse(request.ID) 294 pendingResponse.Reply = user 295 remoting.AddPendingResponse(pendingResponse) 296 err = c.Request(request, 3*time.Second, pendingResponse) 297 assert.NoError(t, err) 298 assert.Equal(t, User{ID: "1", Name: ""}, *user) 299 } 300 301 func testClient_AsyncCall(t *testing.T, client *Client) { 302 user := &User{} 303 wg := sync.WaitGroup{} 304 request := remoting.NewRequest("2.0.2") 305 invocation := createInvocation("GetUser0", nil, nil, []interface{}{"4", nil, "username"}, 306 []reflect.Value{reflect.ValueOf("4"), reflect.ValueOf(nil), reflect.ValueOf("username")}) 307 attachment := map[string]string{InterfaceKey: "com.ikurento.user.UserProvider"} 308 setAttachment(invocation, attachment) 309 request.Data = invocation 310 request.Event = false 311 request.TwoWay = true 312 rsp := remoting.NewPendingResponse(request.ID) 313 rsp.SetResponse(remoting.NewResponse(request.ID, "2.0.2")) 314 remoting.AddPendingResponse(rsp) 315 rsp.Reply = user 316 rsp.Callback = func(response common.CallbackResponse) { 317 r := response.(remoting.AsyncCallbackResponse) 318 rst := *r.Reply.(*remoting.Response).Result.(*protocol.RPCResult) 319 assert.Equal(t, User{ID: "4", Name: "username"}, *(rst.Rest.(*User))) 320 wg.Done() 321 } 322 wg.Add(1) 323 err := client.Request(request, 3*time.Second, rsp) 324 assert.NoError(t, err) 325 assert.Equal(t, User{}, *user) 326 wg.Done() 327 } 328 329 func InitTest(t *testing.T) (*Server, *common.URL) { 330 hessian.RegisterPOJO(&User{}) 331 remoting.RegistryCodec("dubbo", &DubboTestCodec{}) 332 333 methods, err := common.ServiceMap.Register("com.ikurento.user.UserProvider", "dubbo", "", "", &UserProvider{}) 334 assert.NoError(t, err) 335 assert.Equal(t, "GetBigPkg,GetUser,GetUser0,GetUser1,GetUser2,GetUser3,GetUser4,GetUser5,GetUser6", methods) 336 337 // config 338 SetClientConf(ClientConfig{ 339 ConnectionNum: 2, 340 HeartbeatPeriod: "5s", 341 SessionTimeout: "20s", 342 GettySessionParam: GettySessionParam{ 343 CompressEncoding: false, 344 TcpNoDelay: true, 345 TcpKeepAlive: true, 346 KeepAlivePeriod: "120s", 347 TcpRBufSize: 262144, 348 TcpWBufSize: 65536, 349 TcpReadTimeout: "4s", 350 TcpWriteTimeout: "5s", 351 WaitTimeout: "1s", 352 MaxMsgLen: 10240000000, 353 SessionName: "client", 354 }, 355 }) 356 assert.NoError(t, clientConf.CheckValidity()) 357 SetServerConfig(ServerConfig{ 358 SessionNumber: 700, 359 SessionTimeout: "20s", 360 GettySessionParam: GettySessionParam{ 361 CompressEncoding: false, 362 TcpNoDelay: true, 363 TcpKeepAlive: true, 364 KeepAlivePeriod: "120s", 365 TcpRBufSize: 262144, 366 TcpWBufSize: 65536, 367 TcpReadTimeout: "1s", 368 TcpWriteTimeout: "5s", 369 WaitTimeout: "1s", 370 MaxMsgLen: 10240000000, 371 SessionName: "server", 372 }, 373 }) 374 assert.NoError(t, srvConf.CheckValidity()) 375 376 url, err := common.NewURL("dubbo://127.0.0.1:20060/com.ikurento.user.UserProvider?anyhost=true&" + 377 "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 378 "environment=dev&interface=com.ikurento.user.UserProvider&ip=127.0.0.1&methods=GetUser%2C&" + 379 "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 380 "side=provider&timeout=3000×tamp=1556509797245&bean.name=UserProvider") 381 assert.NoError(t, err) 382 // init server 383 userProvider := &UserProvider{} 384 _, err = common.ServiceMap.Register("", url.Protocol, "", "0.0.1", userProvider) 385 assert.NoError(t, err) 386 invoker := &proxy_factory.ProxyInvoker{ 387 BaseInvoker: *protocol.NewBaseInvoker(url), 388 } 389 handler := func(invocation *invocation.RPCInvocation) protocol.RPCResult { 390 // result := protocol.RPCResult{} 391 r := invoker.Invoke(context.Background(), invocation) 392 result := protocol.RPCResult{ 393 Err: r.Error(), 394 Rest: r.Result(), 395 Attrs: r.Attachments(), 396 } 397 return result 398 } 399 server := NewServer(url, handler) 400 server.Start() 401 402 time.Sleep(time.Second * 2) 403 404 return server, url 405 } 406 407 ////////////////////////////////// 408 // provider 409 ////////////////////////////////// 410 411 type ( 412 User struct { 413 ID string `json:"id"` 414 Name string `json:"name"` 415 } 416 417 UserProvider struct { // user map[string]User 418 } 419 ) 420 421 // size:4801228 422 func (u *UserProvider) GetBigPkg(ctx context.Context, req []interface{}, rsp *User) error { 423 argBuf := new(bytes.Buffer) 424 for i := 0; i < 400; i++ { 425 argBuf.WriteString("击鼓其镗,踊跃用兵。土国城漕,我独南行。从孙子仲,平陈与宋。不我以归,忧心有忡。爰居爰处?爰丧其马?于以求之?于林之下。死生契阔,与子成说。执子之手,与子偕老。于嗟阔兮,不我活兮。于嗟洵兮,不我信兮。") 426 argBuf.WriteString("击鼓其镗,踊跃用兵。土国城漕,我独南行。从孙子仲,平陈与宋。不我以归,忧心有忡。爰居爰处?爰丧其马?于以求之?于林之下。死生契阔,与子成说。执子之手,与子偕老。于嗟阔兮,不我活兮。于嗟洵兮,不我信兮。") 427 } 428 rsp.ID = argBuf.String() 429 rsp.Name = argBuf.String() 430 return nil 431 } 432 433 func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User) error { 434 rsp.ID = req[0].(string) 435 rsp.Name = req[1].(string) 436 return nil 437 } 438 439 func (u *UserProvider) GetUser0(id string, k *User, name string) (User, error) { 440 // fix testClient_AsyncCall assertion bug(#1233) 441 time.Sleep(1 * time.Second) 442 return User{ID: id, Name: name}, nil 443 } 444 445 func (u *UserProvider) GetUser1() error { 446 return nil 447 } 448 449 func (u *UserProvider) GetUser2() error { 450 return perrors.New("error") 451 } 452 453 func (u *UserProvider) GetUser3(rsp *[]interface{}) error { 454 *rsp = append(*rsp, User{ID: "1", Name: "username"}) 455 return nil 456 } 457 458 func (u *UserProvider) GetUser4(ctx context.Context, req []interface{}) ([]interface{}, error) { 459 return []interface{}{User{ID: req[0].([]interface{})[0].(string), Name: req[0].([]interface{})[1].(string)}}, nil 460 } 461 462 func (u *UserProvider) GetUser5(ctx context.Context, req []interface{}) (map[interface{}]interface{}, error) { 463 return map[interface{}]interface{}{"key": User{ID: req[0].(map[interface{}]interface{})["id"].(string), Name: req[0].(map[interface{}]interface{})["name"].(string)}}, nil 464 } 465 466 func (u *UserProvider) GetUser6(id int64) (*User, error) { 467 if id == 0 { 468 return nil, nil 469 } 470 return &User{ID: "1"}, nil 471 } 472 473 func (u *UserProvider) Reference() string { 474 return "UserProvider" 475 } 476 477 func (u User) JavaClassName() string { 478 return "com.ikurento.user.User" 479 } 480 481 func TestInitClient(t *testing.T) { 482 originRootConf := config.GetRootConfig() 483 rootConf := config.RootConfig{ 484 Protocols: map[string]*config.ProtocolConfig{ 485 "dubbo": { 486 Name: "dubbo", 487 Ip: "127.0.0.1", 488 Port: "20003", 489 }, 490 }, 491 } 492 config.SetRootConfig(rootConf) 493 initServer("dubbo") 494 config.SetRootConfig(*originRootConf) 495 assert.NotNil(t, srvConf) 496 }