dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/balancer/ringhash/ring_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  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   *
    20   * Copyright 2021 gRPC authors.
    21   *
    22   */
    23  
    24  package ringhash
    25  
    26  import (
    27  	"fmt"
    28  	"math"
    29  	"testing"
    30  )
    31  
    32  import (
    33  	xxhash "github.com/cespare/xxhash/v2"
    34  
    35  	"google.golang.org/grpc/resolver"
    36  )
    37  
    38  func testAddr(addr string, weight uint32) resolver.Address {
    39  	return resolver.Address{Addr: addr, Metadata: weight}
    40  }
    41  
    42  func TestRingNew(t *testing.T) {
    43  	testAddrs := []resolver.Address{
    44  		testAddr("a", 3),
    45  		testAddr("b", 3),
    46  		testAddr("c", 4),
    47  	}
    48  	var totalWeight float64 = 10
    49  	testSubConnMap := map[resolver.Address]*subConn{
    50  		testAddr("a", 3): {addr: "a"},
    51  		testAddr("b", 3): {addr: "b"},
    52  		testAddr("c", 4): {addr: "c"},
    53  	}
    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(testSubConnMap, min, max)
    58  				totalCount := len(r.items)
    59  				if totalCount < int(min) || totalCount > int(max) {
    60  					t.Fatalf("unexpect size %v, want min %v, max %v", totalCount, min, max)
    61  				}
    62  				for _, a := range testAddrs {
    63  					var count int
    64  					for _, ii := range r.items {
    65  						if ii.sc.addr == a.Addr {
    66  							count++
    67  						}
    68  					}
    69  					got := float64(count) / float64(totalCount)
    70  					want := float64(a.Metadata.(uint32)) / 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 TestRingPick(t *testing.T) {
    87  	r, _ := newRing(map[resolver.Address]*subConn{
    88  		{Addr: "a", Metadata: uint32(3)}: {addr: "a"},
    89  		{Addr: "b", Metadata: uint32(3)}: {addr: "b"},
    90  		{Addr: "c", Metadata: uint32(4)}: {addr: "c"},
    91  	}, 10, 20)
    92  	for _, h := range []uint64{xxhash.Sum64String("1"), xxhash.Sum64String("2"), xxhash.Sum64String("3"), xxhash.Sum64String("4")} {
    93  		t.Run(fmt.Sprintf("picking-hash-%v", h), func(t *testing.T) {
    94  			e := r.pick(h)
    95  			var low uint64
    96  			if e.idx > 0 {
    97  				low = r.items[e.idx-1].hash
    98  			}
    99  			high := e.hash
   100  			// h should be in [low, high).
   101  			if h < low || h >= high {
   102  				t.Fatalf("unexpected item picked, hash: %v, low: %v, high: %v", h, low, high)
   103  			}
   104  		})
   105  	}
   106  }
   107  
   108  func TestRingNext(t *testing.T) {
   109  	r, _ := newRing(map[resolver.Address]*subConn{
   110  		{Addr: "a", Metadata: uint32(3)}: {addr: "a"},
   111  		{Addr: "b", Metadata: uint32(3)}: {addr: "b"},
   112  		{Addr: "c", Metadata: uint32(4)}: {addr: "c"},
   113  	}, 10, 20)
   114  
   115  	for _, e := range r.items {
   116  		ne := r.next(e)
   117  		if ne.idx != (e.idx+1)%len(r.items) {
   118  			t.Fatalf("next(%+v) returned unexpected %+v", e, ne)
   119  		}
   120  	}
   121  }