dubbo.apache.org/dubbo-go/v3@v3.1.1/config/root_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  	"testing"
    22  )
    23  
    24  import (
    25  	"github.com/dubbogo/gost/encoding/yaml"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  import (
    31  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    32  	"dubbo.apache.org/dubbo-go/v3/config_center"
    33  )
    34  
    35  func TestGoConfigProcess(t *testing.T) {
    36  	rc := &RootConfigBuilder{rootConfig: newEmptyRootConfig()}
    37  	r := &RegistryConfig{Protocol: "zookeeper", Timeout: "10s", Address: "127.0.0.1:2181"}
    38  	rc.AddRegistry("demoZK", r)
    39  
    40  	// test koan.UnmarshalWithConf error
    41  	b := "dubbo:\n  registries:\n    demoZK:\n      protocol: zookeeper\n      timeout: 11s\n      address: 127.0.0.1:2181\n      simplified: abc123"
    42  	c2 := &config_center.ConfigChangeEvent{Key: "test", Value: b}
    43  	rc.rootConfig.Process(c2)
    44  	assert.Equal(t, rc.rootConfig.Registries["demoZK"].Timeout, "10s")
    45  
    46  	// test update registry time out
    47  	bs, _ := yaml.LoadYMLConfig("./testdata/root_config_test.yml")
    48  	c := &config_center.ConfigChangeEvent{Key: "test", Value: string(bs)}
    49  	rc.rootConfig.Process(c)
    50  	assert.Equal(t, rc.rootConfig.Registries["demoZK"].Timeout, "11s")
    51  	assert.Equal(t, rc.rootConfig.Consumer.RequestTimeout, "6s")
    52  
    53  }
    54  
    55  func TestNewRootConfigBuilder(t *testing.T) {
    56  	registryConfig := NewRegistryConfigWithProtocolDefaultPort("nacos")
    57  	protocolConfig := NewProtocolConfigBuilder().
    58  		SetName("dubbo").
    59  		SetPort("20000").
    60  		Build()
    61  	rootConfig = NewRootConfigBuilder().
    62  		SetConfigCenter(NewConfigCenterConfigBuilder().Build()).
    63  		SetMetadataReport(NewMetadataReportConfigBuilder().Build()).
    64  		AddProtocol("dubbo", protocolConfig).
    65  		AddRegistry("nacos", registryConfig).
    66  		SetProtocols(map[string]*ProtocolConfig{"dubbo": protocolConfig}).
    67  		SetRegistries(map[string]*RegistryConfig{"nacos": registryConfig}).
    68  		SetProvider(NewProviderConfigBuilder().Build()).
    69  		SetConsumer(NewConsumerConfigBuilder().Build()).
    70  		SetMetric(NewMetricConfigBuilder().Build()).
    71  		SetLogger(NewLoggerConfigBuilder().Build()).
    72  		SetShutdown(NewShutDownConfigBuilder().Build()).
    73  		SetShutDown(NewShutDownConfigBuilder().Build()).
    74  		SetRouter([]*RouterConfig{}).
    75  		SetEventDispatcherType("direct").
    76  		SetCacheFile("abc=123").
    77  		Build()
    78  
    79  	assert.Equal(t, rootConfig.Prefix(), constant.Dubbo)
    80  	ids := rootConfig.getRegistryIds()
    81  	assert.Equal(t, ids[0], "nacos")
    82  
    83  	down := GetShutDown()
    84  	assert.NotNil(t, down)
    85  
    86  	application := GetApplicationConfig()
    87  	assert.NotNil(t, application)
    88  
    89  	registerPOJO()
    90  	config := GetRootConfig()
    91  	assert.Equal(t, rootConfig, config)
    92  }