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