@@ -102,8 +102,44 @@ func (s) TestRingNewWeightSumOverflow(t *testing.T) {
102102 }
103103 }
104104 got := float64 (count ) / float64 (len (r .items ))
105- if ! equalApproximately (got , 0.5 ) {
106- t .Fatalf ("endpoint %q occupies %v of the ring, want ~0.5" , hashKey (e ), got )
105+ if got != 0.5 {
106+ t .Fatalf ("endpoint %q occupies %v of the ring, want 0.5" , hashKey (e ), got )
107+ }
108+ }
109+ }
110+
111+ // TestRingNewWeightSumOverflowToNonZero checks that endpoint weights whose sum
112+ // exceeds math.MaxUint32 and wraps to a non-zero value still produce a ring
113+ // within the configured size bounds. A wrapped non-zero sum used to make
114+ // normalizeWeights return weights greater than 1, growing the ring past
115+ // maxRingSize.
116+ func (s ) TestRingNewWeightSumOverflowToNonZero (t * testing.T ) {
117+ endpoints := []resolver.Endpoint {
118+ testEndpoint ("a" , 1 << 31 ),
119+ testEndpoint ("b" , 1 << 31 ),
120+ testEndpoint ("c" , 1 << 30 ), // sum is 2^32 + 2^30, wraps a uint32 to 2^30.
121+ }
122+ m := resolver .NewEndpointMap [* endpointState ]()
123+ m .Set (endpoints [0 ], & endpointState {hashKey : "a" , weight : 1 << 31 })
124+ m .Set (endpoints [1 ], & endpointState {hashKey : "b" , weight : 1 << 31 })
125+ m .Set (endpoints [2 ], & endpointState {hashKey : "c" , weight : 1 << 30 })
126+
127+ const min , max uint64 = 1024 , 4096
128+ r := newRing (m , min , max , nil )
129+ if got := uint64 (len (r .items )); got < min || got > max {
130+ t .Fatalf ("newRing built a ring of size %d, want within [%d, %d]" , got , min , max )
131+ }
132+ wantFractions := map [string ]float64 {"a" : 0.4 , "b" : 0.4 , "c" : 0.2 }
133+ for _ , e := range endpoints {
134+ var count int
135+ for _ , ii := range r .items {
136+ if ii .hashKey == hashKey (e ) {
137+ count ++
138+ }
139+ }
140+ got := float64 (count ) / float64 (len (r .items ))
141+ if want := wantFractions [hashKey (e )]; ! equalApproximately (got , want ) {
142+ t .Fatalf ("endpoint %q occupies %v of the ring, want ~%v" , hashKey (e ), got , want )
107143 }
108144 }
109145}
0 commit comments