@@ -33,62 +33,67 @@ func TestRouteWithOneFactor(t *testing.T) {
3333 {
3434 scores : []int {10 },
3535 idxRange : []int {0 },
36- policy : config .RoutePolicyRandom ,
36+ policy : config .RoutingPolicyRandom ,
3737 },
3838 {
3939 scores : []int {10 , 20 },
4040 idxRange : []int {0 , 1 },
41- policy : config .RoutePolicyRandom ,
41+ policy : config .RoutingPolicyRandom ,
4242 },
4343 {
4444 scores : []int {10 , 20 , 30 },
4545 idxRange : []int {0 , 1 , 2 },
46- policy : config .RoutePolicyRandom ,
46+ policy : config .RoutingPolicyRandom ,
4747 },
4848 {
4949 scores : []int {30 , 20 , 10 },
5050 idxRange : []int {0 , 1 , 2 },
51- policy : config .RoutePolicyRandom ,
51+ policy : config .RoutingPolicyRandom ,
5252 },
5353 {
5454 scores : []int {30 , 11 , 10 },
5555 idxRange : []int {0 , 1 , 2 },
56- policy : config .RoutePolicyRandom ,
56+ policy : config .RoutingPolicyRandom ,
5757 },
5858 {
5959 scores : []int {11 , 11 , 10 },
6060 idxRange : []int {0 , 1 , 2 },
61- policy : config .RoutePolicyRandom ,
61+ policy : config .RoutingPolicyRandom ,
6262 },
6363 {
6464 scores : []int {10 },
6565 idxRange : []int {0 },
66- policy : config .RoutePolicyPreferIdle ,
66+ policy : config .RoutingPolicyPreferIdle ,
6767 },
6868 {
6969 scores : []int {10 , 20 },
7070 idxRange : []int {0 },
71- policy : config .RoutePolicyPreferIdle ,
71+ policy : config .RoutingPolicyPreferIdle ,
7272 },
7373 {
7474 scores : []int {10 , 20 , 30 },
7575 idxRange : []int {0 },
76- policy : config .RoutePolicyPreferIdle ,
76+ policy : config .RoutingPolicyPreferIdle ,
7777 },
7878 {
7979 scores : []int {30 , 20 , 10 },
8080 idxRange : []int {2 },
81- policy : config .RoutePolicyPreferIdle ,
81+ policy : config .RoutingPolicyPreferIdle ,
8282 },
8383 {
8484 scores : []int {30 , 11 , 10 },
8585 idxRange : []int {1 , 2 },
86- policy : config .RoutePolicyPreferIdle ,
86+ policy : config .RoutingPolicyPreferIdle ,
8787 },
8888 {
8989 scores : []int {11 , 11 , 10 },
9090 idxRange : []int {0 , 1 , 2 },
91- policy : config .RoutePolicyPreferIdle ,
91+ policy : config .RoutingPolicyPreferIdle ,
92+ },
93+ {
94+ scores : []int {10 , 10 , 9 },
95+ idxRange : []int {2 },
96+ policy : config .RoutingPolicyIdlest ,
9297 },
9398 {
9499 scores : []int {10 , 20 },
@@ -114,10 +119,10 @@ func TestRouteWithOneFactor(t *testing.T) {
114119 }
115120}
116121
117- func TestRouteIdlestWith2Factors (t * testing.T ) {
122+ func TestRoutePreferIdleWith2Factors (t * testing.T ) {
118123 lg , _ := logger .CreateLoggerForTest (t )
119124 fm := NewFactorBasedBalance (lg , newMockMetricsReader ())
120- fm .routePolicy = config .RoutePolicyPreferIdle
125+ fm .routePolicy = config .RoutingPolicyPreferIdle
121126 factor1 := & mockFactor {bitNum : 4 , balanceCount : 1 , threshold : 1 , canBeRouted : true }
122127 factor2 := & mockFactor {bitNum : 12 , balanceCount : 1 , threshold : 1 , canBeRouted : true }
123128 fm .factors = []Factor {factor1 , factor2 }
@@ -189,7 +194,7 @@ func TestRouteIdlestWith2Factors(t *testing.T) {
189194func TestRouteRandomWith2Factors (t * testing.T ) {
190195 lg , _ := logger .CreateLoggerForTest (t )
191196 fm := NewFactorBasedBalance (lg , newMockMetricsReader ())
192- fm .routePolicy = config .RoutePolicyRandom
197+ fm .routePolicy = config .RoutingPolicyRandom
193198 factor1 := & mockFactor {bitNum : 4 , balanceCount : 1 , threshold : 1 , canBeRouted : false }
194199 factor2 := & mockFactor {bitNum : 12 , balanceCount : 1 , threshold : 1 , canBeRouted : true }
195200 fm .factors = []Factor {factor1 , factor2 }
@@ -253,6 +258,58 @@ func TestRouteRandomWith2Factors(t *testing.T) {
253258 }
254259}
255260
261+ func TestRouteIdlestWith2Factors (t * testing.T ) {
262+ lg , _ := logger .CreateLoggerForTest (t )
263+ fm := NewFactorBasedBalance (lg , newMockMetricsReader ())
264+ fm .routePolicy = config .RoutingPolicyIdlest
265+ factor1 := & mockFactor {bitNum : 4 , balanceCount : 1 , threshold : 1 , canBeRouted : true }
266+ factor2 := & mockFactor {bitNum : 12 , balanceCount : 1 , threshold : 1 , canBeRouted : true }
267+ fm .factors = []Factor {factor1 , factor2 }
268+ require .NoError (t , fm .updateBitNum ())
269+ tests := []struct {
270+ scores1 []int
271+ scores2 []int
272+ idxRange []int
273+ }{
274+ {
275+ scores1 : []int {10 , 0 , 0 },
276+ scores2 : []int {0 , 100 , 200 },
277+ idxRange : []int {1 },
278+ },
279+ {
280+ scores1 : []int {10 , 10 , 0 },
281+ scores2 : []int {0 , 100 , 200 },
282+ idxRange : []int {2 },
283+ },
284+ {
285+ scores1 : []int {10 , 10 , 10 },
286+ scores2 : []int {0 , 100 , 200 },
287+ idxRange : []int {0 },
288+ },
289+ }
290+ for tIdx , test := range tests {
291+ factor1 .updateScore = func (backends []scoredBackend ) {
292+ for i := range backends {
293+ backends [i ].addScore (test .scores1 [i ], factor1 .bitNum )
294+ }
295+ }
296+ factor2 .updateScore = func (backends []scoredBackend ) {
297+ for i := range backends {
298+ backends [i ].addScore (test .scores2 [i ], factor2 .bitNum )
299+ }
300+ }
301+ backends := createBackends (len (test .scores1 ))
302+ targets := make (map [int ]struct {}, len (test .scores1 ))
303+ for i := 0 ; len (targets ) < len (test .idxRange ) || i < 100 ; i ++ {
304+ require .Less (t , i , 100000 , "test index %d" , tIdx )
305+ backend := fm .BackendToRoute (backends )
306+ idx := slices .Index (backends , backend )
307+ require .Contains (t , test .idxRange , idx , "test index %d" , tIdx )
308+ targets [idx ] = struct {}{}
309+ }
310+ }
311+ }
312+
256313func TestBalanceWithOneFactor (t * testing.T ) {
257314 lg , _ := logger .CreateLoggerForTest (t )
258315 fm := NewFactorBasedBalance (lg , newMockMetricsReader ())
@@ -639,11 +696,11 @@ func TestSetConfigsConcurrently(t *testing.T) {
639696 wg .Run (func () {
640697 defer wg .Done ()
641698 policies := []string {config .BalancePolicyConnection , config .BalancePolicyResource , config .BalancePolicyLocation }
642- routePolicies := []string {config .RoutePolicyRandom , config .RoutePolicyPreferIdle }
699+ routePolicies := []string {config .RoutingPolicyRandom , config .RoutingPolicyPreferIdle , config . RoutingPolicyIdlest }
643700 for i := 0 ; ctx .Err () != nil ; i ++ {
644701 cfg .Balance = config.Balance {
645- Policy : policies [i % len (policies )],
646- RoutePolicy : routePolicies [i % len (routePolicies )],
702+ Policy : policies [i % len (policies )],
703+ RoutingPolicy : routePolicies [i % len (routePolicies )],
647704 }
648705 fbb .SetConfig (cfg )
649706 }
0 commit comments