Skip to content
Snippets Groups Projects
reference_config_test.go 8.91 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 (
	"sync"
	"testing"
)

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

vito.he's avatar
vito.he committed
import (
	"github.com/apache/dubbo-go/cluster/cluster_impl"
	"github.com/apache/dubbo-go/common"
AlexStocks's avatar
AlexStocks committed
	"github.com/apache/dubbo-go/common/constant"
	"github.com/apache/dubbo-go/common/extension"
	"github.com/apache/dubbo-go/protocol"
vito.he's avatar
vito.he committed
)

var regProtocol protocol.Protocol

func doInitConsumer() {
vito.he's avatar
vito.he committed
	consumerConfig = &ConsumerConfig{
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",
			},
		},
		References: map[string]*ReferenceConfig{
			"MockService": {
邹毅贤's avatar
邹毅贤 committed
				id: "MockProvider",
				Params: map[string]string{
					"serviceid": "soa.mock",
xujianhai666's avatar
xujianhai666 committed
					"forks":     "5",
Ooo0oO0o0oO's avatar
Ooo0oO0o0oO committed
				Sticky:        false,
				Registry:      "shanghai_reg1,shanghai_reg2,hangzhou_reg1,hangzhou_reg2",
				InterfaceName: "com.MockService",
vito.he's avatar
vito.he committed
				Protocol:      "mock",
				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",
					},
					{
						Name:        "GetUser1",
vito.he's avatar
vito.he committed
						Loadbalance: "random",
Ooo0oO0o0oO's avatar
Ooo0oO0o0oO committed
						Sticky:      true,
邹毅贤's avatar
邹毅贤 committed
var mockProvider = new(MockProvider)

type MockProvider struct {
}

func (m *MockProvider) Reference() string {
	return "MockProvider"
}

func (m *MockProvider) CallBack(res common.CallbackResponse) {
}

func doInitConsumerAsync() {
	doInitConsumer()
	SetConsumerService(mockProvider)
	for _, v := range consumerConfig.References {
		v.Async = true
	}
}

func doInitConsumerWithSingleRegistry() {
	consumerConfig = &ConsumerConfig{
		ApplicationConfig: &ApplicationConfig{
			Organization: "dubbo_org",
			Name:         "dubbo",
			Module:       "module",
			Version:      "2.6.0",
			Owner:        "dubbo",
			Environment:  "test"},
		Registry: &RegistryConfig{
			Address:  "mock://27.0.0.1:2181",
			Username: "user1",
			Password: "pwd1",
		},
		Registries: map[string]*RegistryConfig{},
		References: map[string]*ReferenceConfig{
			"MockService": {
				Params: map[string]string{
					"serviceid": "soa.mock",
					"forks":     "5",
				},
				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",
					},
					{
						Name:        "GetUser1",
vito.he's avatar
vito.he committed
						Retries:     "2",
vito.he's avatar
vito.he committed
func Test_ReferMultireg(t *testing.T) {
vito.he's avatar
vito.he committed
	extension.SetProtocol("registry", GetProtocol)
	extension.SetCluster("registryAware", cluster_impl.NewRegistryAwareCluster)

	for _, reference := range consumerConfig.References {
		reference.Refer(nil)
vito.he's avatar
vito.he committed
		assert.NotNil(t, reference.invoker)
		assert.NotNil(t, reference.pxy)
	}
	consumerConfig = nil
}

func Test_Refer(t *testing.T) {
vito.he's avatar
vito.he committed
	extension.SetProtocol("registry", GetProtocol)
	extension.SetCluster("registryAware", cluster_impl.NewRegistryAwareCluster)

	for _, reference := range consumerConfig.References {
		reference.Refer(nil)
		assert.Equal(t, "soa.mock", reference.Params["serviceid"])
vito.he's avatar
vito.he committed
		assert.NotNil(t, reference.invoker)
		assert.NotNil(t, reference.pxy)
	}
	consumerConfig = nil
}
邹毅贤's avatar
邹毅贤 committed

func Test_ReferAsync(t *testing.T) {
	doInitConsumerAsync()
	extension.SetProtocol("registry", GetProtocol)
	extension.SetCluster("registryAware", cluster_impl.NewRegistryAwareCluster)

	for _, reference := range consumerConfig.References {
xujianhai666's avatar
xujianhai666 committed
		reference.Refer(nil)
邹毅贤's avatar
邹毅贤 committed
		assert.Equal(t, "soa.mock", reference.Params["serviceid"])
		assert.NotNil(t, reference.invoker)
		assert.NotNil(t, reference.pxy)
		assert.NotNil(t, reference.pxy.GetCallback())
	}
	consumerConfig = nil
}

func Test_ReferP2P(t *testing.T) {
	extension.SetProtocol("dubbo", GetProtocol)
	m := consumerConfig.References["MockService"]
	m.Url = "dubbo://127.0.0.1:20000"

	for _, reference := range consumerConfig.References {
		reference.Refer(nil)
		assert.NotNil(t, reference.invoker)
		assert.NotNil(t, reference.pxy)
	}
	consumerConfig = nil
}
func Test_ReferMultiP2P(t *testing.T) {
	extension.SetProtocol("dubbo", GetProtocol)
	m := consumerConfig.References["MockService"]
	m.Url = "dubbo://127.0.0.1:20000;dubbo://127.0.0.2:20000"

	for _, reference := range consumerConfig.References {
		reference.Refer(nil)
		assert.NotNil(t, reference.invoker)
		assert.NotNil(t, reference.pxy)
	}
	consumerConfig = nil
}

func Test_ReferMultiP2PWithReg(t *testing.T) {
	extension.SetProtocol("dubbo", GetProtocol)
	extension.SetProtocol("registry", GetProtocol)
	m := consumerConfig.References["MockService"]
	m.Url = "dubbo://127.0.0.1:20000;registry://127.0.0.2:20000"
vito.he's avatar
vito.he committed

	for _, reference := range consumerConfig.References {
		reference.Refer(nil)
		assert.NotNil(t, reference.invoker)
		assert.NotNil(t, reference.pxy)
	}
	consumerConfig = nil
}
vito.he's avatar
vito.he committed
func Test_Implement(t *testing.T) {
vito.he's avatar
vito.he committed
	extension.SetProtocol("registry", GetProtocol)
	extension.SetCluster("registryAware", cluster_impl.NewRegistryAwareCluster)
	for _, reference := range consumerConfig.References {
		reference.Refer(nil)
vito.he's avatar
vito.he committed
		reference.Implement(&MockService{})
		assert.NotNil(t, reference.GetRPCService())

	}
	consumerConfig = nil
}
xujianhai666's avatar
xujianhai666 committed
func Test_Forking(t *testing.T) {
xujianhai666's avatar
xujianhai666 committed
	extension.SetProtocol("dubbo", GetProtocol)
	extension.SetProtocol("registry", GetProtocol)
	m := consumerConfig.References["MockService"]
	m.Url = "dubbo://127.0.0.1:20000;registry://127.0.0.2:20000"

	for _, reference := range consumerConfig.References {
		reference.Refer(nil)
xujianhai666's avatar
xujianhai666 committed
		forks := int(reference.invoker.GetUrl().GetParamInt(constant.FORKS_KEY, constant.DEFAULT_FORKS))
		assert.Equal(t, 5, forks)
		assert.NotNil(t, reference.pxy)
		assert.NotNil(t, reference.Cluster)
	}
	consumerConfig = nil
}

Ooo0oO0o0oO's avatar
Ooo0oO0o0oO committed
func Test_Sticky(t *testing.T) {
	doInitConsumer()
	extension.SetProtocol("dubbo", GetProtocol)
	extension.SetProtocol("registry", GetProtocol)
	m := consumerConfig.References["MockService"]
	m.Url = "dubbo://127.0.0.1:20000;registry://127.0.0.2:20000"

	reference := consumerConfig.References["MockService"]
xujianhai666's avatar
xujianhai666 committed
	reference.Refer(nil)
Ooo0oO0o0oO's avatar
Ooo0oO0o0oO committed
	referenceSticky := reference.invoker.GetUrl().GetParam(constant.STICKY_KEY, "false")
	assert.Equal(t, "false", referenceSticky)

	method0StickKey := reference.invoker.GetUrl().GetMethodParam(reference.Methods[0].Name, constant.STICKY_KEY, "false")
	assert.Equal(t, "false", method0StickKey)
	method1StickKey := reference.invoker.GetUrl().GetMethodParam(reference.Methods[1].Name, constant.STICKY_KEY, "false")
	assert.Equal(t, "true", method1StickKey)
}

vito.he's avatar
vito.he committed
func GetProtocol() protocol.Protocol {
	if regProtocol != nil {
		return regProtocol
	}
	return newRegistryProtocol()
}

func newRegistryProtocol() protocol.Protocol {
	return &mockRegistryProtocol{}
}

vito.he's avatar
vito.he committed

func (*mockRegistryProtocol) Refer(url common.URL, impl interface{}) protocol.Invoker {
vito.he's avatar
vito.he committed
	return protocol.NewBaseInvoker(url)
}

func (*mockRegistryProtocol) Export(invoker protocol.Invoker) protocol.Exporter {
	return protocol.NewBaseExporter("test", invoker, &sync.Map{})
}

func (*mockRegistryProtocol) Destroy() {}