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