github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/balancer/ringhash/ring_test.go (about) 1 /* 2 * 3 * Copyright 2021 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 ringhash 20 21 import ( 22 "fmt" 23 "math" 24 "testing" 25 26 xxhash "github.com/cespare/xxhash/v2" 27 "github.com/hxx258456/ccgo/grpc/resolver" 28 ) 29 30 func testAddr(addr string, weight uint32) resolver.Address { 31 return resolver.Address{Addr: addr, Metadata: weight} 32 } 33 34 func TestRingNew(t *testing.T) { 35 testAddrs := []resolver.Address{ 36 testAddr("a", 3), 37 testAddr("b", 3), 38 testAddr("c", 4), 39 } 40 var totalWeight float64 = 10 41 testSubConnMap := map[resolver.Address]*subConn{ 42 testAddr("a", 3): {addr: "a"}, 43 testAddr("b", 3): {addr: "b"}, 44 testAddr("c", 4): {addr: "c"}, 45 } 46 for _, min := range []uint64{3, 4, 6, 8} { 47 for _, max := range []uint64{20, 8} { 48 t.Run(fmt.Sprintf("size-min-%v-max-%v", min, max), func(t *testing.T) { 49 r, _ := newRing(testSubConnMap, min, max) 50 totalCount := len(r.items) 51 if totalCount < int(min) || totalCount > int(max) { 52 t.Fatalf("unexpect size %v, want min %v, max %v", totalCount, min, max) 53 } 54 for _, a := range testAddrs { 55 var count int 56 for _, ii := range r.items { 57 if ii.sc.addr == a.Addr { 58 count++ 59 } 60 } 61 got := float64(count) / float64(totalCount) 62 want := float64(a.Metadata.(uint32)) / totalWeight 63 if !equalApproximately(got, want) { 64 t.Fatalf("unexpected item weight in ring: %v != %v", got, want) 65 } 66 } 67 }) 68 } 69 } 70 } 71 72 func equalApproximately(x, y float64) bool { 73 delta := math.Abs(x - y) 74 mean := math.Abs(x+y) / 2.0 75 return delta/mean < 0.25 76 } 77 78 func TestRingPick(t *testing.T) { 79 r, _ := newRing(map[resolver.Address]*subConn{ 80 {Addr: "a", Metadata: uint32(3)}: {addr: "a"}, 81 {Addr: "b", Metadata: uint32(3)}: {addr: "b"}, 82 {Addr: "c", Metadata: uint32(4)}: {addr: "c"}, 83 }, 10, 20) 84 for _, h := range []uint64{xxhash.Sum64String("1"), xxhash.Sum64String("2"), xxhash.Sum64String("3"), xxhash.Sum64String("4")} { 85 t.Run(fmt.Sprintf("picking-hash-%v", h), func(t *testing.T) { 86 e := r.pick(h) 87 var low uint64 88 if e.idx > 0 { 89 low = r.items[e.idx-1].hash 90 } 91 high := e.hash 92 // h should be in [low, high). 93 if h < low || h >= high { 94 t.Fatalf("unexpected item picked, hash: %v, low: %v, high: %v", h, low, high) 95 } 96 }) 97 } 98 } 99 100 func TestRingNext(t *testing.T) { 101 r, _ := newRing(map[resolver.Address]*subConn{ 102 {Addr: "a", Metadata: uint32(3)}: {addr: "a"}, 103 {Addr: "b", Metadata: uint32(3)}: {addr: "b"}, 104 {Addr: "c", Metadata: uint32(4)}: {addr: "c"}, 105 }, 10, 20) 106 107 for _, e := range r.items { 108 ne := r.next(e) 109 if ne.idx != (e.idx+1)%len(r.items) { 110 t.Fatalf("next(%+v) returned unexpected %+v", e, ne) 111 } 112 } 113 }