dubbo.apache.org/dubbo-go/v3@v3.1.1/registry/directory/directory_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 directory
    19  
    20  import (
    21  	"strconv"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  import (
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  import (
    31  	"dubbo.apache.org/dubbo-go/v3/cluster/cluster"
    32  	_ "dubbo.apache.org/dubbo-go/v3/cluster/router/tag"
    33  	"dubbo.apache.org/dubbo-go/v3/common"
    34  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    35  	"dubbo.apache.org/dubbo-go/v3/common/extension"
    36  	"dubbo.apache.org/dubbo-go/v3/config"
    37  	"dubbo.apache.org/dubbo-go/v3/protocol/invocation"
    38  	"dubbo.apache.org/dubbo-go/v3/protocol/protocolwrapper"
    39  	"dubbo.apache.org/dubbo-go/v3/registry"
    40  	"dubbo.apache.org/dubbo-go/v3/remoting"
    41  )
    42  
    43  func init() {
    44  	config.SetRootConfig(config.RootConfig{
    45  		Application: &config.ApplicationConfig{Name: "test-application"},
    46  	})
    47  }
    48  
    49  func TestSubscribe(t *testing.T) {
    50  	registryDirectory, _ := normalRegistryDir()
    51  
    52  	time.Sleep(1e9)
    53  	assert.Len(t, registryDirectory.cacheInvokers, 3)
    54  }
    55  
    56  func TestSubscribe_InvalidUrl(t *testing.T) {
    57  	url, _ := common.NewURL("mock://127.0.0.1:1111")
    58  	mockRegistry, _ := registry.NewMockRegistry(&common.URL{})
    59  	_, err := NewRegistryDirectory(url, mockRegistry)
    60  	assert.Error(t, err)
    61  }
    62  
    63  func Test_Destroy(t *testing.T) {
    64  	registryDirectory, _ := normalRegistryDir()
    65  
    66  	time.Sleep(3e9)
    67  	assert.Len(t, registryDirectory.cacheInvokers, 3)
    68  	assert.Equal(t, true, registryDirectory.IsAvailable())
    69  
    70  	registryDirectory.Destroy()
    71  	assert.Len(t, registryDirectory.cacheInvokers, 0)
    72  	assert.Equal(t, false, registryDirectory.IsAvailable())
    73  }
    74  
    75  func Test_List(t *testing.T) {
    76  	registryDirectory, _ := normalRegistryDir()
    77  
    78  	time.Sleep(6e9)
    79  	assert.Len(t, registryDirectory.List(&invocation.RPCInvocation{}), 3)
    80  	assert.Equal(t, true, registryDirectory.IsAvailable())
    81  }
    82  
    83  func Test_MergeProviderUrl(t *testing.T) {
    84  	registryDirectory, mockRegistry := normalRegistryDir(true)
    85  	providerUrl, _ := common.NewURL("dubbo://0.0.0.0:20000/org.apache.dubbo-go.mockService",
    86  		common.WithParamsValue(constant.ClusterKey, "mock1"),
    87  		common.WithParamsValue(constant.GroupKey, "group"),
    88  		common.WithParamsValue(constant.VersionKey, "1.0.0"))
    89  	mockRegistry.MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: providerUrl})
    90  	time.Sleep(1e9)
    91  	assert.Len(t, registryDirectory.cacheInvokers, 1)
    92  	if len(registryDirectory.cacheInvokers) > 0 {
    93  		assert.Equal(t, "mock1", registryDirectory.cacheInvokers[0].GetURL().GetParam(constant.ClusterKey, ""))
    94  	}
    95  }
    96  
    97  func Test_MergeOverrideUrl(t *testing.T) {
    98  	registryDirectory, mockRegistry := normalRegistryDir(true)
    99  	providerUrl, _ := common.NewURL("dubbo://0.0.0.0:20000/org.apache.dubbo-go.mockService",
   100  		common.WithParamsValue(constant.ClusterKey, "mock"),
   101  		common.WithParamsValue(constant.GroupKey, "group"),
   102  		common.WithParamsValue(constant.VersionKey, "1.0.0"))
   103  	mockRegistry.MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: providerUrl})
   104  Loop1:
   105  	for {
   106  		if len(registryDirectory.cacheInvokers) > 0 {
   107  			overrideUrl, _ := common.NewURL("override://0.0.0.0:20000/org.apache.dubbo-go.mockService",
   108  				common.WithParamsValue(constant.ClusterKey, "mock1"),
   109  				common.WithParamsValue(constant.GroupKey, "group"),
   110  				common.WithParamsValue(constant.VersionKey, "1.0.0"))
   111  			mockRegistry.MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: overrideUrl})
   112  		Loop2:
   113  			for {
   114  				if len(registryDirectory.cacheInvokers) > 0 {
   115  					if "mock1" == registryDirectory.cacheInvokers[0].GetURL().GetParam(constant.ClusterKey, "") {
   116  						assert.Len(t, registryDirectory.cacheInvokers, 1)
   117  						assert.True(t, true)
   118  						break Loop2
   119  					} else {
   120  						time.Sleep(500 * time.Millisecond)
   121  					}
   122  				}
   123  			}
   124  			break Loop1
   125  		}
   126  	}
   127  }
   128  
   129  func Test_RefreshUrl(t *testing.T) {
   130  	registryDirectory, mockRegistry := normalRegistryDir()
   131  	providerUrl, _ := common.NewURL("dubbo://0.0.0.0:20011/org.apache.dubbo-go.mockService",
   132  		common.WithParamsValue(constant.ClusterKey, "mock1"),
   133  		common.WithParamsValue(constant.GroupKey, "group"),
   134  		common.WithParamsValue(constant.VersionKey, "1.0.0"))
   135  	providerUrl2, _ := common.NewURL("dubbo://0.0.0.0:20012/org.apache.dubbo-go.mockService",
   136  		common.WithParamsValue(constant.ClusterKey, "mock1"),
   137  		common.WithParamsValue(constant.GroupKey, "group"),
   138  		common.WithParamsValue(constant.VersionKey, "1.0.0"))
   139  	time.Sleep(1e9)
   140  	assert.Len(t, registryDirectory.cacheInvokers, 3)
   141  	mockRegistry.MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: providerUrl})
   142  	time.Sleep(1e9)
   143  	assert.Len(t, registryDirectory.cacheInvokers, 4)
   144  	mockRegistry.MockEvents([]*registry.ServiceEvent{{Action: remoting.EventTypeUpdate, Service: providerUrl}})
   145  	time.Sleep(1e9)
   146  	assert.Len(t, registryDirectory.cacheInvokers, 1)
   147  	mockRegistry.MockEvents([]*registry.ServiceEvent{
   148  		{Action: remoting.EventTypeUpdate, Service: providerUrl},
   149  		{Action: remoting.EventTypeUpdate, Service: providerUrl2},
   150  	})
   151  	time.Sleep(1e9)
   152  	assert.Len(t, registryDirectory.cacheInvokers, 2)
   153  	// clear all address
   154  	mockRegistry.MockEvents([]*registry.ServiceEvent{})
   155  	time.Sleep(1e9)
   156  	assert.Len(t, registryDirectory.cacheInvokers, 0)
   157  }
   158  
   159  func normalRegistryDir(noMockEvent ...bool) (*RegistryDirectory, *registry.MockRegistry) {
   160  	extension.SetProtocol(protocolwrapper.FILTER, protocolwrapper.NewMockProtocolFilter)
   161  
   162  	url, _ := common.NewURL("mock://127.0.0.1:1111")
   163  	suburl, _ := common.NewURL(
   164  		"dubbo://127.0.0.1:20000/org.apache.dubbo-go.mockService",
   165  		common.WithParamsValue(constant.ClusterKey, "mock"),
   166  		common.WithParamsValue(constant.GroupKey, "group"),
   167  		common.WithParamsValue(constant.VersionKey, "1.0.0"),
   168  	)
   169  	url.SubURL = suburl
   170  	mockRegistry, _ := registry.NewMockRegistry(&common.URL{})
   171  	dir, _ := NewRegistryDirectory(url, mockRegistry)
   172  
   173  	go dir.(*RegistryDirectory).Subscribe(suburl)
   174  	if len(noMockEvent) == 0 {
   175  		for i := 0; i < 3; i++ {
   176  			mockRegistry.(*registry.MockRegistry).MockEvent(
   177  				&registry.ServiceEvent{
   178  					Action: remoting.EventTypeAdd,
   179  					Service: common.NewURLWithOptions(
   180  						common.WithPath("TEST"+strconv.FormatInt(int64(i), 10)),
   181  						common.WithProtocol("dubbo"),
   182  					),
   183  				},
   184  			)
   185  		}
   186  	}
   187  	return dir.(*RegistryDirectory), mockRegistry.(*registry.MockRegistry)
   188  }
   189  
   190  func TestToGroupInvokers(t *testing.T) {
   191  	t.Run("SameGroup", func(t *testing.T) {
   192  		registryDirectory, mockRegistry := normalRegistryDir(true)
   193  		providerUrl, _ := common.NewURL("dubbo://0.0.0.0:20000/org.apache.dubbo-go.mockService",
   194  			common.WithParamsValue(constant.ClusterKey, "mock1"),
   195  			common.WithParamsValue(constant.GroupKey, "group"),
   196  			common.WithParamsValue(constant.VersionKey, "1.0.0"))
   197  		mockRegistry.MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: providerUrl})
   198  		time.Sleep(1e9)
   199  		assert.True(t, len(registryDirectory.toGroupInvokers()) == 1)
   200  		providerUrl1, _ := common.NewURL("dubbo://0.0.0.0:20001/org.apache.dubbo-go.mockService",
   201  			common.WithParamsValue(constant.ClusterKey, "mock1"),
   202  			common.WithParamsValue(constant.GroupKey, "group"),
   203  			common.WithParamsValue(constant.VersionKey, "1.0.0"))
   204  		mockRegistry.MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: providerUrl1})
   205  		time.Sleep(1e9)
   206  		assert.True(t, len(registryDirectory.toGroupInvokers()) == 2)
   207  	})
   208  	t.Run("DifferentGroup", func(t *testing.T) {
   209  		extension.SetCluster("mock", cluster.NewMockCluster)
   210  
   211  		registryDirectory, mockRegistry := normalRegistryDir(true)
   212  		providerUrl, _ := common.NewURL("dubbo://0.0.0.0:20000/org.apache.dubbo-go.mockService",
   213  			common.WithParamsValue(constant.ClusterKey, "mock1"),
   214  			common.WithParamsValue(constant.GroupKey, "group"),
   215  			common.WithParamsValue(constant.VersionKey, "1.0.0"))
   216  		mockRegistry.MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: providerUrl})
   217  		time.Sleep(1e9)
   218  		assert.True(t, len(registryDirectory.toGroupInvokers()) == 1)
   219  
   220  		providerUrl1, _ := common.NewURL("dubbo://0.0.0.0:20000/org.apache.dubbo-go.mockService",
   221  			common.WithParamsValue(constant.ClusterKey, "mock1"),
   222  			common.WithParamsValue(constant.GroupKey, "group1"),
   223  			common.WithParamsValue(constant.VersionKey, "1.0.0"))
   224  		mockRegistry.MockEvent(&registry.ServiceEvent{Action: remoting.EventTypeAdd, Service: providerUrl1})
   225  		time.Sleep(1e9)
   226  		assert.True(t, len(registryDirectory.toGroupInvokers()) == 2)
   227  	})
   228  }