You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
naza/pkg/consistenthash/consistenthash_test.go

96 lines
2.0 KiB
Go

// Copyright 2019, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package consistenthash
import (
"hash/crc32"
"math"
"strconv"
"testing"
"github.com/q191201771/naza/pkg/assert"
"github.com/q191201771/naza/pkg/nazalog"
)
func TestConsistentHash(t *testing.T) {
ch := New(1024)
_, err := ch.Get("aaa")
assert.Equal(t, ErrIsEmpty, err)
ch.Add("127.0.0.1")
ch.Add("0.0.0.0", "8.8.8.8")
ch.Del("127.0.0.1", "8.8.8.8")
ch.Add("114.114.114.114", "255.255.255.255", "1.1.1.1", "2.2.2.2", "3.3.3.3")
exptectedNodes := []string{
"0.0.0.0",
"114.114.114.114",
"255.255.255.255",
"1.1.1.1",
"2.2.2.2",
"3.3.3.3",
}
actualNodes := ch.Nodes()
assert.Equal(t, len(exptectedNodes), len(actualNodes))
for _, en := range exptectedNodes {
_, ok := actualNodes[en]
assert.Equal(t, true, ok)
}
counts := make(map[string]int)
for i := 0; i < 16384; i++ {
node, err := ch.Get(strconv.Itoa(i))
assert.Equal(t, nil, err)
counts[node]++
}
nazalog.Debugf("%+v", counts)
}
func TestConsistentHash_Nodes(t *testing.T) {
nodesGolden := []string{
"0.0.0.0",
"114.114.114.114",
"255.255.255.255",
"1.1.1.1",
"2.2.2.2",
"3.3.3.3",
}
j := 1024 // 1
k := 1024 // 16384
for i := j; i <= k; i = i << 1 {
nazalog.Debugf("-----%d-----", i)
ch := New(i)
ch.Add(nodesGolden...)
nodes := ch.Nodes()
count := uint64(0)
for k, v := range nodes {
count += uint64(v)
nazalog.Debugf("%s: %+v", k, float32(v)/float32(math.MaxUint32+1))
}
assert.Equal(t, uint64(math.MaxUint32+1), count)
}
}
func TestCorner(t *testing.T) {
ch := New(1, func(option *Option) {
option.hfn = crc32.ChecksumIEEE
})
ch = New(1)
nodes := ch.Nodes()
assert.Equal(t, nil, nodes)
ch = New(1)
ch.Add("127.0.0.1")
nodes = ch.Nodes()
exptectedNodes := map[string]uint64{
"127.0.0.1": math.MaxUint32 + 1,
}
assert.Equal(t, exptectedNodes, nodes)
}