github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/balance/consistenthash_test.go (about)

     1  package balance
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/stretchr/testify/assert"
     6  	"strconv"
     7  	"testing"
     8  )
     9  
    10  var nodes []string = []string{
    11  	"192.168.144.210:6666",
    12  	"192.168.48.103:6666",
    13  	"192.168.48.104:6666",
    14  	"192.168.48.105:6666",
    15  	"192.168.48.106:6666",
    16  	"192.168.48.108:6666",
    17  	"192.168.48.110:6666",
    18  	"192.168.50.85:6666",
    19  	"192.168.50.86:6666",
    20  	"192.168.144.210:6667",
    21  	"192.168.48.103:6667",
    22  	"192.168.48.104:6667",
    23  	"192.168.48.105:6667",
    24  	"192.168.48.106:6667",
    25  	"192.168.48.108:6667",
    26  	"192.168.48.110:6667",
    27  	"192.168.50.85:6667",
    28  	"192.168.50.86:6667",
    29  }
    30  
    31  func TestAddNodes(t *testing.T) {
    32  	hashRing := NewHashRing()
    33  	assert.True(t, hashRing.IsEmpty())
    34  	hashRing.AddNodes(nodes...)
    35  	hashRing.AddNodes(nodes...)
    36  	assert.False(t, hashRing.IsEmpty())
    37  }
    38  
    39  func TestGetNode(t *testing.T) {
    40  	hashRing := NewHashRing()
    41  	hashRing.AddNodes(nodes...)
    42  	ipMap := make(map[string]int, 16)
    43  	for i := 0; i < 1000000; i++ {
    44  		key := strconv.Itoa(i) + "hasxchRingas" + strconv.Itoa(i) + "kdfsa"
    45  		hostPort := hashRing.GetNode(key)
    46  		ipMap[hostPort] += 1
    47  	}
    48  	for key, val := range ipMap {
    49  		fmt.Printf("key:%s,val:%d\n", key, val)
    50  	}
    51  }