-
Notifications
You must be signed in to change notification settings - Fork 48
/
consistent_test.go
142 lines (111 loc) · 2.38 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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())
}
func TestDelSlice(t *testing.T) {
items := []uint64{0, 1, 2, 3, 5, 20, 22, 23, 25, 27, 28, 30, 35, 37, 1008, 1009}
deletes := []uint64{25, 37, 1009, 3, 100000}
c := &Consistent{}
c.sortedSet = append(c.sortedSet, items...)
fmt.Printf("before deletion%+v\n", c.sortedSet)
for _, val := range deletes {
c.delSlice(val)
}
for _, val := range deletes {
for _, item := range c.sortedSet {
if item == val {
t.Fatalf("%d wasn't deleted\n", val)
}
}
}
fmt.Printf("after deletions: %+v\n", c.sortedSet)
}