From bfbcdb4199bd766c2256fec8a3d18a0f26ed0fd5 Mon Sep 17 00:00:00 2001
From: LaurenceLiZhixin <382673304@qq.com>
Date: Fri, 19 Mar 2021 11:20:19 +0800
Subject: [PATCH] fix: fix test

---
 cluster/router/chain/chain.go      |  89 +--------
 cluster/router/chain/chain_test.go | 286 -----------------------------
 2 files changed, 1 insertion(+), 374 deletions(-)
 delete mode 100644 cluster/router/chain/chain_test.go

diff --git a/cluster/router/chain/chain.go b/cluster/router/chain/chain.go
index 4ce67a32d..2abce544a 100644
--- a/cluster/router/chain/chain.go
+++ b/cluster/router/chain/chain.go
@@ -76,7 +76,7 @@ func (c *RouterChain) GetNotifyChan() chan struct{} {
 
 // Route Loop routers in RouterChain and call Route method to determine the target invokers list.
 func (c *RouterChain) Route(url *common.URL, invocation protocol.Invocation) []protocol.Invoker {
-	finalInvokers := make([]protocol.Invoker, 0)
+	finalInvokers := c.invokers
 	for _, r := range c.copyRouters() {
 		finalInvokers = r.Route(c.invokers, url, invocation)
 	}
@@ -133,68 +133,6 @@ func (c *RouterChain) copyInvokers() []protocol.Invoker {
 	return ret
 }
 
-// loadCache loads cache from sync.Value to guarantee the visibility
-func (c *RouterChain) loadCache() *InvokerCache {
-	v := c.cache.Load()
-	if v == nil {
-		return nil
-	}
-
-	return v.(*InvokerCache)
-}
-
-// copyInvokerIfNecessary compares chain's invokers copy and cache's invokers copy, to avoid copy as much as possible
-func (c *RouterChain) copyInvokerIfNecessary(cache *InvokerCache) []protocol.Invoker {
-	var invokers []protocol.Invoker
-	if cache != nil {
-		invokers = cache.invokers
-	}
-
-	c.mutex.RLock()
-	defer c.mutex.RUnlock()
-	if isInvokersChanged(invokers, c.invokers) {
-		invokers = c.copyInvokers()
-	}
-	return invokers
-}
-
-// buildCache builds address cache with the new invokers for all poolable routers.
-func (c *RouterChain) buildCache() {
-	origin := c.loadCache()
-	invokers := c.copyInvokerIfNecessary(origin)
-	if len(invokers) == 0 {
-		return
-	}
-
-	var (
-		mutex sync.Mutex
-		wg    sync.WaitGroup
-	)
-
-	cache := BuildCache(invokers)
-	for _, r := range c.copyRouters() {
-		if p, ok := r.(router.Poolable); ok {
-			wg.Add(1)
-			go func(p router.Poolable) {
-				defer wg.Done()
-				pool, info := poolRouter(p, origin, invokers)
-				mutex.Lock()
-				defer mutex.Unlock()
-				cache.pools[p.Name()] = pool
-				cache.metadatas[p.Name()] = info
-			}(p)
-		}
-	}
-	wg.Wait()
-
-	c.cache.Store(cache)
-}
-
-// URL Return URL in RouterChain
-func (c *RouterChain) URL() *common.URL {
-	return c.url
-}
-
 func SetVSAndDRConfigByte(vs, dr []byte) {
 	virtualServiceConfigByte = vs
 	destinationRuleConfigByte = dr
@@ -240,34 +178,9 @@ func NewRouterChain(url *common.URL) (*RouterChain, error) {
 		chain.url = url
 	}
 
-	//go chain.loop()
 	return chain, nil
 }
 
-// poolRouter calls poolable router's Pool() to create new address pool and address metadata if necessary.
-// If the corresponding cache entry exists, and the poolable router answers no need to re-pool (possibly because its
-// rule doesn't change), and the address list doesn't change, then the existing data will be re-used.
-func poolRouter(p router.Poolable, origin *InvokerCache, invokers []protocol.Invoker) (router.AddrPool, router.AddrMetadata) {
-	name := p.Name()
-	if isCacheMiss(origin, name) || p.ShouldPool() || &(origin.invokers) != &invokers {
-		logger.Debugf("build address cache for router %q", name)
-		return p.Pool(invokers)
-	}
-
-	logger.Debugf("reuse existing address cache for router %q", name)
-	return origin.pools[name], origin.metadatas[name]
-}
-
-// isCacheMiss checks if the corresponding cache entry for a poolable router has already existed.
-// False returns when the cache is nil, or cache's pool is nil, or cache's invokers snapshot is nil, or the entry
-// doesn't exist.
-func isCacheMiss(cache *InvokerCache, key string) bool {
-	if cache == nil || cache.pools == nil || cache.invokers == nil || cache.pools[key] == nil {
-		return true
-	}
-	return false
-}
-
 // isInvokersChanged compares new invokers on the right changes, compared with the old invokers on the left.
 func isInvokersChanged(left []protocol.Invoker, right []protocol.Invoker) bool {
 	if len(right) != len(left) {
diff --git a/cluster/router/chain/chain_test.go b/cluster/router/chain/chain_test.go
deleted file mode 100644
index 435ba9570..000000000
--- a/cluster/router/chain/chain_test.go
+++ /dev/null
@@ -1,286 +0,0 @@
-/*
- * 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.
- */
-
-package chain
-
-import (
-	"encoding/base64"
-	"fmt"
-	"testing"
-	"time"
-)
-
-import (
-	"github.com/stretchr/testify/assert"
-)
-
-import (
-	"github.com/apache/dubbo-go/cluster/router"
-	"github.com/apache/dubbo-go/common"
-	"github.com/apache/dubbo-go/common/config"
-	"github.com/apache/dubbo-go/common/constant"
-	"github.com/apache/dubbo-go/common/extension"
-	_ "github.com/apache/dubbo-go/config_center/zookeeper"
-	"github.com/apache/dubbo-go/protocol"
-	"github.com/apache/dubbo-go/protocol/invocation"
-	"github.com/apache/dubbo-go/remoting/zookeeper"
-)
-
-const (
-	localIP    = "127.0.0.1"
-	test1234IP = "1.2.3.4"
-	test1111IP = "1.1.1.1"
-	test0000IP = "0.0.0.0"
-	port20000  = 20000
-
-	path             = "/dubbo/config/dubbo/test-condition.condition-router"
-	zkFormat         = "zookeeper://%s:%d"
-	consumerFormat   = "consumer://%s/com.foo.BarService"
-	dubboForamt      = "dubbo://%s:%d/com.foo.BarService"
-	anyUrlFormat     = "condition://%s/com.foo.BarService"
-	zk               = "zookeeper"
-	applicationKey   = "test-condition"
-	applicationField = "application"
-	forceField       = "force"
-	forceValue       = "true"
-)
-
-func TestNewRouterChain(t *testing.T) {
-	ts, z, _, err := zookeeper.NewMockZookeeperClient("test", 15*time.Second)
-	assert.NoError(t, err)
-	err = z.Create(path)
-	assert.NoError(t, err)
-	testyml := `scope: application
-key: mock-app
-enabled: true
-force: true
-runtime: false
-conditions:
-  - => host != 172.22.3.91
-`
-
-	_, err = z.Conn.Set(path, []byte(testyml), 0)
-	assert.NoError(t, err)
-	defer func() {
-		_ = ts.Stop()
-		z.Close()
-	}()
-
-	zkUrl, _ := common.NewURL(fmt.Sprintf(zkFormat, localIP, ts.Servers[0].Port))
-	configuration, err := extension.GetConfigCenterFactory(zk).GetDynamicConfiguration(zkUrl)
-	config.GetEnvInstance().SetDynamicConfiguration(configuration)
-
-	assert.Nil(t, err)
-	assert.NotNil(t, configuration)
-
-	chain, err := NewRouterChain(getRouteUrl(applicationKey))
-	assert.Nil(t, err)
-	assert.Equal(t, 1, len(chain.routers))
-}
-
-func TestNewRouterChainURLNil(t *testing.T) {
-	chain, err := NewRouterChain(nil)
-	assert.NoError(t, err)
-	assert.NotNil(t, chain)
-}
-
-func TestRouterChainAddRouters(t *testing.T) {
-	ts, z, _, err := zookeeper.NewMockZookeeperClient("test", 15*time.Second)
-	assert.NoError(t, err)
-	err = z.Create(path)
-	assert.NoError(t, err)
-
-	testyml := `scope: application
-key: mock-app
-enabled: true
-force: true
-runtime: false
-conditions:
-  - => host != 172.22.3.91
-`
-
-	_, err = z.Conn.Set(path, []byte(testyml), 0)
-	assert.NoError(t, err)
-	defer func() {
-		_ = ts.Stop()
-		assert.NoError(t, err)
-		z.Close()
-	}()
-
-	zkUrl, _ := common.NewURL(fmt.Sprintf(zkFormat, localIP, ts.Servers[0].Port))
-	configuration, err := extension.GetConfigCenterFactory(zk).GetDynamicConfiguration(zkUrl)
-	assert.NoError(t, err)
-	config.GetEnvInstance().SetDynamicConfiguration(configuration)
-
-	chain, err := NewRouterChain(getConditionRouteUrl(applicationKey))
-	assert.Nil(t, err)
-	assert.Equal(t, 2, len(chain.routers))
-
-	url := getConditionRouteUrl(applicationKey)
-	assert.NotNil(t, url)
-	factory := extension.GetRouterFactory(url.Protocol)
-	notify := make(chan struct{})
-	go func() {
-		for range notify {
-		}
-	}()
-	r, err := factory.NewPriorityRouter([]byte{}, []byte{}, notify)
-	assert.Nil(t, err)
-	assert.NotNil(t, r)
-
-	routers := make([]router.PriorityRouter, 0)
-	routers = append(routers, r)
-	chain.AddRouters(routers)
-	assert.Equal(t, 3, len(chain.routers))
-}
-
-func TestRouterChainRoute(t *testing.T) {
-	ts, z, _, err := zookeeper.NewMockZookeeperClient("test", 15*time.Second)
-	defer func() {
-		err = ts.Stop()
-		assert.NoError(t, err)
-		z.Close()
-	}()
-
-	zkUrl, _ := common.NewURL(fmt.Sprintf(zkFormat, localIP, ts.Servers[0].Port))
-	configuration, err := extension.GetConfigCenterFactory(zk).GetDynamicConfiguration(zkUrl)
-	assert.NoError(t, err)
-	config.GetEnvInstance().SetDynamicConfiguration(configuration)
-
-	chain, err := NewRouterChain(getConditionRouteUrl(applicationKey))
-	assert.Nil(t, err)
-	assert.Equal(t, 1, len(chain.routers))
-
-	url := getConditionRouteUrl(applicationKey)
-	assert.NotNil(t, url)
-
-	var invokers []protocol.Invoker
-	dubboURL, _ := common.NewURL(fmt.Sprintf(dubboForamt, test1234IP, port20000))
-	invokers = append(invokers, protocol.NewBaseInvoker(dubboURL))
-	chain.SetInvokers(invokers)
-	chain.buildCache()
-
-	targetURL, _ := common.NewURL(fmt.Sprintf(consumerFormat, test1111IP))
-	inv := &invocation.RPCInvocation{}
-	finalInvokers := chain.Route(targetURL, inv)
-
-	assert.Equal(t, 1, len(finalInvokers))
-}
-
-func TestRouterChainRouteAppRouter(t *testing.T) {
-	ts, z, _, err := zookeeper.NewMockZookeeperClient("test", 15*time.Second)
-	assert.NoError(t, err)
-	err = z.Create(path)
-	assert.NoError(t, err)
-
-	testyml := `scope: application
-key: mock-app
-enabled: true
-force: true
-runtime: false
-conditions:
-  - => host = 1.1.1.1 => host != 1.2.3.4
-`
-
-	_, err = z.Conn.Set(path, []byte(testyml), 0)
-	assert.NoError(t, err)
-	defer func() {
-		_ = ts.Stop()
-		assert.NoError(t, err)
-		z.Close()
-	}()
-
-	zkUrl, _ := common.NewURL(fmt.Sprintf(zkFormat, localIP, ts.Servers[0].Port))
-	configuration, err := extension.GetConfigCenterFactory(zk).GetDynamicConfiguration(zkUrl)
-	assert.NoError(t, err)
-	config.GetEnvInstance().SetDynamicConfiguration(configuration)
-
-	chain, err := NewRouterChain(getConditionRouteUrl(applicationKey))
-	assert.Nil(t, err)
-	assert.Equal(t, 2, len(chain.routers))
-
-	var invokers []protocol.Invoker
-	dubboURL, _ := common.NewURL(fmt.Sprintf(dubboForamt, test1234IP, port20000))
-	invokers = append(invokers, protocol.NewBaseInvoker(dubboURL))
-	chain.SetInvokers(invokers)
-	chain.buildCache()
-
-	targetURL, _ := common.NewURL(fmt.Sprintf(consumerFormat, test1111IP))
-	inv := &invocation.RPCInvocation{}
-	finalInvokers := chain.Route(targetURL, inv)
-
-	assert.Equal(t, 0, len(finalInvokers))
-}
-
-func TestRouterChainRouteNoRoute(t *testing.T) {
-	ts, z, _, err := zookeeper.NewMockZookeeperClient("test", 15*time.Second)
-	assert.Nil(t, err)
-	defer func() {
-		_ = ts.Stop()
-		assert.NoError(t, err)
-		z.Close()
-	}()
-
-	zkUrl, _ := common.NewURL(fmt.Sprintf(zkFormat, localIP, ts.Servers[0].Port))
-	configuration, err := extension.GetConfigCenterFactory(zk).GetDynamicConfiguration(zkUrl)
-	config.GetEnvInstance().SetDynamicConfiguration(configuration)
-	assert.Nil(t, err)
-
-	chain, err := NewRouterChain(getConditionNoRouteUrl(applicationKey))
-	assert.Nil(t, err)
-	assert.Equal(t, 1, len(chain.routers))
-
-	url := getConditionRouteUrl(applicationKey)
-	assert.NotNil(t, url)
-
-	var invokers []protocol.Invoker
-	dubboURL, _ := common.NewURL(fmt.Sprintf(dubboForamt, test1234IP, port20000))
-	invokers = append(invokers, protocol.NewBaseInvoker(dubboURL))
-	chain.SetInvokers(invokers)
-	chain.buildCache()
-
-	targetURL, _ := common.NewURL(fmt.Sprintf(consumerFormat, test1111IP))
-	inv := &invocation.RPCInvocation{}
-	finalInvokers := chain.Route(targetURL, inv)
-
-	assert.Equal(t, 0, len(finalInvokers))
-}
-
-func getConditionNoRouteUrl(applicationKey string) *common.URL {
-	url, _ := common.NewURL(fmt.Sprintf(anyUrlFormat, test0000IP))
-	url.AddParam(applicationField, applicationKey)
-	url.AddParam(forceField, forceValue)
-	rule := base64.URLEncoding.EncodeToString([]byte("host = 1.1.1.1 => host != 1.2.3.4"))
-	url.AddParam(constant.RULE_KEY, rule)
-	return url
-}
-
-func getConditionRouteUrl(applicationKey string) *common.URL {
-	url, _ := common.NewURL(fmt.Sprintf(anyUrlFormat, test0000IP))
-	url.AddParam(applicationField, applicationKey)
-	url.AddParam(forceField, forceValue)
-	rule := base64.URLEncoding.EncodeToString([]byte("host = 1.1.1.1 => host = 1.2.3.4"))
-	url.AddParam(constant.RULE_KEY, rule)
-	return url
-}
-
-func getRouteUrl(applicationKey string) *common.URL {
-	url, _ := common.NewURL(fmt.Sprintf(anyUrlFormat, test0000IP))
-	url.AddParam(applicationField, applicationKey)
-	url.AddParam(forceField, forceValue)
-	return url
-}
-- 
GitLab