forked from googleapis/retry-request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
297 lines (237 loc) · 7.27 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
'use strict';
var assert = require('assert');
var async = require('async');
var range = require('lodash.range');
var through = require('through2');
var retryRequest = require('./index.js');
describe('retry-request', function () {
var URI_404 = 'http://yahoo.com/theblahstore';
var URI_200 = 'http://yahoo.com/';
var URI_NON_EXISTENT = 'http://theblahstore';
describe('streams', function () {
it('works with defaults in a stream', function (done) {
var responsesEmitted = 0;
retryRequest(URI_404)
.on('error', done)
.on('response', function () {
responsesEmitted++;
})
.on('complete', function () {
assert.strictEqual(responsesEmitted, 1);
done();
});
});
it('allows object mode', function () {
var retryStream = retryRequest(URI_404, { objectMode: true });
assert.strictEqual(retryStream._readableState.objectMode, true);
});
it('emits an error', function (done) {
retryRequest(URI_NON_EXISTENT)
.on('error', function () {
done();
});
});
it('exposes an `abort` fuction to match request', function (done) {
var retryStream = retryRequest(URI_NON_EXISTENT);
retryStream.on('error', function () {
assert.equal(typeof retryStream.abort, 'function');
done();
});
});
it('works on the last attempt', function (done) {
var numAborts = 0;
var numAttempts = 0;
var opts = {
request: function () {
numAttempts++;
var fakeRequestStream = through();
fakeRequestStream.abort = function () {
numAborts++;
};
var shouldReturnError = numAttempts < 3;
var response = shouldReturnError ? { statusCode: 503 } : { statusCode: 200 };
setImmediate(function () {
fakeRequestStream.emit('response', response);
if (shouldReturnError) {
return;
}
setImmediate(function () {
fakeRequestStream.emit('complete', numAttempts);
});
});
return fakeRequestStream;
}
};
retryRequest(URI_404, opts)
.on('error', done)
.on('complete', function (numAttempts) {
assert.strictEqual(numAborts, 2);
assert.deepEqual(numAttempts, 3);
done();
});
});
it('never succeeds', function (done) {
var numAborts = 0;
var numAttempts = 0;
var opts = {
request: function () {
numAttempts++;
var fakeRequestStream = through();
fakeRequestStream.abort = function () {
numAborts++;
};
var response = { statusCode: 503 };
setImmediate(function () {
fakeRequestStream.emit('response', response);
});
return fakeRequestStream;
}
};
retryRequest(URI_404, opts)
.on('response', function () {
assert.strictEqual(numAborts, 2);
assert.strictEqual(numAttempts, 3);
done();
})
.on('error', done);
});
});
describe('callbacks', function () {
it('works with defaults with a callback', function (done) {
retryRequest(URI_404, function () {
done();
});
});
it('exposes an `abort` function', function (done) {
var opts = {
request: function () {
return {
abort: done
};
}
};
var request = retryRequest(URI_200, opts, assert.ifError);
request.abort();
});
it('returns an error', function (done) {
retryRequest(URI_NON_EXISTENT, function (err) {
assert.equal(typeof err, 'object');
done();
});
});
});
describe('overriding', function () {
it('should allow overriding retries', function (done) {
var opts = { retries: 0 };
retryRequest(URI_404, opts, function () {
done();
});
});
it('should allow overriding shouldRetryFn', function (done) {
var shouldRetryFnCalled = false;
var opts = {
retries: 1, // so that our retry function is only called once
shouldRetryFn: function () {
shouldRetryFnCalled = true;
return true;
}
};
retryRequest(URI_404, opts, function () {
assert.strictEqual(shouldRetryFnCalled, true);
done();
});
});
it('should allow overriding request', function (done) {
var opts = {
request: function () {
done();
}
};
retryRequest(URI_200, opts, function () {});
});
});
describe('shouldRetryFn', function() {
var URI = 'http://';
function assertRetried(statusCode, callback) {
var initialRequestMade = false;
retryRequest(URI, {
request: function(_, responseHandler) {
if (initialRequestMade) {
// This is a retry attempt. "Test passed"
callback();
return;
}
initialRequestMade = true;
responseHandler(null, { statusCode: statusCode });
}
}, assert.ifError);
}
function assertNotRetried(statusCode, callback) {
var initialRequestMade = false;
var requestWasRetried = false;
retryRequest(URI, {
request: function(_, responseHandler) {
requestWasRetried = initialRequestMade;
initialRequestMade = true;
responseHandler(null, { statusCode: statusCode });
}
}, function(err) {
if (err) {
callback(err);
return;
}
if (requestWasRetried) {
callback(new Error('Request was retried'));
return;
}
callback();
});
}
it('should retry a 1xx code', function(done) {
async.each(range(100, 199), assertRetried, done);
});
it('should not retry a 2xx code', function(done) {
async.each(range(200, 299), assertNotRetried, done);
});
it('should not retry a 3xx code', function(done) {
async.each(range(300, 399), assertNotRetried, done);
});
it('should not retry a 4xx code', function(done) {
var statusCodes = range(400, 428).concat(range(430, 499));
async.each(statusCodes, assertNotRetried, done);
});
it('should retry a 429 code', function(done) {
assertRetried(429, done);
});
it('should retry a 5xx code', function(done) {
async.each(range(500, 599), assertRetried, done);
});
});
it('should not do any retries if unnecessary', function (done) {
var shouldRetryFnTimesCalled = 0;
var opts = {
shouldRetryFn: function () {
shouldRetryFnTimesCalled++;
return false;
}
};
retryRequest(URI_200, opts, function () {
assert.strictEqual(shouldRetryFnTimesCalled, 1);
done();
});
});
});
describe('getNextRetryDelay', function () {
function secs(seconds) {
return seconds * 1000;
}
it('should return exponential retry delay', function () {
[1, 2, 3, 4, 5].forEach(assertTime);
function assertTime(retryNumber) {
var min = (Math.pow(2, retryNumber) * secs(1));
var max = (Math.pow(2, retryNumber) * secs(1)) + secs(1);
var time = retryRequest.getNextRetryDelay(retryNumber);
assert(time >= min && time <= max);
}
});
});