dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/loadbalance/ringhash/ringhash_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 ringhash
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"testing"
    24  )
    25  
    26  import (
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/mock"
    29  )
    30  
    31  import (
    32  	"dubbo.apache.org/dubbo-go/v3/common"
    33  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    34  	"dubbo.apache.org/dubbo-go/v3/protocol"
    35  	"dubbo.apache.org/dubbo-go/v3/protocol/invocation"
    36  	"dubbo.apache.org/dubbo-go/v3/remoting/xds/mocks"
    37  	"dubbo.apache.org/dubbo-go/v3/xds/client/resource"
    38  )
    39  
    40  func TestSelect(t *testing.T) {
    41  	var invokers []protocol.Invoker
    42  	url, _ := common.NewURL(fmt.Sprintf("dubbo://%s:%d/org.apache.demo.HelloService",
    43  		"192.168.1.1", constant.DefaultPort))
    44  	url.SetParam(constant.InterfaceKey, "org.apache.demo.HelloService")
    45  	url.SetParam(constant.EndPointWeight, "3")
    46  	invokers = append(invokers, protocol.NewBaseInvoker(url))
    47  	url1, _ := common.NewURL(fmt.Sprintf("dubbo://%s:%d/org.apache.demo.HelloService",
    48  		"192.168.1.2", constant.DefaultPort))
    49  	url1.SetParam(constant.InterfaceKey, "org.apache.demo.HelloService")
    50  	url1.SetParam(constant.EndPointWeight, "3")
    51  	invokers = append(invokers, protocol.NewBaseInvoker(url1))
    52  	url2, _ := common.NewURL(fmt.Sprintf("dubbo://%s:%d/org.apache.demo.HelloService",
    53  		"192.168.1.3", constant.DefaultPort))
    54  	url2.SetParam(constant.InterfaceKey, "org.apache.demo.HelloService")
    55  	url2.SetParam(constant.EndPointWeight, "4")
    56  	invokers = append(invokers, protocol.NewBaseInvoker(url2))
    57  	t.Run("normal", func(t *testing.T) {
    58  		mockXDSClient := &mocks.WrappedClientMock{}
    59  		mockXDSClient.On("GetRouterConfig", mock.Anything).Return(resource.RouteConfigUpdate{})
    60  		mockXDSClient.On("GetClusterUpdateIgnoreVersion", mock.Anything).
    61  			Return(resource.ClusterUpdate{LBPolicy: &resource.ClusterLBPolicyRingHash{MinimumRingSize: 9, MaximumRingSize: 10}})
    62  		mockXDSClient.On("GetHostAddrByServiceUniqueKey", mock.Anything).Return("HelloService", nil)
    63  		mockXDSClient.On("MatchRoute", mock.Anything, mock.Anything).
    64  			Return(&resource.Route{HashPolicies: []*resource.HashPolicy{{HashPolicyType: resource.HashPolicyTypeHeader,
    65  				HeaderName: "key"}}}, nil)
    66  		lb := &ringhashLoadBalance{client: mockXDSClient}
    67  		inv := &invocation.RPCInvocation{}
    68  		inv.SetInvoker(protocol.NewBaseInvoker(url))
    69  		inv.SetAttachment("key", "1")
    70  		assert.NotNil(t, lb.Select(invokers, inv))
    71  	})
    72  
    73  	t.Run("normal_hash_channelID", func(t *testing.T) {
    74  		mockXDSClient := &mocks.WrappedClientMock{}
    75  		mockXDSClient.On("GetRouterConfig", mock.Anything).Return(resource.RouteConfigUpdate{})
    76  		mockXDSClient.On("GetClusterUpdateIgnoreVersion", mock.Anything).
    77  			Return(resource.ClusterUpdate{LBPolicy: &resource.ClusterLBPolicyRingHash{MinimumRingSize: 9, MaximumRingSize: 10}})
    78  		mockXDSClient.On("GetHostAddrByServiceUniqueKey", mock.Anything).Return("HelloService", nil)
    79  		mockXDSClient.On("MatchRoute", mock.Anything, mock.Anything).
    80  			Return(&resource.Route{HashPolicies: []*resource.HashPolicy{
    81  				{HashPolicyType: resource.HashPolicyTypeChannelID}}}, nil)
    82  		lb := &ringhashLoadBalance{client: mockXDSClient}
    83  		inv := &invocation.RPCInvocation{}
    84  		inv.SetInvoker(protocol.NewBaseInvoker(url))
    85  		assert.NotNil(t, lb.Select(invokers, inv))
    86  	})
    87  
    88  	t.Run("GetHostAddrByServiceUniqueKey_faild", func(t *testing.T) {
    89  		mockXDSClient := &mocks.WrappedClientMock{}
    90  		mockXDSClient.On("GetHostAddrByServiceUniqueKey", mock.Anything).
    91  			Return("HelloService", errors.New("error"))
    92  		lb := &ringhashLoadBalance{client: mockXDSClient}
    93  		inv := &invocation.RPCInvocation{}
    94  		inv.SetInvoker(protocol.NewBaseInvoker(url))
    95  		inv.SetAttachment("key", "1")
    96  		assert.Nil(t, lb.Select(invokers, inv))
    97  	})
    98  }