33
rrPolicy := new(RoundRobinSelection)
34
req, _ := http.NewRequest("GET", "/", nil)
35
36
- h := rrPolicy.Select(pool, req)
36
+ h := rrPolicy.Select(pool, req, nil)
37
// First selected host is 1, because counter starts at 0
38
// and increments before host is selected
39
if h != pool[1] {
40
t.Error("Expected first round robin host to be second host in the pool.")
41
}
42
- h = rrPolicy.Select(pool, req)
42
+ h = rrPolicy.Select(pool, req, nil)
43
if h != pool[2] {
44
t.Error("Expected second round robin host to be third host in the pool.")
45
}
46
- h = rrPolicy.Select(pool, req)
46
+ h = rrPolicy.Select(pool, req, nil)
47
if h != pool[0] {
48
t.Error("Expected third round robin host to be first host in the pool.")
49
}
50
// mark host as down
51
pool[1].SetHealthy(false)
52
- h = rrPolicy.Select(pool, req)
52
+ h = rrPolicy.Select(pool, req, nil)
53
if h != pool[2] {
54
t.Error("Expected to skip down host.")
55
}
56
// mark host as up
57
pool[1].SetHealthy(true)
58
59
- h = rrPolicy.Select(pool, req)
59
+ h = rrPolicy.Select(pool, req, nil)
60
if h == pool[2] {
61
t.Error("Expected to balance evenly among healthy hosts")
62
}
63
// mark host as full
64
pool[1].CountRequest(1)
65
pool[1].MaxRequests = 1
66
- h = rrPolicy.Select(pool, req)
66
+ h = rrPolicy.Select(pool, req, nil)
67
if h != pool[2] {
68
t.Error("Expected to skip full host.")
69
}
76
77
pool[0].CountRequest(10)
78
pool[1].CountRequest(10)
79
- h := lcPolicy.Select(pool, req)
79
+ h := lcPolicy.Select(pool, req, nil)
80
if h != pool[2] {
81
t.Error("Expected least connection host to be third host.")
82
}
83
pool[2].CountRequest(100)
84
- h = lcPolicy.Select(pool, req)
84
+ h = lcPolicy.Select(pool, req, nil)
85
if h != pool[0] && h != pool[1] {
86
t.Error("Expected least connection host to be first or second host.")
87
}
94
95
// We should be able to predict where every request is routed.
96
req.RemoteAddr = "172.0.0.1:80"
97
- h := ipHash.Select(pool, req)
97
+ h := ipHash.Select(pool, req, nil)
98
if h != pool[1] {
99
t.Error("Expected ip hash policy host to be the second host.")
100
}
101
req.RemoteAddr = "172.0.0.2:80"
102
- h = ipHash.Select(pool, req)
102
+ h = ipHash.Select(pool, req, nil)
103
if h != pool[1] {
104
t.Error("Expected ip hash policy host to be the second host.")
105
}
106
req.RemoteAddr = "172.0.0.3:80"
107
- h = ipHash.Select(pool, req)
107
+ h = ipHash.Select(pool, req, nil)
108
if h != pool[2] {
109
t.Error("Expected ip hash policy host to be the third host.")
110
}
111
req.RemoteAddr = "172.0.0.4:80"
112
- h = ipHash.Select(pool, req)
112
+ h = ipHash.Select(pool, req, nil)
113
if h != pool[1] {
114
t.Error("Expected ip hash policy host to be the second host.")
115
}
116
117
// we should get the same results without a port
118
req.RemoteAddr = "172.0.0.1"
119
- h = ipHash.Select(pool, req)
119
+ h = ipHash.Select(pool, req, nil)
120
if h != pool[1] {
121
t.Error("Expected ip hash policy host to be the second host.")
122
}
123
req.RemoteAddr = "172.0.0.2"
124
- h = ipHash.Select(pool, req)
124
+ h = ipHash.Select(pool, req, nil)
125
if h != pool[1] {
126
t.Error("Expected ip hash policy host to be the second host.")
127
}
128
req.RemoteAddr = "172.0.0.3"
129
- h = ipHash.Select(pool, req)
129
+ h = ipHash.Select(pool, req, nil)
130
if h != pool[2] {
131
t.Error("Expected ip hash policy host to be the third host.")
132
}
133
req.RemoteAddr = "172.0.0.4"
134
- h = ipHash.Select(pool, req)
134
+ h = ipHash.Select(pool, req, nil)
135
if h != pool[1] {
136
t.Error("Expected ip hash policy host to be the second host.")
137
}
140
// healthy host is available
141
req.RemoteAddr = "172.0.0.1"
142
pool[1].SetHealthy(false)
143
- h = ipHash.Select(pool, req)
143
+ h = ipHash.Select(pool, req, nil)
144
if h != pool[2] {
145
t.Error("Expected ip hash policy host to be the third host.")
146
}
147
148
req.RemoteAddr = "172.0.0.2"
149
- h = ipHash.Select(pool, req)
149
+ h = ipHash.Select(pool, req, nil)
150
if h != pool[2] {
151
t.Error("Expected ip hash policy host to be the third host.")
152
}
154
155
req.RemoteAddr = "172.0.0.3"
156
pool[2].SetHealthy(false)
157
- h = ipHash.Select(pool, req)
157
+ h = ipHash.Select(pool, req, nil)
158
if h != pool[0] {
159
t.Error("Expected ip hash policy host to be the first host.")
160
}
161
req.RemoteAddr = "172.0.0.4"
162
- h = ipHash.Select(pool, req)
162
+ h = ipHash.Select(pool, req, nil)
163
if h != pool[1] {
164
t.Error("Expected ip hash policy host to be the second host.")
165
}
171
{Host: new(upstreamHost)},
172
}
173
req.RemoteAddr = "172.0.0.1:80"
174
- h = ipHash.Select(pool, req)
174
+ h = ipHash.Select(pool, req, nil)
175
if h != pool[0] {
176
t.Error("Expected ip hash policy host to be the first host.")
177
}
178
req.RemoteAddr = "172.0.0.2:80"
179
- h = ipHash.Select(pool, req)
179
+ h = ipHash.Select(pool, req, nil)
180
if h != pool[1] {
181
t.Error("Expected ip hash policy host to be the second host.")
182
}
183
req.RemoteAddr = "172.0.0.3:80"
184
- h = ipHash.Select(pool, req)
184
+ h = ipHash.Select(pool, req, nil)
185
if h != pool[0] {
186
t.Error("Expected ip hash policy host to be the first host.")
187
}
188
req.RemoteAddr = "172.0.0.4:80"
189
- h = ipHash.Select(pool, req)
189
+ h = ipHash.Select(pool, req, nil)
190
if h != pool[1] {
191
t.Error("Expected ip hash policy host to be the second host.")
192
}
194
// We should get nil when there are no healthy hosts
195
pool[0].SetHealthy(false)
196
pool[1].SetHealthy(false)
197
- h = ipHash.Select(pool, req)
197
+ h = ipHash.Select(pool, req, nil)
198
if h != nil {
199
t.Error("Expected ip hash policy host to be nil.")
200
}
205
firstPolicy := new(FirstSelection)
206
req := httptest.NewRequest(http.MethodGet, "/", nil)
207
208
- h := firstPolicy.Select(pool, req)
208
+ h := firstPolicy.Select(pool, req, nil)
209
if h != pool[0] {
210
t.Error("Expected first policy host to be the first host.")
211
}
212
213
pool[0].SetHealthy(false)
214
- h = firstPolicy.Select(pool, req)
214
+ h = firstPolicy.Select(pool, req, nil)
215
if h != pool[1] {
216
t.Error("Expected first policy host to be the second host.")
217
}
222
uriPolicy := new(URIHashSelection)
223
224
request := httptest.NewRequest(http.MethodGet, "/test", nil)
225
- h := uriPolicy.Select(pool, request)
225
+ h := uriPolicy.Select(pool, request, nil)
226
if h != pool[0] {
227
t.Error("Expected uri policy host to be the first host.")
228
}
229
230
pool[0].SetHealthy(false)
231
- h = uriPolicy.Select(pool, request)
231
+ h = uriPolicy.Select(pool, request, nil)
232
if h != pool[1] {
233
t.Error("Expected uri policy host to be the first host.")
234
}
235
236
request = httptest.NewRequest(http.MethodGet, "/test_2", nil)
237
- h = uriPolicy.Select(pool, request)
237
+ h = uriPolicy.Select(pool, request, nil)
238
if h != pool[1] {
239
t.Error("Expected uri policy host to be the second host.")
240
}
247
}
248
249
request = httptest.NewRequest(http.MethodGet, "/test", nil)
250
- h = uriPolicy.Select(pool, request)
250
+ h = uriPolicy.Select(pool, request, nil)
251
if h != pool[0] {
252
t.Error("Expected uri policy host to be the first host.")
253
}
254
255
pool[0].SetHealthy(false)
256
- h = uriPolicy.Select(pool, request)
256
+ h = uriPolicy.Select(pool, request, nil)
257
if h != pool[1] {
258
t.Error("Expected uri policy host to be the first host.")
259
}
260
261
request = httptest.NewRequest(http.MethodGet, "/test_2", nil)
262
- h = uriPolicy.Select(pool, request)
262
+ h = uriPolicy.Select(pool, request, nil)
263
if h != pool[1] {
264
t.Error("Expected uri policy host to be the second host.")
265
}
266
267
pool[0].SetHealthy(false)
268
pool[1].SetHealthy(false)
269
- h = uriPolicy.Select(pool, request)
269
+ h = uriPolicy.Select(pool, request, nil)
270
if h != nil {
271
t.Error("Expected uri policy policy host to be nil.")
272
}
311
randomChoicePolicy := new(RandomChoiceSelection)
312
randomChoicePolicy.Choose = 2
313
314
- h := randomChoicePolicy.Select(pool, request)
314
+ h := randomChoicePolicy.Select(pool, request, nil)
315
316
if h == nil {
317
t.Error("RandomChoicePolicy should not return nil")
322
}
323
324
}
325
+
326
+func TestCookieHashPolicy(t *testing.T) {
327
+ pool := testPool()
328
+ pool[0].Dial = "localhost:8080"
329
+ pool[1].Dial = "localhost:8081"
330
+ pool[2].Dial = "localhost:8082"
331
+ pool[0].SetHealthy(true)
332
+ pool[1].SetHealthy(false)
333
+ pool[2].SetHealthy(false)
334
+ request := httptest.NewRequest(http.MethodGet, "/test", nil)
335
+ w := httptest.NewRecorder()
336
+ cookieHashPolicy := new(CookieHashSelection)
337
+ h := cookieHashPolicy.Select(pool, request, w)
338
+ cookie_server1 := w.Result().Cookies()[0]
339
+ if cookie_server1 == nil {
340
+ t.Error("cookieHashPolicy should set a cookie")
341
+ }
342
+ if cookie_server1.Name != "lb" {
343
+ t.Error("cookieHashPolicy should set a cookie with name lb")
344
+ }
345
+ if h != pool[0] {
346
+ t.Error("Expected cookieHashPolicy host to be the first only available host.")
347
+ }
348
+ pool[1].SetHealthy(true)
349
+ pool[2].SetHealthy(true)
350
+ request = httptest.NewRequest(http.MethodGet, "/test", nil)
351
+ w = httptest.NewRecorder()
352
+ request.AddCookie(cookie_server1)
353
+ h = cookieHashPolicy.Select(pool, request, w)
354
+ if h != pool[0] {
355
+ t.Error("Expected cookieHashPolicy host to stick to the first host (matching cookie).")
356
+ }
357
+ s := w.Result().Cookies()
358
+ if len(s) != 0 {
359
+ t.Error("Expected cookieHashPolicy to not set a new cookie.")
360
+ }
361
+ pool[0].SetHealthy(false)
362
+ request = httptest.NewRequest(http.MethodGet, "/test", nil)
363
+ w = httptest.NewRecorder()
364
+ request.AddCookie(cookie_server1)
365
+ h = cookieHashPolicy.Select(pool, request, w)
366
+ if h == pool[0] {
367
+ t.Error("Expected cookieHashPolicy to select a new host.")
368
+ }
369
+ if w.Result().Cookies() == nil {
370
+ t.Error("Expected cookieHashPolicy to set a new cookie.")
371
+ }
372
+}