dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/loadbalance/random/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 random 19 20 import ( 21 "fmt" 22 "net/url" 23 "strconv" 24 "testing" 25 "time" 26 ) 27 28 import ( 29 "github.com/stretchr/testify/assert" 30 ) 31 32 import ( 33 "dubbo.apache.org/dubbo-go/v3/common" 34 "dubbo.apache.org/dubbo-go/v3/common/constant" 35 "dubbo.apache.org/dubbo-go/v3/protocol" 36 "dubbo.apache.org/dubbo-go/v3/protocol/invocation" 37 ) 38 39 const ( 40 tmpUrl = "dubbo://192.168.1.100:20000/com.ikurento.user.UserProvider" 41 tmpUrlFormat = "dubbo://192.168.1.%v:20000/com.ikurento.user.UserProvider" 42 tmpIp = "192.168.1.100" 43 ) 44 45 func TestRandomlbSelect(t *testing.T) { 46 randomlb := NewRandomLoadBalance() 47 48 var invokers []protocol.Invoker 49 50 u, _ := common.NewURL(fmt.Sprintf(tmpUrlFormat, 0)) 51 invokers = append(invokers, protocol.NewBaseInvoker(u)) 52 i := randomlb.Select(invokers, &invocation.RPCInvocation{}) 53 assert.True(t, i.GetURL().URLEqual(u)) 54 55 for i := 1; i < 10; i++ { 56 u, _ := common.NewURL(fmt.Sprintf(tmpUrlFormat, i)) 57 invokers = append(invokers, protocol.NewBaseInvoker(u)) 58 } 59 randomlb.Select(invokers, &invocation.RPCInvocation{}) 60 } 61 62 func TestRandomlbSelectWeight(t *testing.T) { 63 randomlb := NewRandomLoadBalance() 64 65 invokers := []protocol.Invoker{} 66 for i := 0; i < 10; i++ { 67 u, _ := common.NewURL(fmt.Sprintf(tmpUrlFormat, i)) 68 invokers = append(invokers, protocol.NewBaseInvoker(u)) 69 } 70 71 urlParams := url.Values{} 72 urlParams.Set("methods.test."+constant.WeightKey, "10000000000000") 73 urll, _ := common.NewURL(tmpUrl, common.WithParams(urlParams)) 74 invokers = append(invokers, protocol.NewBaseInvoker(urll)) 75 ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test")) 76 77 var selectedInvoker []protocol.Invoker 78 var selected float64 79 for i := 0; i < 10000; i++ { 80 s := randomlb.Select(invokers, ivc) 81 if s.GetURL().Ip == tmpIp { 82 selected++ 83 } 84 selectedInvoker = append(selectedInvoker, s) 85 } 86 assert.Equal(t, 10000, len(selectedInvoker)) 87 88 assert.Condition(t, func() bool { 89 // really is 0.9999999999999 90 return selected/10000 > 0.9 91 }) 92 } 93 94 func TestRandomlbSelectWarmup(t *testing.T) { 95 randomlb := NewRandomLoadBalance() 96 97 invokers := []protocol.Invoker{} 98 for i := 0; i < 10; i++ { 99 u, _ := common.NewURL(fmt.Sprintf(tmpUrlFormat, i)) 100 invokers = append(invokers, protocol.NewBaseInvoker(u)) 101 } 102 103 urlParams := url.Values{} 104 urlParams.Set(constant.RemoteTimestampKey, strconv.FormatInt(time.Now().Add(time.Minute*(-9)).Unix(), 10)) 105 urll, _ := common.NewURL(tmpUrl, common.WithParams(urlParams)) 106 invokers = append(invokers, protocol.NewBaseInvoker(urll)) 107 ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test")) 108 109 var selectedInvoker []protocol.Invoker 110 var selected float64 111 for i := 0; i < 10000; i++ { 112 s := randomlb.Select(invokers, ivc) 113 if s.GetURL().Ip == tmpIp { 114 selected++ 115 } 116 selectedInvoker = append(selectedInvoker, s) 117 } 118 assert.Equal(t, 10000, len(selectedInvoker)) 119 120 assert.Condition(t, func() bool { 121 return selected/10000 < 0.1 122 }) 123 }