bluebird.test.js
8.3 KB
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
var chai = require('chai')
, expect = chai.expect
, Promise = require('bluebird')
, moment = require('moment')
, sinon = require('sinon')
, sinonChai = require('sinon-chai')
, retry = require('../');
chai.use(require('chai-as-promised'));
require('sinon-as-promised')(Promise);
describe('bluebird', function () {
var count
, soRejected
, soResolved;
beforeEach(function () {
count = 0;
soRejected = Math.random().toString();
soResolved = Math.random().toString();
});
it('should reject immediately if max is 1 (using options)', function () {
var callback = sinon.stub();
callback.resolves(soResolved);
callback.onCall(0).rejects(soRejected);
return expect(retry(callback, {max: 1, backoffBase: 0})).to.eventually.be.rejectedWith(soRejected).then(function () {
expect(callback.callCount).to.equal(1);
});
});
it('should reject immediately if max is 1 (using integer)', function () {
var callback = sinon.stub();
callback.resolves(soResolved);
callback.onCall(0).rejects(soRejected);
return expect(retry(callback, 1)).to.eventually.be.rejectedWith(soRejected).then(function () {
expect(callback.callCount).to.equal(1);
});
});
it('should reject after all tries if still rejected', function () {
var callback = sinon.stub();
callback.rejects(soRejected);
return expect(retry(callback, {max: 3, backoffBase: 0})).to.eventually.be.rejectedWith(soRejected).then(function () {
expect(callback.firstCall.args).to.deep.equal([{ current: 1 }]);
expect(callback.secondCall.args).to.deep.equal([{ current: 2 }]);
expect(callback.thirdCall.args).to.deep.equal([{ current: 3 }]);
expect(callback.callCount).to.equal(3);
});
});
it('should resolve immediately if resolved on first try', function () {
var callback = sinon.stub();
callback.resolves(soResolved);
callback.onCall(0).resolves(soResolved);
return expect(retry(callback, {max: 10, backoffBase: 0})).to.eventually.equal(soResolved).then(function () {
expect(callback.callCount).to.equal(1);
});
});
it('should resolve if resolved before hitting max', function () {
var callback = sinon.stub();
callback.rejects(soRejected);
callback.onCall(3).resolves(soResolved);
return expect(retry(callback, {max: 10, backoffBase: 0})).to.eventually.equal(soResolved).then(function () {
expect(callback.firstCall.args).to.deep.equal([{ current: 1 }]);
expect(callback.secondCall.args).to.deep.equal([{ current: 2 }]);
expect(callback.thirdCall.args).to.deep.equal([{ current: 3 }]);
expect(callback.callCount).to.equal(4);
});
});
describe('timeout', function () {
it('should throw if reject on first attempt', function () {
return expect(retry(function () {
return Promise.delay(2000);
}, {
max: 1,
backoffBase: 0,
timeout: 1000
})).to.eventually.be.rejectedWith(Promise.TimeoutError);
});
it('should throw if reject on last attempt', function () {
return expect(retry(function () {
count++;
if (count === 3) {
return Promise.delay(3500);
}
return Promise.reject();
}, {
max: 3,
backoffBase: 0,
timeout: 1500
})).to.eventually.be.rejectedWith(Promise.TimeoutError).then(function () {
expect(count).to.equal(3);
});
});
});
describe('match', function () {
it('should continue retry while error is equal to match string', function () {
var callback = sinon.stub();
callback.rejects(soRejected);
callback.onCall(3).resolves(soResolved);
return expect(retry(callback, {max: 15, backoffBase: 0, match: 'Error: ' + soRejected})).to.eventually.equal(soResolved).then(function () {
expect(callback.callCount).to.equal(4);
});
});
it('should reject immediately if error is not equal to match string', function () {
var callback = sinon.stub();
callback.rejects(soRejected);
return expect(retry(callback, {max: 15, backoffBase: 0, match: 'A custom error string'})).to.eventually.be.rejectedWith(soRejected).then(function () {
expect(callback.callCount).to.equal(1);
});
});
it('should continue retry while error is instanceof match', function () {
var callback = sinon.stub();
callback.rejects(soRejected);
callback.onCall(4).resolves(soResolved);
return expect(retry(callback, {max: 15, backoffBase: 0, match: Error})).to.eventually.equal(soResolved).then(function () {
expect(callback.callCount).to.equal(5);
});
});
it('should reject immediately if error is not instanceof match', function () {
var callback = sinon.stub();
callback.rejects(soRejected);
return expect(retry(callback, {max: 15, backoffBase: 0, match: function foo(){}})).to.eventually.be.rejectedWith(Error).then(function () {
expect(callback.callCount).to.equal(1);
});
});
it('should continue retry while error is equal to match string in array', function () {
var callback = sinon.stub();
callback.rejects(soRejected);
callback.onCall(4).resolves(soResolved);
return expect(retry(callback, {max: 15, backoffBase: 0, match: ['Error: ' + (soRejected + 1), 'Error: ' + soRejected]})).to.eventually.equal(soResolved).then(function () {
expect(callback.callCount).to.equal(5);
});
});
it('should reject immediately if error is not equal to match string in array', function () {
var callback = sinon.stub();
callback.rejects(soRejected);
return expect(retry(callback, {max: 15, backoffBase: 0, match: ['Error: ' + (soRejected + 1), 'Error: ' + (soRejected + 2)]})).to.eventually.be.rejectedWith(Error).then(function () {
expect(callback.callCount).to.equal(1);
});
});
it('should reject immediately if error is not instanceof match in array', function () {
var callback = sinon.stub();
callback.rejects(soRejected);
return expect(retry(callback, {max: 15, backoffBase: 0, match: ['Error: ' + (soRejected + 1), function foo(){}]})).to.eventually.be.rejectedWith(Error).then(function () {
expect(callback.callCount).to.equal(1);
});
});
it('should continue retry while error is instanceof match in array', function () {
var callback = sinon.stub();
callback.rejects(soRejected);
callback.onCall(4).resolves(soResolved);
return expect(retry(callback, {max: 15, backoffBase: 0, match: ['Error: ' + (soRejected + 1), Error]})).to.eventually.equal(soResolved).then(function () {
expect(callback.callCount).to.equal(5);
});
});
});
describe('backoff', function () {
it('should resolve after 5 retries and an eventual delay over 1800ms using default backoff', function () {
var startTime = moment();
var callback = sinon.stub();
callback.rejects(soRejected);
callback.onCall(5).resolves(soResolved);
return expect(retry(callback, {max: 15})).to.eventually.equal(soResolved).then(function () {
expect(callback.callCount).to.equal(6);
expect(moment().diff(startTime)).to.be.above(1800);
expect(moment().diff(startTime)).to.be.below(3400);
});
});
it('should resolve after 1 retry and initial delay equal to the backoffBase', function() {
var initialDelay = 100;
var callback = sinon.stub();
var startTime = moment();
callback.onCall(0).rejects(soRejected);
callback.onCall(1).resolves(soResolved);
return expect(retry(callback, { max: 2, backoffBase: initialDelay, backoffExponent: 3 }))
.to.eventually.equal(soResolved)
.then(function() {
expect(callback.callCount).to.equal(2);
expect(moment().diff(startTime)).to.be.within(initialDelay, initialDelay + 50); // allow for some overhead
});
});
it('should throw TimeoutError and cancel backoff delay if timeout is reached', function () {
return expect(retry(function () {
return Promise.delay(2000);
}, {
max: 15,
timeout: 1000
})).to.eventually.be.rejectedWith(Promise.TimeoutError);
});
});
});