dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/loadbalance/leastactive/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 leastactive 19 20 import ( 21 "fmt" 22 "testing" 23 ) 24 25 import ( 26 "github.com/stretchr/testify/assert" 27 ) 28 29 import ( 30 "dubbo.apache.org/dubbo-go/v3/common" 31 "dubbo.apache.org/dubbo-go/v3/common/constant" 32 "dubbo.apache.org/dubbo-go/v3/protocol" 33 "dubbo.apache.org/dubbo-go/v3/protocol/invocation" 34 ) 35 36 func TestLeastActiveSelect(t *testing.T) { 37 loadBalance := newLeastActiveLoadBalance() 38 39 var invokers []protocol.Invoker 40 41 url, _ := common.NewURL(fmt.Sprintf("dubbo://%s:%d/org.apache.demo.HelloService", constant.LocalHostValue, constant.DefaultPort)) 42 invokers = append(invokers, protocol.NewBaseInvoker(url)) 43 i := loadBalance.Select(invokers, &invocation.RPCInvocation{}) 44 assert.True(t, i.GetURL().URLEqual(url)) 45 46 for i := 1; i < 10; i++ { 47 url, _ := common.NewURL(fmt.Sprintf("dubbo://192.168.1.%v:20000/org.apache.demo.HelloService", i)) 48 invokers = append(invokers, protocol.NewBaseInvoker(url)) 49 } 50 loadBalance.Select(invokers, &invocation.RPCInvocation{}) 51 } 52 53 func TestLeastActiveByWeight(t *testing.T) { 54 loadBalance := newLeastActiveLoadBalance() 55 56 var invokers []protocol.Invoker 57 loop := 3 58 for i := 1; i <= loop; i++ { 59 url, _ := common.NewURL(fmt.Sprintf("test%v://192.168.1.%v:20000/org.apache.demo.HelloService?weight=%v", i, i, i)) 60 invokers = append(invokers, protocol.NewBaseInvoker(url)) 61 } 62 63 inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test")) 64 protocol.BeginCount(invokers[2].GetURL(), inv.MethodName()) 65 66 loop = 10000 67 68 var ( 69 firstCount int 70 secondCount int 71 ) 72 73 for i := 1; i <= loop; i++ { 74 invoker := loadBalance.Select(invokers, inv) 75 if invoker.GetURL().Protocol == "test1" { 76 firstCount++ 77 } else if invoker.GetURL().Protocol == "test2" { 78 secondCount++ 79 } 80 } 81 82 assert.Equal(t, firstCount+secondCount, loop) 83 }