dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/loadbalance/consistenthashing/loadbalance_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 consistenthashing
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  )
    24  
    25  import (
    26  	"github.com/stretchr/testify/suite"
    27  )
    28  
    29  import (
    30  	"dubbo.apache.org/dubbo-go/v3/cluster/loadbalance"
    31  	"dubbo.apache.org/dubbo-go/v3/common"
    32  	"dubbo.apache.org/dubbo-go/v3/protocol"
    33  	"dubbo.apache.org/dubbo-go/v3/protocol/invocation"
    34  )
    35  
    36  const (
    37  	ip       = "192.168.1.0"
    38  	port8080 = 8080
    39  	port8081 = 8081
    40  
    41  	url8080Short = "dubbo://192.168.1.0:8080"
    42  	url8081Short = "dubbo://192.168.1.0:8081"
    43  	url20000     = "dubbo://192.168.1.0:20000/org.apache.demo.HelloService?methods.echo.hash.arguments=0,1"
    44  	url8080      = "dubbo://192.168.1.0:8080/org.apache.demo.HelloService?methods.echo.hash.arguments=0,1"
    45  	url8081      = "dubbo://192.168.1.0:8081/org.apache.demo.HelloService?methods.echo.hash.arguments=0,1"
    46  	url8082      = "dubbo://192.168.1.0:8082/org.apache.demo.HelloService?methods.echo.hash.arguments=0,1"
    47  )
    48  
    49  func TestConsistentHashSelectorSuite(t *testing.T) {
    50  	suite.Run(t, new(consistentHashSelectorSuite))
    51  }
    52  
    53  type consistentHashSelectorSuite struct {
    54  	suite.Suite
    55  	selector *selector
    56  }
    57  
    58  func (s *consistentHashSelectorSuite) SetupTest() {
    59  	var invokers []protocol.Invoker
    60  	url, _ := common.NewURL(url20000)
    61  	invokers = append(invokers, protocol.NewBaseInvoker(url))
    62  	s.selector = newSelector(invokers, "echo", 999944)
    63  }
    64  
    65  func (s *consistentHashSelectorSuite) TestToKey() {
    66  	result := s.selector.toKey([]interface{}{"username", "age"})
    67  	s.Equal(result, "usernameage")
    68  }
    69  
    70  func (s *consistentHashSelectorSuite) TestSelectForKey() {
    71  	url1, _ := common.NewURL(url8080Short)
    72  	url2, _ := common.NewURL(url8081Short)
    73  	s.selector.virtualInvokers = make(map[uint32]protocol.Invoker)
    74  	s.selector.virtualInvokers[99874] = protocol.NewBaseInvoker(url1)
    75  	s.selector.virtualInvokers[9999945] = protocol.NewBaseInvoker(url2)
    76  	s.selector.keys = []uint32{99874, 9999945}
    77  	result := s.selector.selectForKey(9999944)
    78  	s.Equal(result.GetURL().String(), url8081Short+"?")
    79  }
    80  
    81  func TestConsistentHashLoadBalanceSuite(t *testing.T) {
    82  	suite.Run(t, new(consistentHashLoadBalanceSuite))
    83  }
    84  
    85  type consistentHashLoadBalanceSuite struct {
    86  	suite.Suite
    87  	url1     *common.URL
    88  	url2     *common.URL
    89  	url3     *common.URL
    90  	invokers []protocol.Invoker
    91  	invoker1 protocol.Invoker
    92  	invoker2 protocol.Invoker
    93  	invoker3 protocol.Invoker
    94  	lb       loadbalance.LoadBalance
    95  }
    96  
    97  func (s *consistentHashLoadBalanceSuite) SetupTest() {
    98  	var err error
    99  	s.url1, err = common.NewURL(url8080)
   100  	s.NoError(err)
   101  	s.url2, err = common.NewURL(url8081)
   102  	s.NoError(err)
   103  	s.url3, err = common.NewURL(url8082)
   104  	s.NoError(err)
   105  
   106  	s.invoker1 = protocol.NewBaseInvoker(s.url1)
   107  	s.invoker2 = protocol.NewBaseInvoker(s.url2)
   108  	s.invoker3 = protocol.NewBaseInvoker(s.url3)
   109  
   110  	s.invokers = append(s.invokers, s.invoker1, s.invoker2, s.invoker3)
   111  	s.lb = newConshashLoadBalance()
   112  }
   113  
   114  func (s *consistentHashLoadBalanceSuite) TestSelect() {
   115  	args := []interface{}{"name", "password", "age"}
   116  	invoker := s.lb.Select(s.invokers, invocation.NewRPCInvocation("echo", args, nil))
   117  	s.Equal(fmt.Sprintf("%s:%d", ip, port8081), invoker.GetURL().Location)
   118  
   119  	args = []interface{}{"ok", "abc"}
   120  	invoker = s.lb.Select(s.invokers, invocation.NewRPCInvocation("echo", args, nil))
   121  	s.Equal(fmt.Sprintf("%s:%d", ip, port8080), invoker.GetURL().Location)
   122  }