dubbo.apache.org/dubbo-go/v3@v3.1.1/remoting/getty/readwriter_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 "context" 22 "reflect" 23 "testing" 24 "time" 25 ) 26 27 import ( 28 hessian "github.com/apache/dubbo-go-hessian2" 29 30 "github.com/stretchr/testify/assert" 31 ) 32 33 import ( 34 "dubbo.apache.org/dubbo-go/v3/common" 35 "dubbo.apache.org/dubbo-go/v3/common/constant" 36 "dubbo.apache.org/dubbo-go/v3/protocol" 37 "dubbo.apache.org/dubbo-go/v3/protocol/dubbo/impl" 38 "dubbo.apache.org/dubbo-go/v3/protocol/invocation" 39 "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory" 40 "dubbo.apache.org/dubbo-go/v3/remoting" 41 ) 42 43 func TestTCPPackageHandle(t *testing.T) { 44 svr, url := getServer(t) 45 client := getClient(url) 46 testDecodeTCPPackage(t, svr, client) 47 svr.Stop() 48 } 49 50 func testDecodeTCPPackage(t *testing.T, svr *Server, client *Client) { 51 request := remoting.NewRequest("2.0.2") 52 ap := &AdminProvider{} 53 rpcInvocation := createInvocation("GetAdmin", nil, nil, []interface{}{[]interface{}{"1", "username"}}, 54 []reflect.Value{reflect.ValueOf([]interface{}{"1", "username"}), reflect.ValueOf(ap)}) 55 attachment := map[string]string{ 56 constant.InterfaceKey: "com.ikurento.user.AdminProvider", 57 constant.PathKey: "AdminProvider", 58 constant.VersionKey: "1.0.0", 59 } 60 setAttachment(rpcInvocation, attachment) 61 request.Data = rpcInvocation 62 request.Event = false 63 request.TwoWay = false 64 65 pkgWriteHandler := NewRpcClientPackageHandler(client) 66 pkgBytes, err := pkgWriteHandler.Write(nil, request) 67 assert.NoError(t, err) 68 pkgReadHandler := NewRpcServerPackageHandler(svr) 69 _, pkgLen, err := pkgReadHandler.Read(nil, pkgBytes) 70 assert.NoError(t, err) 71 assert.Equal(t, pkgLen, len(pkgBytes)) 72 73 // simulate incomplete tcp package 74 incompletePkgLen := len(pkgBytes) - 10 75 assert.True(t, incompletePkgLen >= impl.HEADER_LENGTH, "header buffer too short") 76 incompletePkg := pkgBytes[0 : incompletePkgLen-1] 77 pkg, pkgLen, err := pkgReadHandler.Read(nil, incompletePkg) 78 assert.Equal(t, err.Error(), "body buffer too short") 79 assert.Equal(t, pkg.(*remoting.DecodeResult).Result, nil) 80 assert.Equal(t, pkgLen, 0) 81 } 82 83 func getServer(t *testing.T) (*Server, *common.URL) { 84 hessian.RegisterPOJO(&User{}) 85 remoting.RegistryCodec("dubbo", &DubboTestCodec{}) 86 87 methods, err := common.ServiceMap.Register("com.ikurento.user.AdminProvider", "dubbo", "", "", &AdminProvider{}) 88 assert.NoError(t, err) 89 assert.Equal(t, "GetAdmin", methods) 90 91 // config 92 SetClientConf(ClientConfig{ 93 ConnectionNum: 2, 94 HeartbeatPeriod: "5s", 95 SessionTimeout: "20s", 96 GettySessionParam: GettySessionParam{ 97 CompressEncoding: false, 98 TcpNoDelay: true, 99 TcpKeepAlive: true, 100 KeepAlivePeriod: "120s", 101 TcpRBufSize: 262144, 102 TcpWBufSize: 65536, 103 TcpReadTimeout: "4s", 104 TcpWriteTimeout: "5s", 105 WaitTimeout: "1s", 106 MaxMsgLen: 10240000000, 107 SessionName: "client", 108 }, 109 }) 110 assert.NoError(t, clientConf.CheckValidity()) 111 SetServerConfig(ServerConfig{ 112 SessionNumber: 700, 113 SessionTimeout: "20s", 114 GettySessionParam: GettySessionParam{ 115 CompressEncoding: false, 116 TcpNoDelay: true, 117 TcpKeepAlive: true, 118 KeepAlivePeriod: "120s", 119 TcpRBufSize: 262144, 120 TcpWBufSize: 65536, 121 TcpReadTimeout: "1s", 122 TcpWriteTimeout: "5s", 123 WaitTimeout: "1s", 124 MaxMsgLen: 10240000000, 125 SessionName: "server", 126 }, 127 }) 128 assert.NoError(t, srvConf.CheckValidity()) 129 130 url, err := common.NewURL("dubbo://127.0.0.1:20061/com.ikurento.user.AdminProvider?anyhost=true&" + 131 "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 132 "environment=dev&interface=com.ikurento.user.AdminProvider&ip=127.0.0.1&methods=GetAdmin%2C&" + 133 "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 134 "side=provider&timeout=3000×tamp=1556509797245&bean.name=AdminProvider") 135 assert.NoError(t, err) 136 // init server 137 adminProvider := &AdminProvider{} 138 _, err = common.ServiceMap.Register("com.ikurento.user.AdminProvider", url.Protocol, "", "0.0.1", adminProvider) 139 assert.NoError(t, err) 140 invoker := &proxy_factory.ProxyInvoker{ 141 BaseInvoker: *protocol.NewBaseInvoker(url), 142 } 143 handler := func(invocation *invocation.RPCInvocation) protocol.RPCResult { 144 // result := protocol.RPCResult{} 145 r := invoker.Invoke(context.Background(), invocation) 146 result := protocol.RPCResult{ 147 Err: r.Error(), 148 Rest: r.Result(), 149 Attrs: r.Attachments(), 150 } 151 return result 152 } 153 server := NewServer(url, handler) 154 server.Start() 155 156 time.Sleep(time.Second * 2) 157 158 return server, url 159 } 160 161 type AdminProvider struct{} 162 163 func (a *AdminProvider) GetAdmin(ctx context.Context, req []interface{}, rsp *User) error { 164 rsp.ID = req[0].(string) 165 rsp.Name = req[1].(string) 166 return nil 167 } 168 169 func (a *AdminProvider) Reference() string { 170 return "AdminProvider" 171 }