-
Notifications
You must be signed in to change notification settings - Fork 627
/
Copy pathconnection_internal_test.go
365 lines (297 loc) · 8.96 KB
/
connection_internal_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Licensed to Elasticsearch B.V. under one or more agreements.
// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
// +build !integration
package estransport
import (
"net/url"
"regexp"
"testing"
"time"
)
func TestSingleConnectionPoolNext(t *testing.T) {
t.Run("Single URL", func(t *testing.T) {
pool := &singleConnectionPool{
connection: &Connection{URL: &url.URL{Scheme: "http", Host: "foo1"}},
}
for i := 0; i < 7; i++ {
c, err := pool.Next()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if c.URL.String() != "http://foo1" {
t.Errorf("Unexpected URL, want=http://foo1, got=%s", c.URL)
}
}
})
}
func TestSingleConnectionPoolOnFailure(t *testing.T) {
t.Run("Noop", func(t *testing.T) {
pool := &singleConnectionPool{
connection: &Connection{URL: &url.URL{Scheme: "http", Host: "foo1"}},
}
if err := pool.OnFailure(&Connection{URL: &url.URL{Scheme: "http", Host: "foo1"}}); err != nil {
t.Errorf("Unexpected error: %s", err)
}
})
}
func TestStatusConnectionPoolNext(t *testing.T) {
t.Run("No URL", func(t *testing.T) {
pool := &statusConnectionPool{}
c, err := pool.Next()
if err == nil {
t.Errorf("Expected error, but got: %s", c.URL)
}
})
t.Run("Two URLs", func(t *testing.T) {
var c *Connection
pool := &statusConnectionPool{
live: []*Connection{
&Connection{URL: &url.URL{Scheme: "http", Host: "foo1"}},
&Connection{URL: &url.URL{Scheme: "http", Host: "foo2"}},
},
selector: &roundRobinSelector{curr: -1},
}
c, _ = pool.Next()
if c.URL.String() != "http://foo1" {
t.Errorf("Unexpected URL, want=foo1, got=%s", c.URL)
}
c, _ = pool.Next()
if c.URL.String() != "http://foo2" {
t.Errorf("Unexpected URL, want=http://foo2, got=%s", c.URL)
}
c, _ = pool.Next()
if c.URL.String() != "http://foo1" {
t.Errorf("Unexpected URL, want=http://foo1, got=%s", c.URL)
}
})
t.Run("Three URLs", func(t *testing.T) {
pool := &statusConnectionPool{
live: []*Connection{
&Connection{URL: &url.URL{Scheme: "http", Host: "foo1"}},
&Connection{URL: &url.URL{Scheme: "http", Host: "foo2"}},
&Connection{URL: &url.URL{Scheme: "http", Host: "foo3"}},
},
selector: &roundRobinSelector{curr: -1},
}
var expected string
for i := 0; i < 11; i++ {
c, err := pool.Next()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
switch i % len(pool.live) {
case 0:
expected = "http://foo1"
case 1:
expected = "http://foo2"
case 2:
expected = "http://foo3"
default:
t.Fatalf("Unexpected i %% 3: %d", i%3)
}
if c.URL.String() != expected {
t.Errorf("Unexpected URL, want=%s, got=%s", expected, c.URL)
}
}
})
t.Run("Resurrect dead connection when no live is available", func(t *testing.T) {
pool := &statusConnectionPool{
live: []*Connection{},
dead: []*Connection{
&Connection{URL: &url.URL{Scheme: "http", Host: "foo1"}, Failures: 3},
&Connection{URL: &url.URL{Scheme: "http", Host: "foo2"}, Failures: 1},
},
selector: &roundRobinSelector{curr: -1},
}
c, err := pool.Next()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if c == nil {
t.Errorf("Expected connection, got nil: %s", c)
}
if c.URL.String() != "http://foo2" {
t.Errorf("Expected <http://foo2>, got: %s", c.URL.String())
}
if c.IsDead {
t.Errorf("Expected connection to be live, got: %s", c)
}
if len(pool.live) != 1 {
t.Errorf("Expected 1 connection in live list, got: %s", pool.live)
}
if len(pool.dead) != 1 {
t.Errorf("Expected 1 connection in dead list, got: %s", pool.dead)
}
})
}
func TestStatusConnectionPoolOnSuccess(t *testing.T) {
t.Run("Move connection to live list and mark it as healthy", func(t *testing.T) {
pool := &statusConnectionPool{
dead: []*Connection{
&Connection{URL: &url.URL{Scheme: "http", Host: "foo1"}, Failures: 3, IsDead: true},
},
selector: &roundRobinSelector{curr: -1},
}
conn := pool.dead[0]
if err := pool.OnSuccess(conn); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if conn.IsDead {
t.Errorf("Expected the connection to be live; %s", conn)
}
if !conn.DeadSince.IsZero() {
t.Errorf("Unexpected value for DeadSince: %s", conn.DeadSince)
}
if len(pool.live) != 1 {
t.Errorf("Expected 1 live connection, got: %d", len(pool.live))
}
if len(pool.dead) != 0 {
t.Errorf("Expected 0 dead connections, got: %d", len(pool.dead))
}
})
}
func TestStatusConnectionPoolOnFailure(t *testing.T) {
t.Run("Remove connection, mark it, and sort dead connections", func(t *testing.T) {
pool := &statusConnectionPool{
live: []*Connection{
&Connection{URL: &url.URL{Scheme: "http", Host: "foo1"}},
&Connection{URL: &url.URL{Scheme: "http", Host: "foo2"}},
},
dead: []*Connection{
&Connection{URL: &url.URL{Scheme: "http", Host: "foo3"}, Failures: 0},
&Connection{URL: &url.URL{Scheme: "http", Host: "foo4"}, Failures: 99},
},
selector: &roundRobinSelector{curr: -1},
}
conn := pool.live[0]
if err := pool.OnFailure(conn); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if !conn.IsDead {
t.Errorf("Expected the connection to be dead; %s", conn)
}
if conn.DeadSince.IsZero() {
t.Errorf("Unexpected value for DeadSince: %s", conn.DeadSince)
}
if len(pool.live) != 1 {
t.Errorf("Expected 1 live connection, got: %d", len(pool.live))
}
if len(pool.dead) != 3 {
t.Errorf("Expected 3 dead connections, got: %d", len(pool.dead))
}
expected := []string{
"http://foo4",
"http://foo1",
"http://foo3",
}
for i, u := range expected {
if pool.dead[i].URL.String() != u {
t.Errorf("Unexpected value for item %d in pool.dead: %s", i, pool.dead[i].URL.String())
}
}
})
t.Run("Short circuit when the connection is already dead", func(t *testing.T) {
pool := &statusConnectionPool{
live: []*Connection{
&Connection{URL: &url.URL{Scheme: "http", Host: "foo1"}},
&Connection{URL: &url.URL{Scheme: "http", Host: "foo2"}},
&Connection{URL: &url.URL{Scheme: "http", Host: "foo3"}},
},
selector: &roundRobinSelector{curr: -1},
}
conn := pool.live[0]
conn.IsDead = true
if err := pool.OnFailure(conn); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if len(pool.dead) != 0 {
t.Errorf("Expected the dead list to be empty, got: %s", pool.dead)
}
})
}
func TestStatusConnectionPoolResurrect(t *testing.T) {
t.Run("Mark the connection as dead and add/remove it to the lists", func(t *testing.T) {
pool := &statusConnectionPool{
live: []*Connection{},
dead: []*Connection{&Connection{URL: &url.URL{Scheme: "http", Host: "foo1"}, IsDead: true}},
selector: &roundRobinSelector{curr: -1},
}
conn := pool.dead[0]
if err := pool.resurrect(conn, true); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if conn.IsDead {
t.Errorf("Expected connection to be dead, got: %s", conn)
}
if len(pool.dead) != 0 {
t.Errorf("Expected no dead connections, got: %s", pool.dead)
}
if len(pool.live) != 1 {
t.Errorf("Expected 1 live connection, got: %s", pool.live)
}
})
t.Run("Short circuit removal when the connection is not in the dead list", func(t *testing.T) {
pool := &statusConnectionPool{
dead: []*Connection{&Connection{URL: &url.URL{Scheme: "http", Host: "bar"}, IsDead: true}},
selector: &roundRobinSelector{curr: -1},
}
conn := &Connection{URL: &url.URL{Scheme: "http", Host: "foo1"}, IsDead: true}
if err := pool.resurrect(conn, true); err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if len(pool.live) != 1 {
t.Errorf("Expected 1 live connection, got: %s", pool.live)
}
if len(pool.dead) != 1 {
t.Errorf("Expected 1 dead connection, got: %s", pool.dead)
}
})
t.Run("Schedule resurrect", func(t *testing.T) {
defaultResurrectTimeoutInitial = 0
defer func() { defaultResurrectTimeoutInitial = 60 * time.Second }()
pool := &statusConnectionPool{
live: []*Connection{},
dead: []*Connection{
&Connection{
URL: &url.URL{Scheme: "http", Host: "foo1"},
Failures: 100,
IsDead: true,
DeadSince: time.Now().UTC(),
},
},
selector: &roundRobinSelector{curr: -1},
}
conn := pool.dead[0]
pool.scheduleResurrect(conn)
time.Sleep(50 * time.Millisecond)
pool.Lock()
defer pool.Unlock()
if len(pool.live) != 1 {
t.Errorf("Expected 1 live connection, got: %s", pool.live)
}
if len(pool.dead) != 0 {
t.Errorf("Expected no dead connections, got: %s", pool.dead)
}
})
}
func TestConnection(t *testing.T) {
t.Run("String", func(t *testing.T) {
conn := &Connection{
URL: &url.URL{Scheme: "http", Host: "foo1"},
Failures: 10,
IsDead: true,
DeadSince: time.Now().UTC(),
}
match, err := regexp.MatchString(
`<http://foo1> dead=true failures=10`,
conn.String(),
)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if !match {
t.Errorf("Unexpected output: %s", conn)
}
})
}