dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/dubbo/dubbo_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 dubbo
    19  
    20  import (
    21  	"testing"
    22  )
    23  
    24  import (
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  import (
    29  	"dubbo.apache.org/dubbo-go/v3/common"
    30  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    31  	"dubbo.apache.org/dubbo-go/v3/protocol"
    32  	"dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory"
    33  	"dubbo.apache.org/dubbo-go/v3/remoting/getty"
    34  )
    35  
    36  const (
    37  	mockCommonUrl = "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" +
    38  		"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
    39  		"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
    40  		"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
    41  		"side=provider&timeout=3000&timestamp=1556509797245"
    42  )
    43  
    44  func initDubboInvokerTest() {
    45  	getty.SetServerConfig(getty.ServerConfig{
    46  		SessionNumber:  700,
    47  		SessionTimeout: "20s",
    48  		GettySessionParam: getty.GettySessionParam{
    49  			CompressEncoding: false,
    50  			TcpNoDelay:       true,
    51  			TcpKeepAlive:     true,
    52  			KeepAlivePeriod:  "120s",
    53  			TcpRBufSize:      262144,
    54  			TcpWBufSize:      65536,
    55  			TcpReadTimeout:   "1s",
    56  			TcpWriteTimeout:  "5s",
    57  			WaitTimeout:      "1s",
    58  			MaxMsgLen:        10240000000,
    59  			SessionName:      "server",
    60  		},
    61  	})
    62  	getty.SetClientConf(getty.ClientConfig{
    63  		ConnectionNum:   1,
    64  		HeartbeatPeriod: "3s",
    65  		SessionTimeout:  "20s",
    66  		GettySessionParam: getty.GettySessionParam{
    67  			CompressEncoding: false,
    68  			TcpNoDelay:       true,
    69  			TcpKeepAlive:     true,
    70  			KeepAlivePeriod:  "120s",
    71  			TcpRBufSize:      262144,
    72  			TcpWBufSize:      65536,
    73  			TcpReadTimeout:   "4s",
    74  			TcpWriteTimeout:  "5s",
    75  			WaitTimeout:      "1s",
    76  			MaxMsgLen:        10240000000,
    77  			SessionName:      "client",
    78  		},
    79  	})
    80  }
    81  
    82  func TestDubboProtocol_Export(t *testing.T) {
    83  	initDubboInvokerTest()
    84  	srvCfg := getty.GetDefaultServerConfig()
    85  	getty.SetServerConfig(*srvCfg)
    86  	// Export
    87  	proto := GetProtocol()
    88  	url, err := common.NewURL(mockCommonUrl)
    89  	assert.NoError(t, err)
    90  	exporter := proto.Export(protocol.NewBaseInvoker(url))
    91  	// make sure url
    92  	eq := exporter.GetInvoker().GetURL().URLEqual(url)
    93  	assert.True(t, eq)
    94  
    95  	// second service: the same path and the different version
    96  	url2, err := common.NewURL(mockCommonUrl, common.WithParamsValue(constant.VersionKey, "v1.1"))
    97  	assert.NoError(t, err)
    98  	exporter2 := proto.Export(protocol.NewBaseInvoker(url2))
    99  	// make sure url
   100  	eq2 := exporter2.GetInvoker().GetURL().URLEqual(url2)
   101  	assert.True(t, eq2)
   102  
   103  	// make sure exporterMap after 'UnExport'
   104  	_, ok := proto.(*DubboProtocol).ExporterMap().Load(url2.ServiceKey())
   105  	assert.True(t, ok)
   106  	exporter2.UnExport()
   107  	_, ok = proto.(*DubboProtocol).ExporterMap().Load(url2.ServiceKey())
   108  	assert.False(t, ok)
   109  
   110  	// make sure serverMap after 'Destroy'
   111  	_, ok = proto.(*DubboProtocol).serverMap[url.Location]
   112  	assert.True(t, ok)
   113  	proto.Destroy()
   114  	_, ok = proto.(*DubboProtocol).serverMap[url.Location]
   115  	assert.False(t, ok)
   116  }
   117  
   118  func TestDubboProtocolReferNoConnect(t *testing.T) {
   119  	// Refer
   120  	initDubboInvokerTest()
   121  	proto := GetProtocol()
   122  	url, err := common.NewURL(mockCommonUrl)
   123  	assert.NoError(t, err)
   124  	invoker := proto.Refer(url)
   125  	assert.Nil(t, invoker)
   126  }
   127  
   128  func TestDubboProtocol_Refer(t *testing.T) {
   129  	initDubboInvokerTest()
   130  	cliCfg := getty.GetDefaultClientConfig()
   131  	getty.SetClientConf(*cliCfg)
   132  	// Refer
   133  	proto := GetProtocol()
   134  
   135  	url, err := common.NewURL(mockCommonUrl)
   136  	proto.Export(&proxy_factory.ProxyInvoker{
   137  		BaseInvoker: *protocol.NewBaseInvoker(url),
   138  	})
   139  	assert.NoError(t, err)
   140  	invoker := proto.Refer(url)
   141  	// make sure url
   142  	eq := invoker.GetURL().URLEqual(url)
   143  	assert.True(t, eq)
   144  
   145  	// make sure invokers after 'Destroy'
   146  	invokersLen := len(proto.(*DubboProtocol).Invokers())
   147  	assert.Equal(t, 1, invokersLen)
   148  	proto.Destroy()
   149  	invokersLen = len(proto.(*DubboProtocol).Invokers())
   150  	assert.Equal(t, 0, invokersLen)
   151  }