dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/dubbo3/dubbo3_protocol_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 dubbo3 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/protocol" 36 ) 37 38 const ( 39 mockDubbo3CommonUrl = "tri://127.0.0.1:20002/DubboGreeterImpl?accesslog=&anyhost=true&app.version=0.0.1&application=BDTService&async=false&bean.name=greeterProvider" + 40 "&category=providers&cluster=failover&dubbo=dubbo-provider-golang-2.6.0&environment=dev&execute.limit=&execute.limit.rejected.handler=&generic=false&group=&interface=org.apache.dubbo.DubboGreeterImpl" + 41 "&ip=192.168.1.106&loadbalance=random&methods.SayHello.loadbalance=random&methods.SayHello.retries=1&methods.SayHello.tps.limit.interval=&methods.SayHello.tps.limit.rate=&methods.SayHello.tps.limit.strategy=" + 42 "&methods.SayHello.weight=0&module=dubbogo+say-hello+client&name=BDTService&organization=ikurento.com&owner=ZX&pid=49427&reference.filter=cshutdown®istry.role=3&remote.timestamp=1576923717&retries=" + 43 "&service.filter=echo%2Ctoken%2Caccesslog%2Ctps%2Cexecute%2Cpshutdown&side=provider×tamp=1576923740&tps.limit.interval=&tps.limit.rate=&tps.limit.rejected.handler=&tps.limit.strategy=&tps.limiter=&version=&warmup=100!" 44 ) 45 46 func TestDubboProtocolExport(t *testing.T) { 47 // Export 48 addService() 49 50 proto := GetProtocol() 51 url, err := common.NewURL(mockDubbo3CommonUrl) 52 assert.NoError(t, err) 53 exporter := proto.Export(protocol.NewBaseInvoker(url)) 54 time.Sleep(time.Second) 55 56 // make sure url 57 eq := exporter.GetInvoker().GetURL().URLEqual(url) 58 assert.True(t, eq) 59 60 // make sure exporterMap after 'UnExport' 61 _, ok := proto.(*DubboProtocol).ExporterMap().Load(url.ServiceKey()) 62 assert.True(t, ok) 63 exporter.UnExport() 64 _, ok = proto.(*DubboProtocol).ExporterMap().Load(url.ServiceKey()) 65 assert.False(t, ok) 66 67 // make sure serverMap after 'Destroy' 68 _, ok = proto.(*DubboProtocol).serverMap[url.Location] 69 assert.True(t, ok) 70 proto.Destroy() 71 _, ok = proto.(*DubboProtocol).serverMap[url.Location] 72 assert.False(t, ok) 73 } 74 75 func TestDubboProtocolRefer(t *testing.T) { 76 proto := GetProtocol() 77 url, err := common.NewURL(mockDubbo3CommonUrl) 78 assert.NoError(t, err) 79 invoker := proto.Refer(url) 80 81 // make sure url 82 eq := invoker.GetURL().URLEqual(url) 83 assert.True(t, eq) 84 85 // make sure invokers after 'Destroy' 86 invokersLen := len(proto.(*DubboProtocol).Invokers()) 87 assert.Equal(t, 1, invokersLen) 88 proto.Destroy() 89 invokersLen = len(proto.(*DubboProtocol).Invokers()) 90 assert.Equal(t, 0, invokersLen) 91 } 92 93 type MockUser struct { 94 Name string 95 } 96 97 func (m *MockUser) JavaClassName() string { 98 return "mockuser" 99 } 100 101 type MockService struct { 102 } 103 104 func (m *MockService) GetUser(ctx context.Context, user, user2 *MockUser) (*MockUser, error) { 105 return user, nil 106 } 107 108 func TestDubbo3UnaryService_GetReqParamsInterfaces(t *testing.T) { 109 hessian.RegisterPOJO(&MockUser{}) 110 srv := UnaryService{} 111 valueOf := reflect.ValueOf(&MockService{}) 112 typeOf := valueOf.Type() 113 numField := valueOf.NumMethod() 114 for i := 0; i < numField; i++ { 115 ft := typeOf.Method(i) 116 // num in/out is checked in common/rpc_service.go 117 typs := make([]reflect.Type, 0) 118 for j := 2; j < ft.Type.NumIn(); j++ { 119 typs = append(typs, ft.Type.In(j)) 120 } 121 srv.setReqParamsTypes("GetUser", typs) 122 } 123 paramsInterfaces, ok := srv.GetReqParamsInterfaces("GetUser") 124 assert.True(t, ok) 125 enc := hessian.NewEncoder() 126 err := enc.Encode(&MockUser{ 127 Name: "laurence", 128 }) 129 assert.Nil(t, err) 130 data := enc.Buffer() 131 decoder := hessian.NewDecoder(data) 132 val, err := decoder.Decode() 133 assert.Nil(t, err) 134 assert.Equal(t, 2, len(paramsInterfaces)) 135 subTest(t, val, paramsInterfaces) 136 args := make([]interface{}, 0, 1) 137 for _, v := range paramsInterfaces { 138 tempParamObj := reflect.ValueOf(v).Elem().Interface() 139 args = append(args, tempParamObj) 140 } 141 assert.Equal(t, "laurence", args[0].(*MockUser).Name) 142 assert.Equal(t, "laurence", args[1].(*MockUser).Name) 143 } 144 145 func subTest(t *testing.T, val, paramsInterfaces interface{}) { 146 list := paramsInterfaces.([]interface{}) 147 for k := range list { 148 err := hessian.ReflectResponse(val, list[k]) 149 assert.Nil(t, err) 150 } 151 }