-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.js
355 lines (275 loc) · 11.2 KB
/
test.js
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
/*globals it, describe, beforeEach, afterEach */
var expect = require('expect.js');
var Sinon = require('sinon');
var DEP = require('./');
describe('DNS Endpoint Pool', function () {
var stubs = [];
var clock;
function autoRestore(stub) {
stubs.push(stub);
return stub;
}
beforeEach(function () {
clock = autoRestore(Sinon.useFakeTimers());
});
afterEach(function () {
var stub;
while ((stub = stubs.pop())) {
stub.restore();
}
});
it('enforces that all arguments are passed to the constructor', function () {
autoRestore(Sinon.stub(DEP.prototype, 'update'));
[
function () { return new DEP(); },
function () { return new DEP('foo.localhost'); }
].forEach(function (fn) {
expect(fn).to.throwError('Must supply all arguments');
});
});
it('will automatically begin updating when constructed', function () {
/*eslint no-unused-vars: 0 */
var stub = autoRestore(Sinon.stub(DEP.prototype, 'update'));
var dep = new DEP('foo.localhost', 5000);
Sinon.assert.calledOnce(stub);
});
it('will execute a callback after the first update', function () {
var resolve = autoRestore(Sinon.stub(DEP.prototype, 'resolve'));
var called = false;
resolve.callsArgWith(0, null, []);
var dep = new DEP('foo.localhost', 5000, null, function () {
called = true;
});
expect(called).to.be(true);
dep.stopUpdating();
});
it('will update on a timer', function () {
var resolve = autoRestore(Sinon.stub(DEP.prototype, 'resolve'));
resolve.callsArgWith(0, null, []);
var dep = new DEP('foo.localhost', 5000);
Sinon.assert.calledOnce(resolve);
clock.tick(5000);
Sinon.assert.calledTwice(resolve);
clock.tick(5000);
Sinon.assert.calledThrice(resolve);
dep.stopUpdating();
});
it('will add resolved endpoints and serve them in rotation', function () {
var resolve = autoRestore(Sinon.stub(DEP.prototype, 'resolve'));
resolve.callsArgWith(0, null, [
{ name: 'bar.localhost', port: 8000 },
{ name: 'baz.localhost', port: 8001 }
]);
var dep = new DEP('foo.localhost', 5000);
expect(dep.getEndpoint().url).to.be('bar.localhost:8000');
expect(dep.getEndpoint().url).to.be('baz.localhost:8001');
dep.stopUpdating();
});
it('will trigger an error if resolving fails', function () {
var resolve = autoRestore(Sinon.stub(DEP.prototype, 'resolve'));
resolve
.onFirstCall().callsArgWith(0, null, [
{ name: 'bar.localhost', port: 8000 },
{ name: 'baz.localhost', port: 8001 }
])
.onSecondCall().callsArgWith(0, { error: true });
var dep = new DEP('foo.localhost', 5000);
expect(dep.getEndpoint().url).to.be('bar.localhost:8000');
var errorData;
dep.on('updateError', function (err) {
errorData = err;
});
clock.tick(5000);
expect(errorData).to.eql({ error: true });
// but it reuses the previous hosts
expect(dep.getEndpoint().url).to.be('baz.localhost:8001');
});
it('will update with new endpoints returned', function () {
var resolve = autoRestore(Sinon.stub(DEP.prototype, 'resolve'));
resolve
.onFirstCall().callsArgWith(0, null, [
{ name: 'bar.localhost', port: 8000 },
{ name: 'baz.localhost', port: 8001 }
])
.onSecondCall().callsArgWith(0, null, [
{ name: 'baz.localhost', port: 8001 },
{ name: 'quux.localhost', port: 8002 }
]);
var dep = new DEP('foo.localhost', 5000);
dep.getEndpoint(); // bar
var bazEndpoint = dep.getEndpoint();
expect(bazEndpoint.url).to.be('baz.localhost:8001');
clock.tick(5000);
expect(dep.getEndpoint()).to.be(bazEndpoint);
expect(dep.getEndpoint().url).to.be('quux.localhost:8002');
dep.stopUpdating();
});
it('can query the state of endpoints', function () {
var resolve = autoRestore(Sinon.stub(DEP.prototype, 'resolve'));
resolve
.onFirstCall().callsArgWith(0, { error: true })
.onSecondCall().callsArgWith(0, null, [ { name: 'bar.localhost', port: 8000 } ])
.onThirdCall().callsArgWith(0, { error: true });
var dep = new DEP('foo.localhost', 5000);
expect(dep.hasEndpoints()).to.be(false);
clock.tick(5000);
expect(dep.hasEndpoints()).to.be(true);
clock.tick(5000);
// should still have old endpoints
expect(dep.hasEndpoints()).to.be(true);
});
describe('with eject-on-error pool management', function () {
it('enforces that config object has proper shape', function () {
autoRestore(Sinon.stub(DEP.prototype, 'update'));
[
function () { return new DEP('foo.localhost', 5000, { maxFailures: 2 }); },
function () { return new DEP('foo.localhost', 5000, { maxFailures: 2, failureWindow: 10000 }); },
function () { return new DEP('foo.localhost', 5000, { failureRate: 0.5 }); },
function () { return new DEP('foo.localhost', 5000, { failureRateWindow: 10 }); }
].forEach(function (fn) {
expect(fn).to.throwError('Must supply either configuration to ejectOnErrorPoolManager');
});
});
describe('using time-based rolling window', function () {
var ejectOnErrorConfig = {
maxFailures: 2,
failureWindow: 10000,
resetTimeout: 10000
};
it('will remove endpoints from the pool if they fail', function () {
var resolve = autoRestore(Sinon.stub(DEP.prototype, 'resolve'));
resolve.callsArgWith(0, null, [
{ name: 'bar.localhost', port: 8000 },
{ name: 'baz.localhost', port: 8001 }
]);
var dep = new DEP('foo.localhost', 5000, ejectOnErrorConfig);
var barEndpoint = dep.getEndpoint();
barEndpoint.callback(true);
var bazEndpoint = dep.getEndpoint();
expect(dep.getEndpoint()).to.be(barEndpoint); // still in the pool
barEndpoint.callback(true);
expect(dep.getEndpoint()).to.be(bazEndpoint);
expect(dep.getEndpoint()).to.be(bazEndpoint); // bar is removed
var status = dep.getStatus();
expect(status.total).to.be(2);
expect(status.unhealthy).to.be(1);
expect(status.age).to.be.a('number');
dep.stopUpdating();
});
it('will reinstate endpoints for a single request after a timeout', function () {
var resolve = autoRestore(Sinon.stub(DEP.prototype, 'resolve'));
resolve.callsArgWith(0, null, [
{ name: 'bar.localhost', port: 8000 },
{ name: 'baz.localhost', port: 8001 }
]);
var dep = new DEP('foo.localhost', 5000, ejectOnErrorConfig);
var barEndpoint = dep.getEndpoint();
barEndpoint.callback(true);
barEndpoint.callback(true); // removed from pool.
var bazEndpoint = dep.getEndpoint();
clock.tick(10000);
expect(dep.getEndpoint()).to.be(barEndpoint);
expect(dep.getEndpoint()).to.be(bazEndpoint);
expect(dep.getEndpoint()).to.be(bazEndpoint); // only return barEndpoint once
barEndpoint.callback(null); // denotes success
expect(dep.getEndpoint()).to.be(barEndpoint); // it's back in the game
dep.stopUpdating();
});
it('reports the age of the endpoints when updates fail', function () {
var resolve = autoRestore(Sinon.stub(DEP.prototype, 'resolve'));
var errorHandler = Sinon.spy();
var errObj = { error: true };
// works on the first and fourth calls, fails every other time
resolve
.callsArgWith(0, errObj)
.onFirstCall().callsArgWith(0, null, [
{ name: 'bar.localhost', port: 8000 },
{ name: 'baz.localhost', port: 8001 }
])
.onCall(3).callsArgWith(0, null, [
{ name: 'bar.localhost', port: 8000 },
{ name: 'baz.localhost', port: 8001 }
]);
var dep = new DEP('foo.localhost', 5000, ejectOnErrorConfig); // call 1
dep.on('updateError', errorHandler);
clock.tick(5000); // call 2
clock.tick(5000); // call 3
clock.tick(5000); // call 4, should reset the timer
clock.tick(5000); // call 5
Sinon.assert.calledThrice(errorHandler);
expect(errorHandler.firstCall.calledWithExactly(errObj, 5000)).to.be(true);
expect(errorHandler.secondCall.calledWithExactly(errObj, 10000)).to.be(true);
expect(errorHandler.thirdCall.calledWithExactly(errObj, 5000)).to.be(true);
dep.stopUpdating();
});
});
describe('using percentage error rates', function () {
var ejectOnErrorConfig = {
failureRate: 1,
failureRateWindow: 2,
resetTimeout: 10000
};
it('will remove endpoints from the pool if they fail', function () {
var resolve = autoRestore(Sinon.stub(DEP.prototype, 'resolve'));
resolve.callsArgWith(0, null, [
{ name: 'bar.localhost', port: 8000 },
{ name: 'baz.localhost', port: 8001 }
]);
var dep = new DEP('foo.localhost', 5000, ejectOnErrorConfig);
var barEndpoint = dep.getEndpoint();
barEndpoint.callback(true);
var bazEndpoint = dep.getEndpoint();
expect(dep.getEndpoint()).to.be(barEndpoint); // still in the pool
barEndpoint.callback(true);
expect(dep.getEndpoint()).to.be(bazEndpoint);
expect(dep.getEndpoint()).to.be(bazEndpoint); // bar is removed
var status = dep.getStatus();
expect(status.total).to.be(2);
expect(status.unhealthy).to.be(1);
expect(status.age).to.be.a('number');
dep.stopUpdating();
});
it('will remove an endpoint from the pool once it reaches its error threshold', function () {
var resolve = autoRestore(Sinon.stub(DEP.prototype, 'resolve'));
resolve.callsArgWith(0, null, [
{ name: 'bar.localhost', port: 8000 }
]);
var dep = new DEP('foo.localhost', 5000, {
failureRate: 0.75,
failureRateWindow: 4,
resetTimeout: 10000
});
var endpoint = dep.getEndpoint();
endpoint.callback(true);
endpoint = dep.getEndpoint();
endpoint.callback(true);
endpoint = dep.getEndpoint();
endpoint.callback(true);
expect(dep.getEndpoint()).to.be(null);
});
it('will reinstate endpoints for a single request after a timeout', function () {
var resolve = autoRestore(Sinon.stub(DEP.prototype, 'resolve'));
resolve.callsArgWith(0, null, [
{ name: 'bar.localhost', port: 8000 },
{ name: 'baz.localhost', port: 8001 }
]);
var dep = new DEP('foo.localhost', 5000, {
failureRate: 1,
failureRateWindow: 2,
resetTimeout: 10000
});
var barEndpoint = dep.getEndpoint();
barEndpoint.callback(true);
barEndpoint.callback(true); // removed from pool.
var bazEndpoint = dep.getEndpoint();
clock.tick(10000);
expect(dep.getEndpoint()).to.be(barEndpoint);
expect(dep.getEndpoint()).to.be(bazEndpoint);
expect(dep.getEndpoint()).to.be(bazEndpoint); // only return barEndpoint once
barEndpoint.callback(null); // denotes success
expect(dep.getEndpoint()).to.be(barEndpoint); // it's back in the game
dep.stopUpdating();
});
});
});
});