Skip to content
Snippets Groups Projects
config_loader_test.go 4.32 KiB
Newer Older
AlexStocks's avatar
AlexStocks committed
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
fangyincheng's avatar
fangyincheng committed

vito.he's avatar
vito.he committed
package config
fangyincheng's avatar
fangyincheng committed

import (
	"path/filepath"
	"testing"
)

import (
	"github.com/stretchr/testify/assert"
)

	"github.com/apache/dubbo-go/cluster/cluster_impl"
	"github.com/apache/dubbo-go/common"
	"github.com/apache/dubbo-go/common/extension"
	"github.com/apache/dubbo-go/common/proxy/proxy_factory"
fangyincheng's avatar
fangyincheng committed
func TestConfigLoader(t *testing.T) {
fangyincheng's avatar
fangyincheng committed
	conPath, err := filepath.Abs("./testdata/consumer_config.yml")
fangyincheng's avatar
fangyincheng committed
	assert.NoError(t, err)
fangyincheng's avatar
fangyincheng committed
	proPath, err := filepath.Abs("./testdata/provider_config.yml")
fangyincheng's avatar
fangyincheng committed
	assert.NoError(t, err)

	assert.Nil(t, consumerConfig)
fangyincheng's avatar
fangyincheng committed
	assert.Equal(t, ConsumerConfig{}, GetConsumerConfig())
fangyincheng's avatar
fangyincheng committed
	assert.Nil(t, providerConfig)
fangyincheng's avatar
fangyincheng committed
	assert.Equal(t, ProviderConfig{}, GetProviderConfig())
fangyincheng's avatar
fangyincheng committed

	err = consumerInit(conPath)
	assert.NoError(t, err)
	err = providerInit(proPath)
	assert.NoError(t, err)

	assert.NotNil(t, consumerConfig)
fangyincheng's avatar
fangyincheng committed
	assert.NotEqual(t, ConsumerConfig{}, GetConsumerConfig())
fangyincheng's avatar
fangyincheng committed
	assert.NotNil(t, providerConfig)
fangyincheng's avatar
fangyincheng committed
	assert.NotEqual(t, ProviderConfig{}, GetProviderConfig())
fangyincheng's avatar
fangyincheng committed
}

func TestLoad(t *testing.T) {
	doInit()
	doinit()

	ms := &MockService{}
	SetConsumerService(ms)
	SetProviderService(ms)

	extension.SetProtocol("registry", GetProtocol)
	extension.SetCluster("registryAware", cluster_impl.NewRegistryAwareCluster)
	extension.SetProxyFactory("default", proxy_factory.NewDefaultProxyFactory)
vito.he's avatar
vito.he committed
	Load()

	assert.Equal(t, ms, GetRPCService(ms.Service()))
	ms2 := &struct {
		MockService
	}{}
	RPCService(ms2)
	assert.NotEqual(t, ms2, GetRPCService(ms2.Service()))

	conServices = map[string]common.RPCService{}
	proServices = map[string]common.RPCService{}
	common.ServiceMap.UnRegister("mock", "MockService")
	consumerConfig = nil
	providerConfig = nil
}
func TestWithNoRegLoad(t *testing.T) {
	doInit()
	doinit()
	providerConfig.Services["MockService"].Registry = ""
	consumerConfig.References["MockService"].Registry = ""
	ms := &MockService{}
	SetConsumerService(ms)
	SetProviderService(ms)

	extension.SetProtocol("registry", GetProtocol)
	extension.SetCluster("registryAware", cluster_impl.NewRegistryAwareCluster)
	extension.SetProxyFactory("default", proxy_factory.NewDefaultProxyFactory)

	Load()

	assert.Equal(t, ms, GetRPCService(ms.Service()))
	ms2 := &struct {
		MockService
	}{}
	RPCService(ms2)
	assert.NotEqual(t, ms2, GetRPCService(ms2.Service()))
	conServices = map[string]common.RPCService{}
	proServices = map[string]common.RPCService{}
	common.ServiceMap.UnRegister("mock", "MockService")
	consumerConfig = nil
	providerConfig = nil
}
func TestConfigLoaderWithConfigCenter(t *testing.T) {
	extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory {
		return &config_center.MockDynamicConfigurationFactory{}
	})

	conPath, err := filepath.Abs("./testdata/consumer_config_with_configcenter.yml")
	assert.NoError(t, err)
	proPath, err := filepath.Abs("./testdata/provider_config.yml")
	assert.NoError(t, err)

	assert.Nil(t, consumerConfig)
	assert.Equal(t, ConsumerConfig{}, GetConsumerConfig())
	assert.Nil(t, providerConfig)
	assert.Equal(t, ProviderConfig{}, GetProviderConfig())

	err = consumerInit(conPath)
	configCenterRefreshConsumer()
	assert.NoError(t, err)
	err = providerInit(proPath)
	configCenterRefreshProvider()
	assert.NoError(t, err)

	assert.NotNil(t, consumerConfig)
	assert.NotEqual(t, ConsumerConfig{}, GetConsumerConfig())
	assert.NotNil(t, providerConfig)
	assert.NotEqual(t, ProviderConfig{}, GetProviderConfig())

	assert.Equal(t, "BDTService", consumerConfig.ApplicationConfig.Name)
	assert.Equal(t, "127.0.0.1:2181", consumerConfig.Registries["hangzhouzk"].Address)

}