dubbo.apache.org/dubbo-go/v3@v3.1.1/config/service_config_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 config
    19  
    20  import (
    21  	"context"
    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/common/constant"
    33  	_ "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory"
    34  )
    35  
    36  type HelloService struct {
    37  }
    38  
    39  func (hs *HelloService) Say(ctx context.Context, name string) (string, error) {
    40  	return name, nil
    41  }
    42  func (hs *HelloService) Reference() string {
    43  	return "HelloService"
    44  }
    45  
    46  func (hs *HelloService) JavaClassName() string {
    47  	return "org.apache.dubbo.HelloService"
    48  }
    49  
    50  func TestNewServiceConfigBuilder(t *testing.T) {
    51  	SetProviderService(&HelloService{})
    52  	var serviceConfig = newEmptyServiceConfig()
    53  	t.Run("NewServiceConfigBuilder", func(t *testing.T) {
    54  		registryConfig := NewRegistryConfigWithProtocolDefaultPort("nacos")
    55  		protocolConfig := NewProtocolConfigBuilder().
    56  			SetName("dubbo").
    57  			SetPort("20000").
    58  			Build()
    59  		rc := newEmptyRootConfig()
    60  
    61  		serviceConfig = NewServiceConfigBuilder().
    62  			SetRegistryIDs("nacos").
    63  			SetProtocolIDs("dubbo").
    64  			SetInterface("org.apache.dubbo.HelloService").
    65  			SetMetadataType("local").
    66  			SetLoadBalancce("random").
    67  			SetWarmUpTie("warmup").
    68  			SetCluster("cluster").
    69  			AddRCRegistry("nacos", registryConfig).
    70  			AddRCProtocol("dubbo", protocolConfig).
    71  			SetGroup("dubbo").
    72  			SetVersion("1.0.0").
    73  			SetProxyFactoryKey("default").
    74  			SetSerialization("serialization").
    75  			SetServiceID("HelloService").
    76  			Build()
    77  
    78  		serviceConfig.InitExported()
    79  
    80  		serviceConfig.Methods = []*MethodConfig{
    81  			{
    82  				Name:    "Say",
    83  				Retries: "3",
    84  			},
    85  		}
    86  
    87  		err := serviceConfig.Init(rc)
    88  		assert.NoError(t, err)
    89  		err = serviceConfig.check()
    90  		assert.NoError(t, err)
    91  
    92  		assert.Equal(t, serviceConfig.Prefix(), strings.Join([]string{constant.ServiceConfigPrefix, serviceConfig.id}, "."))
    93  		assert.Equal(t, serviceConfig.IsExport(), false)
    94  	})
    95  
    96  	t.Run("loadRegistries&loadProtocol&getRandomPort", func(t *testing.T) {
    97  		registries := loadRegistries(serviceConfig.RegistryIDs, serviceConfig.RCRegistriesMap, common.PROVIDER)
    98  		assert.Equal(t, len(registries), 1)
    99  		assert.Equal(t, "service-discovery-registry", registries[0].Protocol)
   100  		assert.Equal(t, registries[0].Port, "8848")
   101  		assert.Equal(t, registries[0].GetParam("registry.role", "1"), "3")
   102  		assert.Equal(t, registries[0].GetParam("registry", "zk"), "nacos")
   103  
   104  		protocols := loadProtocol(serviceConfig.ProtocolIDs, serviceConfig.RCProtocolsMap)
   105  		assert.Equal(t, len(protocols), 1)
   106  		assert.Equal(t, protocols[0].Name, "dubbo")
   107  		assert.Equal(t, protocols[0].Port, "20000")
   108  
   109  		ports := getRandomPort(protocols)
   110  		nextPort := ports.Front()
   111  		assert.Nil(t, nextPort)
   112  	})
   113  	t.Run("getUrlMap", func(t *testing.T) {
   114  		values := serviceConfig.getUrlMap()
   115  		assert.Equal(t, values.Get("methods.Say.weight"), "0")
   116  		assert.Equal(t, values.Get("methods.Say.tps.limit.rate"), "")
   117  		assert.Equal(t, values.Get(constant.ServiceFilterKey), "echo,token,accesslog,tps,generic_service,execute,pshutdown")
   118  	})
   119  
   120  	t.Run("Implement", func(t *testing.T) {
   121  		serviceConfig.Implement(&HelloService{})
   122  		//urls := serviceConfig.GetExportedUrls()
   123  		//err := serviceConfig.Export()
   124  		assert.NotNil(t, serviceConfig.rpcService)
   125  	})
   126  }