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

package directory

import (
	"net/url"
	"strconv"
	"testing"
	"time"
)
	"github.com/golang/mock/gomock"
	"github.com/stretchr/testify/assert"
)
	"github.com/apache/dubbo-go/cluster/cluster_impl"
邹毅贤's avatar
邹毅贤 committed
	_ "github.com/apache/dubbo-go/cluster/router"
邹毅贤's avatar
邹毅贤 committed
	_ "github.com/apache/dubbo-go/cluster/router/condition"
	"github.com/apache/dubbo-go/common"
	"github.com/apache/dubbo-go/common/constant"
	"github.com/apache/dubbo-go/common/extension"
vito.he's avatar
vito.he committed
	"github.com/apache/dubbo-go/config"
	"github.com/apache/dubbo-go/protocol/invocation"
	"github.com/apache/dubbo-go/protocol/mock"
	"github.com/apache/dubbo-go/protocol/protocolwrapper"
	"github.com/apache/dubbo-go/registry"
	"github.com/apache/dubbo-go/remoting"
vito.he's avatar
vito.he committed
func init() {
flycash's avatar
flycash committed
	config.SetConsumerConfig(config.ConsumerConfig{
		BaseConfig: config.BaseConfig{
			ApplicationConfig: &config.ApplicationConfig{Name: "test-application"},
		},
	})
vito.he's avatar
vito.he committed
}
func TestSubscribe(t *testing.T) {
vito.he's avatar
vito.he committed
	registryDirectory, _ := normalRegistryDir()

	time.Sleep(1e9)
	assert.Len(t, registryDirectory.cacheInvokers, 3)
}

vito.he's avatar
vito.he committed
////Deprecated! not support delete
//func TestSubscribe_Delete(t *testing.T) {
//	registryDirectory, mockRegistry := normalRegistryDir()
//	time.Sleep(1e9)
//	assert.Len(t, registryDirectory.cacheInvokers, 3)
flycash's avatar
flycash committed
//	mockRegistry.MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeDel, Service: *event.NewURLWithOptions(event.WithPath("TEST0"), event.WithProtocol("dubbo"))})
vito.he's avatar
vito.he committed
//	time.Sleep(1e9)
//	assert.Len(t, registryDirectory.cacheInvokers, 2)
//}
func TestSubscribe_InvalidUrl(t *testing.T) {
	url, _ := common.NewURL("mock://127.0.0.1:1111")
vito.he's avatar
vito.he committed
	mockRegistry, _ := registry.NewMockRegistry(&common.URL{})
haohongfan's avatar
haohongfan committed
	_, err := NewRegistryDirectory(url, mockRegistry)
	assert.Error(t, err)
}

func TestSubscribe_Group(t *testing.T) {
	extension.SetProtocol(protocolwrapper.FILTER, protocolwrapper.NewMockProtocolFilter)
vito.he's avatar
vito.he committed
	extension.SetCluster("mock", cluster_impl.NewMockCluster)
	regurl, _ := common.NewURL("mock://127.0.0.1:1111")
	suburl, _ := common.NewURL("dubbo://127.0.0.1:20000")
vito.he's avatar
vito.he committed
	suburl.SetParam(constant.CLUSTER_KEY, "mock")
haohongfan's avatar
haohongfan committed
	regurl.SubURL = suburl
vito.he's avatar
vito.he committed
	mockRegistry, _ := registry.NewMockRegistry(&common.URL{})
haohongfan's avatar
haohongfan committed
	dir, _ := NewRegistryDirectory(regurl, mockRegistry)
	go dir.(*RegistryDirectory).subscribe(common.NewURLWithOptions(common.WithPath("testservice")))
	//for group1
	urlmap := url.Values{}
	urlmap.Set(constant.GROUP_KEY, "group1")
	urlmap.Set(constant.CLUSTER_KEY, "failover") //to test merge url
	for i := 0; i < 3; i++ {
haohongfan's avatar
haohongfan committed
		mockRegistry.(*registry.MockRegistry).MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: common.NewURLWithOptions(common.WithPath("TEST"+strconv.FormatInt(int64(i), 10)), common.WithProtocol("dubbo"),
vito.he's avatar
vito.he committed
			common.WithParams(urlmap))})
	}
	//for group2
	urlmap2 := url.Values{}
	urlmap2.Set(constant.GROUP_KEY, "group2")
	urlmap2.Set(constant.CLUSTER_KEY, "failover") //to test merge url
	for i := 0; i < 3; i++ {
haohongfan's avatar
haohongfan committed
		mockRegistry.(*registry.MockRegistry).MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: common.NewURLWithOptions(common.WithPath("TEST"+strconv.FormatInt(int64(i), 10)), common.WithProtocol("dubbo"),
vito.he's avatar
vito.he committed
			common.WithParams(urlmap2))})
	assert.Len(t, dir.(*RegistryDirectory).cacheInvokers, 2)
func Test_Destroy(t *testing.T) {
vito.he's avatar
vito.he committed
	registryDirectory, _ := normalRegistryDir()

邹毅贤's avatar
邹毅贤 committed
	time.Sleep(3e9)
vito.he's avatar
vito.he committed
	assert.Len(t, registryDirectory.cacheInvokers, 3)
	assert.Equal(t, true, registryDirectory.IsAvailable())

	registryDirectory.Destroy()
	assert.Len(t, registryDirectory.cacheInvokers, 0)
	assert.Equal(t, false, registryDirectory.IsAvailable())
}

