google.golang.org/grpc@v1.72.2/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 "google.golang.org/grpc/internal/balancer/weight" 27 "google.golang.org/grpc/resolver" 28 29 xxhash "github.com/cespare/xxhash/v2" 30 ) 31 32 var testEndpoints []resolver.Endpoint 33 var testEndpointStateMap *resolver.EndpointMap[*endpointState] 34 35 func init() { 36 testEndpoints = []resolver.Endpoint{ 37 testEndpoint("a", 3), 38 testEndpoint("b", 3), 39 testEndpoint("c", 4), 40 } 41 testEndpointStateMap = resolver.NewEndpointMap[*endpointState]() 42 testEndpointStateMap.Set(testEndpoints[0], &endpointState{hashKey: "a", weight: 3}) 43 testEndpointStateMap.Set(testEndpoints[1], &endpointState{hashKey: "b", weight: 3}) 44 testEndpointStateMap.Set(testEndpoints[2], &endpointState{hashKey: "c", weight: 4}) 45 } 46 47 func testEndpoint(addr string, endpointWeight uint32) resolver.Endpoint { 48 ep := resolver.Endpoint{Addresses: []resolver.Address{{Addr: addr}}} 49 return weight.Set(ep, weight.EndpointInfo{Weight: endpointWeight}) 50 } 51 52 func (s) TestRingNew(t *testing.T) { 53 var totalWeight float64 = 10 54 for _, min := range []uint64{3, 4, 6, 8} { 55 for _, max := range []uint64{20, 8} { 56 t.Run(fmt.Sprintf("size-min-%v-max-%v", min, max), func(t *testing.T) { 57 r := newRing(testEndpointStateMap, min, max, nil) 58 totalCount := len(r.items) 59 if totalCount < int(min) || totalCount > int(max) { 60 t.Fatalf("unexpected size %v, want min %v, max %v", totalCount, min, max) 61 } 62 for _, e := range testEndpoints { 63 var count int 64 for _, ii := range r.items { 65 if ii.hashKey == hashKey(e) { 66 count++ 67 } 68 } 69 got := float64(count) / float64(totalCount) 70 want := float64(getWeightAttribute(e)) / totalWeight 71 if !equalApproximately(got, want) { 72 t.Fatalf("unexpected item weight in ring: %v != %v", got, want) 73 } 74 } 75 }) 76 } 77 } 78 } 79 80 func equalApproximately(x, y float64) bool { 81 delta := math.Abs(x - y) 82 mean := math.Abs(x+y) / 2.0 83 return delta/mean < 0.25 84 } 85 86 func (s) TestRingPick(t *testing.T) { 87 r := newRing(testEndpointStateMap, 10, 20, nil) 88 for _, h := range []uint64{xxhash.Sum64String("1"), xxhash.Sum64String("2"), xxhash.Sum64String("3"), xxhash.Sum64String("4")} { 89 t.Run(fmt.Sprintf("picking-hash-%v", h), func(t *testing.T) { 90 e := r.pick(h) 91 var low uint64 92 if e.idx > 0 { 93 low = r.items[e.idx-1].hash 94 } 95 high := e.hash 96 // h should be in [low, high). 97 if h < low || h >= high { 98 t.Fatalf("unexpected item picked, hash: %v, low: %v, high: %v", h, low, high) 99 } 100 }) 101 } 102 } 103 104 func (s) TestRingNext(t *testing.T) { 105 r := newRing(testEndpointStateMap, 10, 20, nil) 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 }