dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/jsonrpc/jsonrpc_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 jsonrpc 19 20 import ( 21 "fmt" 22 "strings" 23 "testing" 24 ) 25 26 import ( 27 "github.com/stretchr/testify/assert" 28 ) 29 30 import ( 31 "dubbo.apache.org/dubbo-go/v3/common" 32 "dubbo.apache.org/dubbo-go/v3/config" 33 "dubbo.apache.org/dubbo-go/v3/protocol" 34 ) 35 36 func TestJsonrpcProtocolExport(t *testing.T) { 37 // Export 38 proto := GetProtocol() 39 url, err := common.NewURL("jsonrpc://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" + 40 "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 41 "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" + 42 "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 43 "side=provider&timeout=3000×tamp=1556509797245") 44 assert.NoError(t, err) 45 exporter := proto.Export(protocol.NewBaseInvoker(url)) 46 47 // make sure url 48 eq := exporter.GetInvoker().GetURL().URLEqual(url) 49 assert.True(t, eq) 50 51 // make sure exporterMap after 'UnExport' 52 fmt.Println(url.Path) 53 _, ok := proto.(*JsonrpcProtocol).ExporterMap().Load(strings.TrimPrefix(url.Path, "/")) 54 assert.True(t, ok) 55 exporter.UnExport() 56 _, ok = proto.(*JsonrpcProtocol).ExporterMap().Load(strings.TrimPrefix(url.Path, "/")) 57 assert.False(t, ok) 58 59 // make sure serverMap after 'Destroy' 60 _, ok = proto.(*JsonrpcProtocol).serverMap[url.Location] 61 assert.True(t, ok) 62 proto.Destroy() 63 _, ok = proto.(*JsonrpcProtocol).serverMap[url.Location] 64 assert.False(t, ok) 65 } 66 67 func TestJsonrpcProtocolRefer(t *testing.T) { 68 // Refer 69 proto := GetProtocol() 70 url, err := common.NewURL("jsonrpc://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" + 71 "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" + 72 "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" + 73 "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" + 74 "side=provider&timeout=3000×tamp=1556509797245") 75 assert.NoError(t, err) 76 con := config.ConsumerConfig{ 77 RequestTimeout: "5s", 78 } 79 config.SetConsumerConfig(con) 80 invoker := proto.Refer(url) 81 82 // make sure url 83 eq := invoker.GetURL().URLEqual(url) 84 assert.True(t, eq) 85 86 // make sure invokers after 'Destroy' 87 invokersLen := len(proto.(*JsonrpcProtocol).Invokers()) 88 assert.Equal(t, 1, invokersLen) 89 proto.Destroy() 90 invokersLen = len(proto.(*JsonrpcProtocol).Invokers()) 91 assert.Equal(t, 0, invokersLen) 92 }