-
Notifications
You must be signed in to change notification settings - Fork 6
/
xring_test.go
71 lines (61 loc) · 1.75 KB
/
xring_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
package xring
import (
"testing"
)
func TestGet(t *testing.T) {
nodes := []string{"a", "b", "c"}
cnf := &Config{
VirtualNodes: 0,
LoadFactor: 1,
}
hashRing := NewRing(nodes, cnf)
expectNodesABC(t, hashRing)
}
func expectNodesABC(t *testing.T, hashRing *Ring) {
// Python hash_ring module test case
expectNode(t, hashRing, "test", "a")
expectNode(t, hashRing, "test", "a")
expectNode(t, hashRing, "test1", "c")
expectNode(t, hashRing, "test2", "c")
expectNode(t, hashRing, "test3", "c")
expectNode(t, hashRing, "test4", "c")
expectNode(t, hashRing, "test5", "b")
expectNode(t, hashRing, "aaaa", "c")
expectNode(t, hashRing, "bbbb", "a")
}
func expectNode(t *testing.T, hashRing *Ring, key string, expected string) {
node, err := hashRing.Get(key)
if err != nil || node != expected {
t.Error("GetNode(", key, ") expected", expected, "but got", node, err)
}
hashRing.Done(node)
}
func failNode(t *testing.T, hashRing *Ring, key string, expected string, expectedErr error) {
node, err := hashRing.Get(key)
if err != expectedErr || node != expected {
t.Error("GetNode(", key, ") expected", expected, "but got", node, err)
}
}
func TestHeavyLoad(t *testing.T) {
nodes := []string{"a", "b", "c"}
cnf := &Config{
VirtualNodes: 0,
LoadFactor: 1,
}
hashRing := NewRing(nodes, cnf)
failNode(t, hashRing, "test", "a", nil)
failNode(t, hashRing, "test", "b", nil)
failNode(t, hashRing, "test", "c", nil)
failNode(t, hashRing, "test", "", ERR_HEAVY_LOAD)
}
func TestDistribution(t *testing.T) {
nodes := []string{"a", "b", "c"}
cnf := &Config{
VirtualNodes: 0,
LoadFactor: 1,
}
hashRing := NewRing(nodes, cnf)
failNode(t, hashRing, "test", "a", nil)
failNode(t, hashRing, "test", "b", nil)
failNode(t, hashRing, "test", "c", nil)
}