-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster_test.go
232 lines (209 loc) · 4.95 KB
/
cluster_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"fmt"
"testing"
)
var gCluster *cluster
func makeNewClusterData(neighbors []nodeData) (nodeData, *cluster) {
myself := nodeData{
Nickname: "myself",
UUID: "test-uuid",
BaseURL: "localhost:8080",
Location: "test",
Writeable: true,
}
if gCluster == nil {
c := newCluster(myself)
for _, n := range neighbors {
c.AddNeighbor(n)
}
gCluster = c
return myself, gCluster
}
gCluster.Myself = myself
gCluster.neighbors = map[string]nodeData{}
return myself, gCluster
}
func Test_ClusterOfOneInitialNeighbors(t *testing.T) {
var n []nodeData
_, c := makeNewClusterData(n)
if len(c.GetNeighbors()) != 0 {
t.Error("should not have any neighbors yet")
}
}
func Test_ClusterOfOneNeighborsInclusive(t *testing.T) {
var n []nodeData
myself, c := makeNewClusterData(n)
neighbors := c.NeighborsInclusive()
if len(neighbors) != 1 {
t.Error("too many neighbors for empty cluster")
}
if neighbors[0].Nickname != myself.Nickname {
t.Error("single node is not myself")
}
}
func Test_ClusterOfOneFindNeighbors(t *testing.T) {
var n []nodeData
myself, c := makeNewClusterData(n)
neighbors := c.NeighborsInclusive()
_, found := c.FindNeighborByUUID("test-uuid")
if found {
t.Error("neighbors should be empty")
}
neighbors = c.WriteableNeighbors()
if len(neighbors) != 1 {
t.Error("too many neighbors for empty cluster")
}
if neighbors[0].Nickname != myself.Nickname {
t.Error("single node is not myself")
}
r := c.Ring()
if len(r) != REPLICAS {
t.Error("wrong number of ring entries")
}
wr := c.WriteRing()
if len(wr) != REPLICAS {
t.Error("wrong number of write ring entries")
}
ro := c.ReadOrder("anyhash")
if len(ro) != 1 {
t.Error("only one node, should only be one result in the list")
}
if ro[0].UUID != myself.UUID {
t.Error("it's not me!")
}
wo := c.WriteOrder("anyhash")
if len(wo) != 1 {
t.Error("only one node, should only be one result in the list")
}
if wo[0].UUID != myself.UUID {
t.Error("it's not me!")
}
}
func checkForNeighbor(c *cluster, n nodeData, t *testing.T) {
rn, found := c.FindNeighborByUUID(n.UUID)
if !found {
t.Error(fmt.Sprintf("couldn't find %s by UUID", n.UUID))
}
if rn.Nickname != n.Nickname {
t.Error("not the same nickname")
}
}
func checkForNeighborAfterRemoval(c *cluster, n nodeData, i int, t *testing.T) {
rn, found := c.FindNeighborByUUID(n.UUID)
if i == 2 {
// the one that was removed
if found {
t.Error("found the one we removed")
}
} else {
if !found {
t.Error(fmt.Sprintf("couldn't find %s by UUID", n.UUID))
}
if rn.Nickname != n.Nickname {
t.Error("not the same nickname")
}
}
}
func Test_AddNeighbor(t *testing.T) {
var n []nodeData
_, c := makeNewClusterData(n)
if len(c.GetNeighbors()) != 0 {
t.Error("should not have any neighbors yet")
}
c.AddNeighbor(nodeData{
Nickname: "addedneighbor",
UUID: "test-uuid-2",
BaseURL: "localhost:8081",
Location: "test",
Writeable: true,
})
if len(c.GetNeighbors()) != 1 {
t.Error("should be only one")
}
}
func Test_RemoveNeighbor(t *testing.T) {
var n []nodeData
_, c := makeNewClusterData(n)
if len(c.GetNeighbors()) != 0 {
t.Error("should not have any neighbors yet")
}
nd := nodeData{
Nickname: "addedneighbor",
UUID: "test-uuid-2",
BaseURL: "localhost:8081",
Location: "test",
Writeable: true,
}
nd2 := nodeData{
Nickname: "addedneighbor3",
UUID: "test-uuid-3",
BaseURL: "localhost:8082",
Location: "test",
Writeable: true,
}
c.AddNeighbor(nd)
if len(c.GetNeighbors()) != 1 {
t.Error("should be only one")
}
c.AddNeighbor(nd2)
if len(c.GetNeighbors()) != 2 {
t.Error("should be two")
}
c.RemoveNeighbor(nd)
if len(c.GetNeighbors()) != 1 {
t.Error("should be one in there")
}
c.RemoveNeighbor(nd2)
if len(c.GetNeighbors()) != 0 {
t.Error("should be back to zero")
}
}
func Test_UpdateNeighbor(t *testing.T) {
var n []nodeData
_, c := makeNewClusterData(n)
if len(c.GetNeighbors()) != 0 {
t.Error("should not have any neighbors yet")
}
nd := nodeData{
Nickname: "addedneighbor",
UUID: "test-uuid-2",
BaseURL: "localhost:8081",
Location: "test",
Writeable: true,
}
c.AddNeighbor(nd)
if len(c.GetNeighbors()) != 1 {
t.Error("should be only one")
}
nd.BaseURL = "localhost:8082"
c.UpdateNeighbor(nd)
neighbors := c.GetNeighbors()
if neighbors[0].BaseURL != "localhost:8082" {
t.Error("update didn't take")
}
}
func Test_FailedNeighbor(t *testing.T) {
var n []nodeData
_, c := makeNewClusterData(n)
if len(c.GetNeighbors()) != 0 {
t.Error("should not have any neighbors yet")
}
nd := nodeData{
Nickname: "addedneighbor",
UUID: "test-uuid-2",
BaseURL: "localhost:8081",
Location: "test",
Writeable: true,
}
c.AddNeighbor(nd)
if len(c.GetNeighbors()) != 1 {
t.Error("should be only one")
}
nd.BaseURL = "localhost:8082"
c.FailedNeighbor(nd)
neighbors := c.GetNeighbors()
if neighbors[0].Writeable {
t.Error("failed notification didn't take")
}
}