Skip to content
Snippets Groups Projects
service_config_test.go 5.43 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

import (
	"testing"
)

邹毅贤's avatar
邹毅贤 committed
	gxnet "github.com/dubbogo/gost/net"
	"github.com/stretchr/testify/assert"
邹毅贤's avatar
邹毅贤 committed
import (
	"github.com/apache/dubbo-go/common/extension"
)

func doInitProvider() {
vito.he's avatar
vito.he committed
	providerConfig = &ProviderConfig{
vito.he's avatar
vito.he committed
			Organization: "dubbo_org",
			Name:         "dubbo",
			Module:       "module",
			Version:      "2.6.0",
			Owner:        "dubbo",
			Environment:  "test"},
		Registries: map[string]*RegistryConfig{
			"shanghai_reg1": {
				Protocol:   "mock",
vito.he's avatar
vito.he committed
				TimeoutStr: "2s",
				Group:      "shanghai_idc",
				Address:    "127.0.0.1:2181",
				Username:   "user1",
				Password:   "pwd1",
			},
vito.he's avatar
vito.he committed
				TimeoutStr: "2s",
				Group:      "shanghai_idc",
				Address:    "127.0.0.2:2181",
				Username:   "user1",
				Password:   "pwd1",
			},
vito.he's avatar
vito.he committed
				TimeoutStr: "2s",
				Group:      "hangzhou_idc",
				Address:    "127.0.0.3:2181",
				Username:   "user1",
				Password:   "pwd1",
			},
vito.he's avatar
vito.he committed
				TimeoutStr: "2s",
				Group:      "hangzhou_idc",
				Address:    "127.0.0.4:2181",
				Username:   "user1",
				Password:   "pwd1",
			},
		},
		Services: map[string]*ServiceConfig{
			"MockService": {
				InterfaceName: "com.MockService",
vito.he's avatar
vito.he committed
				Protocol:      "mock",
				Registry:      "shanghai_reg1,shanghai_reg2,hangzhou_reg1,hangzhou_reg2",
vito.he's avatar
vito.he committed
				Cluster:       "failover",
				Loadbalance:   "random",
vito.he's avatar
vito.he committed
				Group:         "huadong_idc",
				Version:       "1.0.0",
vito.he's avatar
vito.he committed
					{
						Name:        "GetUser",
vito.he's avatar
vito.he committed
						Loadbalance: "random",
						Weight:      200,
					},
					{
						Name:        "GetUser1",
vito.he's avatar
vito.he committed
						Loadbalance: "random",
						Weight:      200,
					},
				},
			},
			"MockServiceNoRightProtocol": {
				InterfaceName: "com.MockService",
				Protocol:      "mock1",
				Registry:      "shanghai_reg1,shanghai_reg2,hangzhou_reg1,hangzhou_reg2",
				Cluster:       "failover",
				Loadbalance:   "random",
				Retries:       "3",
				Group:         "huadong_idc",
				Version:       "1.0.0",
				Methods: []*MethodConfig{
					{
						Name:        "GetUser",
						Retries:     "2",
						Loadbalance: "random",
						Weight:      200,
					},
					{
						Name:        "GetUser1",
						Retries:     "2",
						Loadbalance: "random",
						Weight:      200,
					},
				},
			},
vito.he's avatar
vito.he committed
		},
		Protocols: map[string]*ProtocolConfig{
			"mock": {
				Name: "mock",
				Ip:   "127.0.0.1",
				Port: "20000",
func doInitProviderWithSingleRegistry() {
	providerConfig = &ProviderConfig{
		ApplicationConfig: &ApplicationConfig{
			Organization: "dubbo_org",
			Name:         "dubbo",
			Module:       "module",
			Version:      "2.6.0",
			Owner:        "dubbo",
			Environment:  "test"},
		Registry: &RegistryConfig{
			Address:  "mock://127.0.0.1:2181",
			Username: "user1",
			Password: "pwd1",
		},
		Registries: map[string]*RegistryConfig{},
		Services: map[string]*ServiceConfig{
			"MockService": {
				InterfaceName: "com.MockService",
				Protocol:      "mock",
				Cluster:       "failover",
				Loadbalance:   "random",
vito.he's avatar
vito.he committed
				Retries:       "3",
				Group:         "huadong_idc",
				Version:       "1.0.0",
				Methods: []*MethodConfig{
					{
						Name:        "GetUser",
vito.he's avatar
vito.he committed
						Retries:     "2",
						Loadbalance: "random",
						Weight:      200,
					},
					{
						Name:        "GetUser1",
vito.he's avatar
vito.he committed
						Retries:     "2",
						Loadbalance: "random",
						Weight:      200,
					},
				},
			},
		},
		Protocols: map[string]*ProtocolConfig{
			"mock": {
				Name: "mock",
				Ip:   "127.0.0.1",
				Port: "20000",
			},
		},
	}
}

vito.he's avatar
vito.he committed
func Test_Export(t *testing.T) {
vito.he's avatar
vito.he committed
	extension.SetProtocol("registry", GetProtocol)

vito.he's avatar
vito.he committed
	for i := range providerConfig.Services {
		service := providerConfig.Services[i]
vito.he's avatar
vito.he committed
		service.Implement(&MockService{})
		service.Export()
	}
	providerConfig = nil
}
邹毅贤's avatar
邹毅贤 committed

func Test_getRandomPort(t *testing.T) {
	protocolConfigs := make([]*ProtocolConfig, 0, 3)

	ip, err := gxnet.GetLocalIP()
	protocolConfigs = append(protocolConfigs, &ProtocolConfig{
		Ip: ip,
	})
	protocolConfigs = append(protocolConfigs, &ProtocolConfig{
		Ip: ip,
	})
	protocolConfigs = append(protocolConfigs, &ProtocolConfig{
		Ip: ip,
	})
	assert.NoError(t, err)
	ports := getRandomPort(protocolConfigs)

	assert.Equal(t, ports.Len(), len(protocolConfigs))

	front := ports.Front()
	for {
		if front == nil {
			break
		}
		t.Logf("port:%v", front.Value)
		front = front.Next()
	}

	protocolConfigs = make([]*ProtocolConfig, 0, 3)
	ports = getRandomPort(protocolConfigs)
	assert.Equal(t, ports.Len(), len(protocolConfigs))
}