This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
serverset_test.go
152 lines (122 loc) · 3.83 KB
/
serverset_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
package serversets
import (
"reflect"
"testing"
)
const TestServer = "localhost"
// This is the big run through a typical use case of add and remove and make sure it works.
func TestServerSetAddAndRemove(t *testing.T) {
set := New(Test, "gotest", []string{TestServer})
watch, err := set.Watch()
if err != nil {
panic(err)
}
// add first endpoint
ep1, err := set.RegisterEndpoint("localhost", 1, nil)
if err != nil {
t.Fatalf("registration failure: %v", err)
}
<-watch.Event()
if !reflect.DeepEqual(watch.Endpoints(), []string{"localhost:1"}) {
t.Errorf("server list incorrect, got %v", watch.Endpoints())
}
// add second endpoint
ep2, err := set.RegisterEndpoint("localhost", 2, nil)
if err != nil {
t.Fatalf("registration failure: %v", err)
}
<-watch.Event()
if !reflect.DeepEqual(watch.Endpoints(), []string{"localhost:1", "localhost:2"}) {
t.Errorf("server list incorrect, got %v", watch.Endpoints())
}
// add third endpoint
ep3, err := set.RegisterEndpoint("localhost", 3, nil)
if err != nil {
t.Fatalf("registration failure: %v", err)
}
<-watch.Event()
if !reflect.DeepEqual(watch.Endpoints(), []string{"localhost:1", "localhost:2", "localhost:3"}) {
t.Errorf("server list incorrect, got %v", watch.Endpoints())
}
// remove second endpoint
ep2.Close()
<-watch.Event()
if !reflect.DeepEqual(watch.Endpoints(), []string{"localhost:1", "localhost:3"}) {
t.Errorf("server list incorrect, got %v", watch.Endpoints())
}
// add fourth endpoint
ep4, err := set.RegisterEndpoint("localhost", 4, nil)
if err != nil {
t.Fatalf("registration failure: %v", err)
}
<-watch.Event()
if !reflect.DeepEqual(watch.Endpoints(), []string{"localhost:1", "localhost:3", "localhost:4"}) {
t.Errorf("server list incorrect, got %v", watch.Endpoints())
}
// close and reopen watch
if watch.EventCount != 5 {
t.Errorf("event count incorrect, got %d", watch.EventCount)
}
watch.Close()
watch, err = set.Watch()
if err != nil {
panic(err)
}
if !reflect.DeepEqual(watch.Endpoints(), []string{"localhost:1", "localhost:3", "localhost:4"}) {
t.Errorf("server list incorrect, got %v", watch.Endpoints())
}
// remove third endpoint
ep3.Close()
<-watch.Event()
if !reflect.DeepEqual(watch.Endpoints(), []string{"localhost:1", "localhost:4"}) {
t.Errorf("server list incorrect, got %v", watch.Endpoints())
}
// remove first endpoint
ep1.Close()
<-watch.Event()
if !reflect.DeepEqual(watch.Endpoints(), []string{"localhost:4"}) {
t.Errorf("server list incorrect, got %v", watch.Endpoints())
}
// remove fourth endpoint
ep4.Close()
<-watch.Event()
if !reflect.DeepEqual(watch.Endpoints(), []string{}) {
t.Errorf("server list incorrect, got %v", watch.Endpoints())
}
if watch.EventCount != 3 {
t.Errorf("event count incorrect, got %d", watch.EventCount)
}
watch.Close()
}
func TestBaseZnodePath(t *testing.T) {
// to verify nothing happens to the default
path := BaseZnodePath(Test, "gotest")
if path != "/discovery/test/gotest" {
t.Errorf("baseznodepath incorrect, got %v", path)
}
}
func TestServerSetDirectoryPath(t *testing.T) {
set := New(Test, "gotest", []string{TestServer})
path := set.directoryPath()
// should just be a pass through to BaseZnodePath
if path != BaseZnodePath(Test, "gotest") {
t.Errorf("directory path incorrect, got %v", path)
}
}
func TestSplitPaths(t *testing.T) {
path := "/discovery/test/gotest"
parts := splitPaths(path)
if !reflect.DeepEqual(parts, []string{"/discovery", "/discovery/test", "/discovery/test/gotest"}) {
t.Errorf("split not correct, got %v", parts)
}
path = "/discovery/test/"
parts = splitPaths(path)
if !reflect.DeepEqual(parts, []string{"/discovery", "/discovery/test"}) {
t.Errorf("split not correct, got %v", parts)
}
path = "/"
parts = splitPaths(path)
if !reflect.DeepEqual(parts, []string{}) {
t.Errorf("split not correct, got %v", parts)
}
}