dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/loadbalance/p2c/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 p2c 19 20 import ( 21 "math/rand" 22 "testing" 23 ) 24 25 import ( 26 "github.com/golang/mock/gomock" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 import ( 32 "dubbo.apache.org/dubbo-go/v3/cluster/metrics" 33 "dubbo.apache.org/dubbo-go/v3/common" 34 "dubbo.apache.org/dubbo-go/v3/protocol" 35 protoinvoc "dubbo.apache.org/dubbo-go/v3/protocol/invocation" 36 ) 37 38 func TestLoadBalance(t *testing.T) { 39 lb := newP2CLoadBalance() 40 invocation := protoinvoc.NewRPCInvocation("TestMethod", []interface{}{}, nil) 41 randSeed := func() int64 { 42 return 0 43 } 44 45 t.Run("no invokers", func(t *testing.T) { 46 rand.Seed(randSeed()) 47 ivk := lb.Select([]protocol.Invoker{}, invocation) 48 assert.Nil(t, ivk) 49 }) 50 51 t.Run("one invoker", func(t *testing.T) { 52 rand.Seed(randSeed()) 53 url0, _ := common.NewURL("dubbo://192.168.1.0:20000/com.ikurento.user.UserProvider") 54 55 ivkArr := []protocol.Invoker{ 56 protocol.NewBaseInvoker(url0), 57 } 58 ivk := lb.Select(ivkArr, invocation) 59 assert.Equal(t, ivkArr[0].GetURL().String(), ivk.GetURL().String()) 60 }) 61 62 t.Run("two invokers", func(t *testing.T) { 63 rand.Seed(randSeed()) 64 ctrl := gomock.NewController(t) 65 defer ctrl.Finish() 66 67 m := metrics.NewMockMetrics(ctrl) 68 metrics.LocalMetrics = m 69 70 url0, _ := common.NewURL("dubbo://192.168.1.0:20000/com.ikurento.user.UserProvider") 71 url1, _ := common.NewURL("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider") 72 73 m.EXPECT(). 74 GetMethodMetrics(gomock.Eq(url0), gomock.Eq(invocation.MethodName()), gomock.Eq(metrics.HillClimbing)). 75 Times(1). 76 Return(uint64(10), nil) 77 m.EXPECT(). 78 GetMethodMetrics(gomock.Eq(url1), gomock.Eq(invocation.MethodName()), gomock.Eq(metrics.HillClimbing)). 79 Times(1). 80 Return(uint64(5), nil) 81 82 ivkArr := []protocol.Invoker{ 83 protocol.NewBaseInvoker(url0), 84 protocol.NewBaseInvoker(url1), 85 } 86 87 ivk := lb.Select(ivkArr, invocation) 88 89 assert.Equal(t, ivkArr[0].GetURL().String(), ivk.GetURL().String()) 90 }) 91 92 t.Run("multiple invokers", func(t *testing.T) { 93 rand.Seed(randSeed()) 94 ctrl := gomock.NewController(t) 95 defer ctrl.Finish() 96 97 m := metrics.NewMockMetrics(ctrl) 98 metrics.LocalMetrics = m 99 100 url0, _ := common.NewURL("dubbo://192.168.1.0:20000/com.ikurento.user.UserProvider") 101 url1, _ := common.NewURL("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider") 102 url2, _ := common.NewURL("dubbo://192.168.1.2:20000/com.ikurento.user.UserProvider") 103 104 m.EXPECT(). 105 GetMethodMetrics(gomock.Eq(url0), gomock.Eq(invocation.MethodName()), gomock.Eq(metrics.HillClimbing)). 106 Times(1). 107 Return(uint64(10), nil) 108 m.EXPECT(). 109 GetMethodMetrics(gomock.Eq(url1), gomock.Eq(invocation.MethodName()), gomock.Eq(metrics.HillClimbing)). 110 Times(1). 111 Return(uint64(5), nil) 112 113 ivkArr := []protocol.Invoker{ 114 protocol.NewBaseInvoker(url0), 115 protocol.NewBaseInvoker(url1), 116 protocol.NewBaseInvoker(url2), 117 } 118 119 ivk := lb.Select(ivkArr, invocation) 120 121 assert.Equal(t, ivkArr[0].GetURL().String(), ivk.GetURL().String()) 122 }) 123 124 t.Run("metrics i not found", func(t *testing.T) { 125 rand.Seed(randSeed()) 126 ctrl := gomock.NewController(t) 127 defer ctrl.Finish() 128 129 m := metrics.NewMockMetrics(ctrl) 130 metrics.LocalMetrics = m 131 132 url0, _ := common.NewURL("dubbo://192.168.1.0:20000/com.ikurento.user.UserProvider") 133 url1, _ := common.NewURL("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider") 134 url2, _ := common.NewURL("dubbo://192.168.1.2:20000/com.ikurento.user.UserProvider") 135 136 m.EXPECT(). 137 GetMethodMetrics(gomock.Eq(url0), gomock.Eq(invocation.MethodName()), gomock.Eq(metrics.HillClimbing)). 138 Times(1). 139 Return(0, metrics.ErrMetricsNotFound) 140 141 ivkArr := []protocol.Invoker{ 142 protocol.NewBaseInvoker(url0), 143 protocol.NewBaseInvoker(url1), 144 protocol.NewBaseInvoker(url2), 145 } 146 147 ivk := lb.Select(ivkArr, invocation) 148 149 assert.Equal(t, ivkArr[0].GetURL().String(), ivk.GetURL().String()) 150 }) 151 152 t.Run("metrics j not found", func(t *testing.T) { 153 rand.Seed(randSeed()) 154 ctrl := gomock.NewController(t) 155 defer ctrl.Finish() 156 157 m := metrics.NewMockMetrics(ctrl) 158 metrics.LocalMetrics = m 159 160 url0, _ := common.NewURL("dubbo://192.168.1.0:20000/com.ikurento.user.UserProvider") 161 url1, _ := common.NewURL("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider") 162 url2, _ := common.NewURL("dubbo://192.168.1.2:20000/com.ikurento.user.UserProvider") 163 164 m.EXPECT(). 165 GetMethodMetrics(gomock.Eq(url0), gomock.Eq(invocation.MethodName()), gomock.Eq(metrics.HillClimbing)). 166 Times(1). 167 Return(uint64(0), nil) 168 169 m.EXPECT(). 170 GetMethodMetrics(gomock.Eq(url1), gomock.Eq(invocation.MethodName()), gomock.Eq(metrics.HillClimbing)). 171 Times(1). 172 Return(uint64(0), metrics.ErrMetricsNotFound) 173 174 ivkArr := []protocol.Invoker{ 175 protocol.NewBaseInvoker(url0), 176 protocol.NewBaseInvoker(url1), 177 protocol.NewBaseInvoker(url2), 178 } 179 180 ivk := lb.Select(ivkArr, invocation) 181 182 assert.Equal(t, ivkArr[1].GetURL().String(), ivk.GetURL().String()) 183 }) 184 185 }