File size: 1,349 Bytes
287a0bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package utils

import (
	"fmt"
	"math"
	"testing"
)

func mockHasher(member string, key string) uint64 {
	members := []string{"a", "b", "c"}
	for i, m := range members {
		if m == member {
			return uint64(i)
		}
	}
	return 0
}

func TestRendezvousHash(t *testing.T) {
	members := []string{"a", "b", "c"}
	key := "key"

	// Test that the assign function returns the expected result
	node, error := Assign(key, members, mockHasher)

	if error != nil {
		t.Errorf("Assign() returned an error: %v", error)
	}

	if node != "c" {
		t.Errorf("Assign() = %v, want %v", node, "c")
	}
}

func TestEvenDistribution(t *testing.T) {
	memberCount := 10
	tolerance := 25
	var nodes []string
	for i := 0; i < memberCount; i++ {
		nodes = append(nodes, fmt.Sprint(i+'0')) // Convert int to string
	}

	keyDistribution := make(map[string]int)
	numKeys := 1000

	// Test if keys are evenly distributed across nodes
	for i := 0; i < numKeys; i++ {
		key := "key_" + fmt.Sprint(i)
		node, err := Assign(key, nodes, Murmur3Hasher)
		if err != nil {
			t.Errorf("Assign() returned an error: %v", err)
		}
		keyDistribution[node]++
	}

	// Check if keys are somewhat evenly distributed
	for _, count := range keyDistribution {
		if math.Abs(float64(count-numKeys/memberCount)) > float64(tolerance) {
			t.Errorf("Key distribution is uneven: %v", keyDistribution)
		}
	}
}