forked from lafikl/consistent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsistent_test.go
118 lines (93 loc) · 1.86 KB
/
consistent_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
package consistent
import (
"fmt"
"testing"
)
func TestAdd(t *testing.T) {
c := New()
c.Add("127.0.0.1:8000")
if len(c.sortedSet) != replicationFactor {
t.Fatal("vnodes number is incorrect")
}
}
func TestGet(t *testing.T) {
c := New()
c.Add("127.0.0.1:8000")
host, err := c.Get("127.0.0.1:8000")
if err != nil {
t.Fatal(err)
}
if host != "127.0.0.1:8000" {
t.Fatal("returned host is not what expected")
}
}
func TestRemove(t *testing.T) {
c := New()
c.Add("127.0.0.1:8000")
c.Remove("127.0.0.1:8000")
if len(c.sortedSet) != 0 && len(c.hosts) != 0 {
t.Fatal(("remove is not working"))
}
}
func TestGetLeast(t *testing.T) {
c := New()
c.Add("127.0.0.1:8000")
c.Add("92.0.0.1:8000")
for i := 0; i < 100; i++ {
host, err := c.GetLeast("92.0.0.1:80001")
if err != nil {
t.Fatal(err)
}
c.Inc(host)
}
for k, v := range c.GetLoads() {
if v > c.MaxLoad() {
t.Fatalf("host %s is overloaded. %d > %d\n", k, v, c.MaxLoad())
}
}
fmt.Println("Max load per node", c.MaxLoad())
fmt.Println(c.GetLoads())
}
func TestIncDone(t *testing.T) {
c := New()
c.Add("127.0.0.1:8000")
c.Add("92.0.0.1:8000")
host, err := c.GetLeast("92.0.0.1:80001")
if err != nil {
t.Fatal(err)
}
c.Inc(host)
if c.loadMap[host].Load != 1 {
t.Fatalf("host %s load should be 1\n", host)
}
c.Done(host)
if c.loadMap[host].Load != 0 {
t.Fatalf("host %s load should be 0\n", host)
}
}
func TestHosts(t *testing.T) {
hosts := []string{
"127.0.0.1:8000",
"92.0.0.1:8000",
}
c := New()
for _, h := range hosts {
c.Add(h)
}
fmt.Println("hosts in the ring", c.Hosts())
addedHosts := c.Hosts()
for _, h := range hosts {
found := false
for _, ah := range addedHosts {
if h == ah {
found = true
break
}
}
if !found {
t.Fatal("missing host", h)
}
}
c.Remove("127.0.0.1:8000")
fmt.Println("hosts in the ring", c.Hosts())
}