func Test_List(t *testing.T) {
	registryDirectory, _ := normalRegistryDir()

邹毅贤's avatar
邹毅贤 committed
	assert.Len(t, registryDirectory.List(&invocation.RPCInvocation{}), 3)
vito.he's avatar
vito.he committed
	assert.Equal(t, true, registryDirectory.IsAvailable())

vito.he's avatar
vito.he committed
}
vito.he's avatar
vito.he committed
func Test_MergeProviderUrl(t *testing.T) {
	registryDirectory, mockRegistry := normalRegistryDir(true)
	providerUrl, _ := common.NewURL("dubbo://0.0.0.0:20000/org.apache.dubbo-go.mockService",
vito.he's avatar
vito.he committed
		common.WithParamsValue(constant.CLUSTER_KEY, "mock1"),
		common.WithParamsValue(constant.GROUP_KEY, "group"),
		common.WithParamsValue(constant.VERSION_KEY, "1.0.0"))
	mockRegistry.MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: providerUrl})
	time.Sleep(1e9)
	assert.Len(t, registryDirectory.cacheInvokers, 1)
	if len(registryDirectory.cacheInvokers) > 0 {
		assert.Equal(t, "mock", registryDirectory.cacheInvokers[0].GetUrl().GetParam(constant.CLUSTER_KEY, ""))
	}

vito.he's avatar
vito.he committed
func Test_MergeOverrideUrl(t *testing.T) {
	registryDirectory, mockRegistry := normalRegistryDir(true)
	providerUrl, _ := common.NewURL("dubbo://0.0.0.0:20000/org.apache.dubbo-go.mockService",
vito.he's avatar
vito.he committed
		common.WithParamsValue(constant.CLUSTER_KEY, "mock"),
		common.WithParamsValue(constant.GROUP_KEY, "group"),
		common.WithParamsValue(constant.VERSION_KEY, "1.0.0"))
	mockRegistry.MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: providerUrl})
Loop1:
	for {
		if len(registryDirectory.cacheInvokers) > 0 {
			overrideUrl, _ := common.NewURL("override://0.0.0.0:20000/org.apache.dubbo-go.mockService",
				common.WithParamsValue(constant.CLUSTER_KEY, "mock1"),
				common.WithParamsValue(constant.GROUP_KEY, "group"),
				common.WithParamsValue(constant.VERSION_KEY, "1.0.0"))
			mockRegistry.MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: overrideUrl})
		Loop2:
			for {
				if len(registryDirectory.cacheInvokers) > 0 {
					if "mock1" == registryDirectory.cacheInvokers[0].GetUrl().GetParam(constant.CLUSTER_KEY, "") {
						assert.Len(t, registryDirectory.cacheInvokers, 1)
						assert.True(t, true)
						break Loop2
					} else {
						time.Sleep(500 * time.Millisecond)
					}
				}
			}
			break Loop1
		}
vito.he's avatar
vito.he committed
	}
vito.he's avatar
vito.he committed

watermelo's avatar
watermelo committed
func Test_toGroupInvokers(t *testing.T) {
	registryDirectory, _ := normalRegistryDir()
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()
	invoker := mock.NewMockInvoker(ctrl)
	newUrl, _ := common.NewURL("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider")
	invoker.EXPECT().GetUrl().Return(newUrl).AnyTimes()

	registryDirectory.cacheInvokersMap.Store("group1", invoker)
	registryDirectory.cacheInvokersMap.Store("group2", invoker)
	registryDirectory.cacheInvokersMap.Store("group1", invoker)
	groupInvokers := registryDirectory.toGroupInvokers()
	assert.Len(t, groupInvokers, 2)
func normalRegistryDir(noMockEvent ...bool) (*RegistryDirectory, *registry.MockRegistry) {
vito.he's avatar
vito.he committed
	extension.SetProtocol(protocolwrapper.FILTER, protocolwrapper.NewMockProtocolFilter)

	url, _ := common.NewURL("mock://127.0.0.1:1111")
	suburl, _ := common.NewURL(
		"dubbo://127.0.0.1:20000/org.apache.dubbo-go.mockService",
		common.WithParamsValue(constant.CLUSTER_KEY, "mock"),
		common.WithParamsValue(constant.GROUP_KEY, "group"),
		common.WithParamsValue(constant.VERSION_KEY, "1.0.0"),
	)
haohongfan's avatar
haohongfan committed
	url.SubURL = suburl
vito.he's avatar
vito.he committed
	mockRegistry, _ := registry.NewMockRegistry(&common.URL{})
haohongfan's avatar
haohongfan committed
	dir, _ := NewRegistryDirectory(url, mockRegistry)
haohongfan's avatar
haohongfan committed
	go dir.(*RegistryDirectory).subscribe(suburl)
vito.he's avatar
vito.he committed
	if len(noMockEvent) == 0 {
		for i := 0; i < 3; i++ {
			mockRegistry.(*registry.MockRegistry).MockEvent(
				&registry.ServiceEvent{
					Action: remoting.EventTypeAdd,
haohongfan's avatar
haohongfan committed
					Service: common.NewURLWithOptions(
						common.WithPath("TEST"+strconv.FormatInt(int64(i), 10)),
						common.WithProtocol("dubbo"),
					),
				},
			)
vito.he's avatar
vito.he committed
		}
	return dir.(*RegistryDirectory), mockRegistry.(*registry.MockRegistry)