-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrendezvous_test.go
147 lines (122 loc) · 3.19 KB
/
rendezvous_test.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package rendezvous
import (
"fmt"
"reflect"
"testing"
)
var sampleKeys = []string{
"352DAB08-C1FD-4462-B573-7640B730B721",
"382080D3-B847-4BB5-AEA8-644C3E56F4E1",
"2B340C12-7958-4DBE-952C-67496E15D0C8",
"BE05F82B-902E-4868-8CC9-EE50A6C64636",
"C7ECC571-E924-4523-A313-951DFD5D8073",
}
type getTestcase struct {
key string
expectedNode string
}
func TestHashGet(t *testing.T) {
hash := New()
gotNode := hash.Get("foo")
if len(gotNode) != 0 {
t.Errorf("got: %#v, expected: %#v", gotNode, "")
}
hash.Add("a", "b", "c", "d", "e")
testcases := []getTestcase{
{"", "d"},
{"foo", "e"},
{"bar", "c"},
}
for _, testcase := range testcases {
gotNode := hash.Get(testcase.key)
if gotNode != testcase.expectedNode {
t.Errorf("got: %#v, expected: %#v", gotNode, testcase.expectedNode)
}
}
}
func BenchmarkHashGet_5nodes(b *testing.B) {
hash := New("a", "b", "c", "d", "e")
for i := 0; i < b.N; i++ {
hash.Get(sampleKeys[i%len(sampleKeys)])
}
}
func BenchmarkHashGet_10nodes(b *testing.B) {
hash := New("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
for i := 0; i < b.N; i++ {
hash.Get(sampleKeys[i%len(sampleKeys)])
}
}
type getNTestcase struct {
count int
key string
expectedNodes []string
}
func Test_Hash_GetN(t *testing.T) {
hash := New()
gotNodes := hash.GetN(2, "foo")
if len(gotNodes) != 0 {
t.Errorf("got: %#v, expected: %#v", gotNodes, []string{})
}
hash.Add("a", "b", "c", "d", "e")
testcases := []getNTestcase{
{1, "foo", []string{"e"}},
{2, "bar", []string{"c", "e"}},
{3, "baz", []string{"d", "a", "b"}},
{2, "biz", []string{"b", "a"}},
{0, "boz", []string{}},
{100, "floo", []string{"d", "a", "b", "c", "e"}},
}
for _, testcase := range testcases {
gotNodes := hash.GetN(testcase.count, testcase.key)
if !reflect.DeepEqual(gotNodes, testcase.expectedNodes) {
t.Errorf("got: %#v, expected: %#v", gotNodes, testcase.expectedNodes)
}
}
}
func BenchmarkHashGetN3_5_nodes(b *testing.B) {
hash := New("a", "b", "c", "d", "e")
for i := 0; i < b.N; i++ {
hash.GetN(3, sampleKeys[i%len(sampleKeys)])
}
}
func BenchmarkHashGetN5_5_nodes(b *testing.B) {
hash := New("a", "b", "c", "d", "e")
for i := 0; i < b.N; i++ {
hash.GetN(5, sampleKeys[i%len(sampleKeys)])
}
}
func BenchmarkHashGetN3_10_nodes(b *testing.B) {
hash := New("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
for i := 0; i < b.N; i++ {
hash.GetN(3, sampleKeys[i%len(sampleKeys)])
}
}
func BenchmarkHashGetN5_10_nodes(b *testing.B) {
hash := New("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
for i := 0; i < b.N; i++ {
hash.GetN(5, sampleKeys[i%len(sampleKeys)])
}
}
func TestHashRemove(t *testing.T) {
hash := New("a", "b", "c")
var keyForB string
for i := 0; i < 10000; i++ {
randomKey := fmt.Sprintf("key-%d", i)
if hash.Get(randomKey) == "b" {
keyForB = randomKey
break
}
}
if keyForB == "" {
t.Fatalf("Failed to find a key that maps to 'b'")
}
hash.Remove("b")
// Check if the key now maps to a different node
newNode := hash.Get(keyForB)
if newNode == "b" {
t.Errorf("Key %s still maps to removed node 'b'", keyForB)
}
if newNode == "" {
t.Errorf("Key %s does not map to any node after removing 'b'", keyForB)
}
}