gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/xds/internal/testutils/balancer_test.go (about) 1 /* 2 * 3 * Copyright 2020 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * 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 19 package testutils 20 21 import ( 22 "testing" 23 24 "gitee.com/ks-custle/core-gm/grpc/balancer" 25 "gitee.com/ks-custle/core-gm/grpc/internal/testutils" 26 ) 27 28 func TestIsRoundRobin(t *testing.T) { 29 var ( 30 sc1 = testutils.TestSubConns[0] 31 sc2 = testutils.TestSubConns[1] 32 sc3 = testutils.TestSubConns[2] 33 ) 34 35 testCases := []struct { 36 desc string 37 want []balancer.SubConn 38 got []balancer.SubConn 39 pass bool 40 }{ 41 { 42 desc: "0 element", 43 want: []balancer.SubConn{}, 44 got: []balancer.SubConn{}, 45 pass: true, 46 }, 47 { 48 desc: "1 element RR", 49 want: []balancer.SubConn{sc1}, 50 got: []balancer.SubConn{sc1, sc1, sc1, sc1}, 51 pass: true, 52 }, 53 { 54 desc: "1 element not RR", 55 want: []balancer.SubConn{sc1}, 56 got: []balancer.SubConn{sc1, sc2, sc1}, 57 pass: false, 58 }, 59 { 60 desc: "2 elements RR", 61 want: []balancer.SubConn{sc1, sc2}, 62 got: []balancer.SubConn{sc1, sc2, sc1, sc2, sc1, sc2}, 63 pass: true, 64 }, 65 { 66 desc: "2 elements RR different order from want", 67 want: []balancer.SubConn{sc2, sc1}, 68 got: []balancer.SubConn{sc1, sc2, sc1, sc2, sc1, sc2}, 69 pass: true, 70 }, 71 { 72 desc: "2 elements RR not RR, mistake in first iter", 73 want: []balancer.SubConn{sc1, sc2}, 74 got: []balancer.SubConn{sc1, sc1, sc1, sc2, sc1, sc2}, 75 pass: false, 76 }, 77 { 78 desc: "2 elements RR not RR, mistake in second iter", 79 want: []balancer.SubConn{sc1, sc2}, 80 got: []balancer.SubConn{sc1, sc2, sc1, sc1, sc1, sc2}, 81 pass: false, 82 }, 83 { 84 desc: "2 elements weighted RR", 85 want: []balancer.SubConn{sc1, sc1, sc2}, 86 got: []balancer.SubConn{sc1, sc1, sc2, sc1, sc1, sc2}, 87 pass: true, 88 }, 89 { 90 desc: "2 elements weighted RR different order", 91 want: []balancer.SubConn{sc1, sc1, sc2}, 92 got: []balancer.SubConn{sc1, sc2, sc1, sc1, sc2, sc1}, 93 pass: true, 94 }, 95 96 { 97 desc: "3 elements RR", 98 want: []balancer.SubConn{sc1, sc2, sc3}, 99 got: []balancer.SubConn{sc1, sc2, sc3, sc1, sc2, sc3, sc1, sc2, sc3}, 100 pass: true, 101 }, 102 { 103 desc: "3 elements RR different order", 104 want: []balancer.SubConn{sc1, sc2, sc3}, 105 got: []balancer.SubConn{sc3, sc2, sc1, sc3, sc2, sc1}, 106 pass: true, 107 }, 108 { 109 desc: "3 elements weighted RR", 110 want: []balancer.SubConn{sc1, sc1, sc1, sc2, sc2, sc3}, 111 got: []balancer.SubConn{sc1, sc2, sc3, sc1, sc2, sc1, sc1, sc2, sc3, sc1, sc2, sc1}, 112 pass: true, 113 }, 114 { 115 desc: "3 elements weighted RR not RR, mistake in first iter", 116 want: []balancer.SubConn{sc1, sc1, sc1, sc2, sc2, sc3}, 117 got: []balancer.SubConn{sc1, sc2, sc1, sc1, sc2, sc1, sc1, sc2, sc3, sc1, sc2, sc1}, 118 pass: false, 119 }, 120 { 121 desc: "3 elements weighted RR not RR, mistake in second iter", 122 want: []balancer.SubConn{sc1, sc1, sc1, sc2, sc2, sc3}, 123 got: []balancer.SubConn{sc1, sc2, sc3, sc1, sc2, sc1, sc1, sc1, sc3, sc1, sc2, sc1}, 124 pass: false, 125 }, 126 } 127 for _, tC := range testCases { 128 t.Run(tC.desc, func(t *testing.T) { 129 err := testutils.IsRoundRobin(tC.want, (&testClosure{r: tC.got}).next) 130 if err == nil != tC.pass { 131 t.Errorf("want pass %v, want %v, got err %v", tC.pass, tC.want, err) 132 } 133 }) 134 } 135 } 136 137 // testClosure is a test util for TestIsRoundRobin. 138 type testClosure struct { 139 r []balancer.SubConn 140 i int 141 } 142 143 func (tc *testClosure) next() balancer.SubConn { 144 ret := tc.r[tc.i] 145 tc.i = (tc.i + 1) % len(tc.r) 146 return ret 147 }