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
/
Copy pathendpoint_test.go
163 lines (130 loc) · 3.26 KB
/
endpoint_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
package serversets
import (
"errors"
"testing"
"time"
)
func TestEndpointSameName(t *testing.T) {
set := New(Test, "gotest", []string{TestServer})
watch, err := set.Watch()
if err != nil {
panic(err)
}
defer watch.Close()
// add first endpoint
ep1, err := set.RegisterEndpoint("localhost", 1, nil)
if err != nil {
t.Fatalf("registration failure: %v", err)
}
defer ep1.Close()
<-watch.Event()
ep2, err := set.RegisterEndpoint("localhost", 1, nil)
if err != nil {
t.Fatalf("registration failure: %v", err)
}
defer ep2.Close()
<-watch.Event()
if l := len(watch.Endpoints()); l != 2 {
t.Errorf("should have 2 servers, got %v", watch.Endpoints())
}
}
func TestEndpointPingInitiallyUp(t *testing.T) {
set := New(Test, "gotest", []string{TestServer})
watch, err := set.Watch()
if err != nil {
panic(err)
}
defer watch.Close()
alive := false // will switch to up on first check
pingFunction := func() error {
alive = !alive
if !alive {
return errors.New("not alive")
}
return nil
}
// add endpoint
ep, err := set.RegisterEndpoint("localhost", 1, pingFunction)
if err != nil {
t.Fatalf("registration failure: %v", err)
}
defer ep.Close()
<-watch.Event()
if l := len(watch.Endpoints()); l != 1 {
t.Errorf("should have one endpoint, got %v", watch.Endpoints())
}
// in a second should ping to false
<-watch.Event()
if l := len(watch.Endpoints()); l != 0 {
t.Errorf("should have zero endpoints, got %v", watch.Endpoints())
}
// third ping should be true
<-watch.Event()
if l := len(watch.Endpoints()); l != 1 {
t.Errorf("should have one endpoint, got %v", watch.Endpoints())
}
}
func TestEndpointPingInitiallyDown(t *testing.T) {
set := New(Test, "gotest", []string{TestServer})
watch, err := set.Watch()
if err != nil {
panic(err)
}
defer watch.Close()
alive := true // will switch to down on first check
pingFunction := func() error {
alive = !alive
if !alive {
return errors.New("not alive")
}
return nil
}
// add endpoint
ep, err := set.RegisterEndpoint("localhost", 1, pingFunction)
if err != nil {
t.Fatalf("registration failure: %v", err)
}
defer ep.Close()
// down so doesn't register itself
// <-watch.Event()
if l := len(watch.Endpoints()); l != 0 {
t.Errorf("should have zero endpoints, got %v", watch.Endpoints())
}
// in a second should ping to true
<-watch.Event()
if l := len(watch.Endpoints()); l != 1 {
t.Errorf("should have one endpoint, got %v", watch.Endpoints())
}
// third ping should be false
<-watch.Event()
if l := len(watch.Endpoints()); l != 0 {
t.Errorf("should have zero endpoints, got %v", watch.Endpoints())
}
}
func TestEndpointClosePingRoutine(t *testing.T) {
set := New(Test, "gotest", []string{TestServer})
ping := 0
ep, err := set.RegisterEndpoint("localhost", 1, func() error {
ping++
return nil
})
if err != nil {
t.Fatalf("registration failure: %v", err)
}
ep.Close()
time.Sleep(3 * time.Second)
if ping > 1 {
t.Errorf("ping should be closed, called %d times", ping)
}
}
func TestEndpointMultipleClose(t *testing.T) {
set := New(Test, "gotest", []string{TestServer})
ep, err := set.RegisterEndpoint("localhost", 1, nil)
if err != nil {
t.Fatalf("registration failure: %v", err)
}
ep.Close()
ep.Close()
ep.Close()
ep.Close()
}