goconsistencehash/consistencehash_test.go
2025-05-07 10:34:10 +08:00

150 lines
3.5 KiB
Go

package consistencehash
import (
"context"
"fmt"
"git.zhangshuocauc.cn/redhat/goconsistencehash/local"
"git.zhangshuocauc.cn/redhat/goconsistencehash/redis/hashring"
"github.com/redis/go-redis/v9"
"hash/fnv"
"testing"
)
func migrator(ctx context.Context, datas map[string]struct{}, from, to string) error {
fmt.Printf("from: %s, to: %s, datas: %v", from, to, datas)
return nil
}
func TestConHash_Local(t *testing.T) {
hashRing := local.NewHashRing(&local.BlockLock{})
encrytor := NewEncrytorImp(fnv.New32())
hash := NewConsistenceHash(
hashRing,
encrytor,
migrator,
WithReplicas(10),
WithLockExpire(10),
)
test(t, hash)
}
func TestConHash_Redis(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: "192.168.8.1:6379",
Password: "",
})
lockwap := hashring.NewRedisLockWrap(client, "redhat_redis_lock")
hashRing := hashring.NewRedisHashRing(client, "redhat_redis_hashring", lockwap)
encrytor := NewEncrytorImp(fnv.New32())
hash := NewConsistenceHash(
hashRing,
encrytor,
migrator,
WithReplicas(10),
WithLockExpire(10),
)
test(t, hash)
}
func test(t *testing.T, consistentHash *ConsistenceHash) {
ctx := context.Background()
nodeA := "node_a"
weightNodeA := 2
nodeB := "node_b"
weightNodeB := 1
nodeC := "node_c"
weightNodeC := 1
if err := consistentHash.AddNode(ctx, nodeA, weightNodeA); err != nil {
t.Error(err)
return
}
if err := consistentHash.AddNode(ctx, nodeB, weightNodeB); err != nil {
t.Error(err)
return
}
dataKeyA := "data_a"
dataKeyB := "data_b"
dataKeyC := "data_c"
dataKeyD := "data_d"
node, err := consistentHash.GetNode(ctx, dataKeyA)
fmt.Println(node)
if err != nil {
t.Error(err)
return
}
t.Logf("data: %s belongs to node: %s", dataKeyA, node)
if node, err = consistentHash.GetNode(ctx, dataKeyB); err != nil {
t.Error(err)
return
}
t.Logf("data: %s belongs to node: %s", dataKeyB, node)
if node, err = consistentHash.GetNode(ctx, dataKeyC); err != nil {
t.Error(err)
return
}
t.Logf("data: %s belongs to node: %s", dataKeyC, node)
if node, err = consistentHash.GetNode(ctx, dataKeyD); err != nil {
t.Error(err)
return
}
t.Logf("data: %s belongs to node: %s", dataKeyD, node)
if err := consistentHash.AddNode(ctx, nodeC, weightNodeC); err != nil {
t.Error(err)
return
}
if node, err = consistentHash.GetNode(ctx, dataKeyA); err != nil {
t.Error(err)
return
}
t.Logf("data: %s belongs to node: %s", dataKeyA, node)
if node, err = consistentHash.GetNode(ctx, dataKeyB); err != nil {
t.Error(err)
return
}
t.Logf("data: %s belongs to node: %s", dataKeyB, node)
if node, err = consistentHash.GetNode(ctx, dataKeyC); err != nil {
t.Error(err)
return
}
t.Logf("data: %s belongs to node: %s", dataKeyC, node)
if node, err = consistentHash.GetNode(ctx, dataKeyD); err != nil {
t.Error(err)
return
}
t.Logf("data: %s belongs to node: %s", dataKeyD, node)
if err = consistentHash.DelNode(ctx, nodeC); err != nil {
t.Error(err)
return
}
if node, err = consistentHash.GetNode(ctx, dataKeyA); err != nil {
t.Error(err)
return
}
t.Logf("data: %s belongs to node: %s", dataKeyA, node)
if node, err = consistentHash.GetNode(ctx, dataKeyB); err != nil {
t.Error(err)
return
}
t.Logf("data: %s belongs to node: %s", dataKeyB, node)
if node, err = consistentHash.GetNode(ctx, dataKeyC); err != nil {
t.Error(err)
return
}
t.Logf("data: %s belongs to node: %s", dataKeyC, node)
if node, err = consistentHash.GetNode(ctx, dataKeyD); err != nil {
t.Error(err)
return
}
t.Logf("data: %s belongs to node: %s", dataKeyD, node)
t.Error("ok")
}