dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/dubbo/dubbo_invoker_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 dubbo 19 20 // 21 //import ( 22 // "bytes" 23 // "context" 24 // "sync" 25 // "testing" 26 // "time" 27 //) 28 // 29 //import ( 30 // hessian "github.com/apache/dubbo-go-hessian2" 31 // 32 // "github.com/opentracing/opentracing-go" 33 // 34 // perrors "github.com/pkg/errors" 35 // 36 // "github.com/stretchr/testify/assert" 37 //) 38 // 39 //import ( 40 // "dubbo.apache.org/dubbo-go/v3/common" 41 // "dubbo.apache.org/dubbo-go/v3/common/constant" 42 // "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory" 43 // "dubbo.apache.org/dubbo-go/v3/protocol" 44 // "dubbo.apache.org/dubbo-go/v3/protocol/invocation" 45 // "dubbo.apache.org/dubbo-go/v3/remoting" 46 // "dubbo.apache.org/dubbo-go/v3/remoting/getty" 47 //) 48 // 49 //func TestDubboInvokerInvoke(t *testing.T) { 50 // proto, url := InitTest(t) 51 // 52 // c := getExchangeClient(url) 53 // 54 // invoker := NewDubboInvoker(url, c) 55 // user := &User{} 56 // 57 // inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUser"), invocation.WithArguments([]interface{}{"1", "username"}), 58 // invocation.WithReply(user), invocation.WithAttachments(map[string]interface{}{"test_key": "test_value"})) 59 // 60 // // Call 61 // res := invoker.Invoke(context.Background(), inv) 62 // assert.NoError(t, res.Error()) 63 // assert.Equal(t, User{ID: "1", Name: "username"}, *res.Result().(*User)) 64 // 65 // // CallOneway 66 // inv.SetAttachment(constant.ASYNC_KEY, "true") 67 // res = invoker.Invoke(context.Background(), inv) 68 // assert.NoError(t, res.Error()) 69 // 70 // // AsyncCall 71 // lock := sync.Mutex{} 72 // lock.Lock() 73 // inv.SetCallBack(func(response common.CallbackResponse) { 74 // r := response.(remoting.AsyncCallbackResponse) 75 // rst := *r.Reply.(*remoting.Response).Result.(*protocol.RPCResult) 76 // assert.Equal(t, User{ID: "1", Name: "username"}, *(rst.Rest.(*User))) 77 // // assert.Equal(t, User{ID: "1", Name: "username"}, *r.Reply.(*Response).reply.(*User)) 78 // lock.Unlock() 79 // }) 80 // res = invoker.Invoke(context.Background(), inv) 81 // assert.NoError(t, res.Error()) 82 // 83 // // Err_No_Reply 84 // inv.SetAttachment(constant.ASYNC_KEY, "false") 85 // inv.SetReply(nil) 86 // res = invoker.Invoke(context.Background(), inv) 87 // assert.EqualError(t, res.Error(), "request need @response") 88 // 89 // // testing appendCtx 90 // span, ctx := opentracing.StartSpanFromContext(context.Background(), "TestOperation") 91 // invoker.Invoke(ctx, inv) 92 // span.Finish() 93 // 94 // // destroy 95 // lock.Lock() 96 // defer lock.Unlock() 97 // proto.Destroy() 98 //} 99 // 100 //func InitTest(t *testing.T) (protocol.Protocol, *common.URL) { 101 // hessian.RegisterPOJO(&User{}) 102 // 103 // methods, err := common.ServiceMap.Register("com.ikurento.user.UserProvider", "dubbo", "", "", &UserProvider{}) 104 // assert.NoError(t, err) 105 // assert.Equal(t, "GetBigPkg,GetUser,GetUser0,GetUser1,GetUser2,GetUser3,GetUser4,GetUser5,GetUser6", methods) 106 // 107 // // config 108 // getty.SetClientConf(getty.ClientConfig{ 109 // ConnectionNum: 2, 110 // HeartbeatPeriod: "5s", 111 // SessionTimeout: "20s", 112 // GettySessionParam: getty.GettySessionParam{ 113 // CompressEncoding: false, 114 // TcpNoDelay: true, 115 // TcpKeepAlive: true, 116 // KeepAlivePeriod: "120s", 117 // TcpRBufSize: 262144, 118 // TcpWBufSize: 65536, 119 // TcpReadTimeout: "4s", 120 // TcpWriteTimeout: "5s", 121 // WaitTimeout: "1s", 122 // MaxMsgLen: 10240000000, 123 // SessionName: "client", 124 // }, 125 // }) 126 // getty.SetServerConfig(getty.ServerConfig{ 127 // SessionNumber: 700, 128 // SessionTimeout: "20s", 129 // GettySessionParam: getty.GettySessionParam{ 130 // CompressEncoding: false, 131 // TcpNoDelay: true, 132 // TcpKeepAlive: true, 133 // KeepAlivePeriod: "120s", 134 // TcpRBufSize: 262144, 135 // TcpWBufSize: 65536, 136 // TcpReadTimeout: "1s", 137 // TcpWriteTimeout: "5s", 138 // WaitTimeout: "1s", 139 // MaxMsgLen: 10240000000, 140 // SessionName: "server", 141 // }, 142 // }) 143 // 144 // // Export 145 // proto := GetProtocol() 146 // url, err := common.NewURL("dubbo://127.0.0.1:20702/UserProvider?anyhost=true&" + 147 // "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 148 // "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" + 149 // "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 150 // "side=provider&timeout=3000×tamp=1556509797245&bean.name=UserProvider") 151 // assert.NoError(t, err) 152 // proto.Export(&proxy_factory.ProxyInvoker{ 153 // BaseInvoker: *protocol.NewBaseInvoker(url), 154 // }) 155 // 156 // time.Sleep(time.Second * 2) 157 // 158 // return proto, url 159 //} 160 // 161 //////////////////////////////////// 162 //// provider 163 //////////////////////////////////// 164 // 165 //type ( 166 // User struct { 167 // ID string `json:"id"` 168 // Name string `json:"name"` 169 // } 170 // 171 // UserProvider struct { // user map[string]User 172 // } 173 //) 174 // 175 //// size:4801228 176 //func (u *UserProvider) GetBigPkg(ctx context.Context, req []interface{}, rsp *User) error { 177 // argBuf := new(bytes.Buffer) 178 // for i := 0; i < 800; i++ { 179 // // use chinese for test 180 // argBuf.WriteString("击鼓其镗,踊跃用兵。土国城漕,我独南行。从孙子仲,平陈与宋。不我以归,忧心有忡。爰居爰处?爰丧其马?于以求之?于林之下。死生契阔,与子成说。执子之手,与子偕老。于嗟阔兮,不我活兮。于嗟洵兮,不我信兮。") 181 // } 182 // rsp.ID = argBuf.String() 183 // rsp.Name = argBuf.String() 184 // return nil 185 //} 186 // 187 //func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User) error { 188 // rsp.ID = req[0].(string) 189 // rsp.Name = req[1].(string) 190 // return nil 191 //} 192 // 193 //func (u *UserProvider) GetUser0(id string, k *User, name string) (User, error) { 194 // return User{ID: id, Name: name}, nil 195 //} 196 // 197 //func (u *UserProvider) GetUser1() error { 198 // return nil 199 //} 200 // 201 //func (u *UserProvider) GetUser2() error { 202 // return perrors.New("error") 203 //} 204 // 205 //func (u *UserProvider) GetUser3(rsp *[]interface{}) error { 206 // *rsp = append(*rsp, User{ID: "1", Name: "username"}) 207 // return nil 208 //} 209 // 210 //func (u *UserProvider) GetUser4(ctx context.Context, req []interface{}) ([]interface{}, error) { 211 // return []interface{}{User{ID: req[0].([]interface{})[0].(string), Name: req[0].([]interface{})[1].(string)}}, nil 212 //} 213 // 214 //func (u *UserProvider) GetUser5(ctx context.Context, req []interface{}) (map[interface{}]interface{}, error) { 215 // return map[interface{}]interface{}{"key": User{ID: req[0].(map[interface{}]interface{})["id"].(string), Name: req[0].(map[interface{}]interface{})["name"].(string)}}, nil 216 //} 217 // 218 //func (u *UserProvider) GetUser6(id int64) (*User, error) { 219 // if id == 0 { 220 // return nil, nil 221 // } 222 // return &User{ID: "1"}, nil 223 //} 224 // 225 //func (u *UserProvider) Reference() string { 226 // return "UserProvider" 227 //} 228 // 229 //func (u User) JavaClassName() string { 230 // return "com.ikurento.user.User" 231 //}