dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/loadbalance/interleavedweightedroundrobin/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 iwrr 19 20 import ( 21 "fmt" 22 "testing" 23 24 "dubbo.apache.org/dubbo-go/v3/common" 25 "dubbo.apache.org/dubbo-go/v3/common/constant" 26 "dubbo.apache.org/dubbo-go/v3/protocol" 27 "dubbo.apache.org/dubbo-go/v3/protocol/invocation" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestIWrrRoundRobinSelect(t *testing.T) { 32 loadBalance := newInterleavedWeightedRoundRobinBalance() 33 34 var invokers []protocol.Invoker 35 36 url, _ := common.NewURL(fmt.Sprintf("dubbo://%s:%d/org.apache.demo.HelloService", 37 constant.LocalHostValue, constant.DefaultPort)) 38 invokers = append(invokers, protocol.NewBaseInvoker(url)) 39 i := loadBalance.Select(invokers, &invocation.RPCInvocation{}) 40 assert.True(t, i.GetURL().URLEqual(url)) 41 42 for i := 1; i < 10; i++ { 43 url, _ := common.NewURL(fmt.Sprintf("dubbo://192.168.1.%v:20000/org.apache.demo.HelloService", i)) 44 invokers = append(invokers, protocol.NewBaseInvoker(url)) 45 } 46 loadBalance.Select(invokers, &invocation.RPCInvocation{}) 47 } 48 49 func TestIWrrRoundRobinByWeight(t *testing.T) { 50 loadBalance := newInterleavedWeightedRoundRobinBalance() 51 52 var invokers []protocol.Invoker 53 loop := 10 54 for i := 1; i <= loop; i++ { 55 url, _ := common.NewURL(fmt.Sprintf("dubbo://192.168.1.%v:20000/org.apache.demo.HelloService?weight=%v", i, i)) 56 invokers = append(invokers, protocol.NewBaseInvoker(url)) 57 } 58 59 loop = (1 + loop) * loop / 2 60 selected := make(map[protocol.Invoker]int) 61 62 for i := 1; i <= loop; i++ { 63 invoker := loadBalance.Select(invokers, &invocation.RPCInvocation{}) 64 selected[invoker]++ 65 } 66 67 sum := 0 68 for _, value := range selected { 69 sum += value 70 } 71 72 assert.Equal(t, loop, sum) 73 }