summaryrefslogtreecommitdiff
path: root/test/retry.js
blob: f76c7d8a0ef8db07ed21b2479e784254054aac9f (plain)
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
var async = require('../lib');
var {expect} = require('chai');
var assert = require('assert');
var _ = require('lodash');

describe("retry", () => {

    // Issue 306 on github: https://github.com/caolan/async/issues/306
    it('retry when attempt succeeds',(done) => {
        var failed = 3;
        var callCount = 0;
        var expectedResult = 'success';
        function fn(callback) {
            callCount++;
            failed--;
            if (!failed) callback(null, expectedResult);
            else callback(true); // respond with error
        }
        async.retry(fn, (err, result) => {
            assert(err === null, err + " passed instead of 'null'");
            assert.equal(callCount, 3, 'did not retry the correct number of times');
            assert.equal(result, expectedResult, 'did not return the expected result');
            done();
        });
    });

    it('retry when all attempts fail',(done) => {
        var times = 3;
        var callCount = 0;
        var error = 'ERROR';
        var erroredResult = 'RESULT';
        function fn(callback) {
            callCount++;
            callback(error + callCount, erroredResult + callCount); // respond with indexed values
        }
        async.retry(times, fn, (err, result) => {
            assert.equal(callCount, 3, "did not retry the correct number of times");
            assert.equal(err, error + times, "Incorrect error was returned");
            assert.equal(result, erroredResult + times, "Incorrect result was returned");
            done();
        });
    });

    it('retry fails with invalid arguments',(done) => {
        expect(() => {
            async.retry("");
        }).to.throw();
        expect(() => {
            async.retry();
        }).to.throw();
        expect(() => {
            async.retry(() => {}, 2, () => {});
        }).to.throw();
        done();
    });

    it('retry with interval when all attempts fail',function(done) {
        this.retries(3); // this test is flakey due to timing issues

        var times = 3;
        var interval = 50;
        var callCount = 0;
        var error = 'ERROR';
        var erroredResult = 'RESULT';
        function fn(callback) {
            callCount++;
            callback(error + callCount, erroredResult + callCount); // respond with indexed values
        }
        var start = Date.now();
        async.retry({ times, interval}, fn, (err, result) => {
            var duration = Date.now() - start;
            expect(duration).to.be.above(interval * (times - 1) - times);
            assert.equal(callCount, 3, "did not retry the correct number of times");
            assert.equal(err, error + times, "Incorrect error was returned");
            assert.equal(result, erroredResult + times, "Incorrect result was returned");
            done();
        });
    });

    it('retry with custom interval when all attempts fail',(done) => {
        var times = 3;
        var retryCounts = []
        var intervalFunc = function(retryCount) {
            retryCounts.push(retryCount)
            return retryCount * 100;
        };
        var callCount = 0;
        var error = 'ERROR';
        var erroredResult = 'RESULT';
        function fn(callback) {
            callCount++;
            callback(error + callCount, erroredResult + callCount); // respond with indexed values
        }
        var start = Date.now();
        async.retry({ times, interval: intervalFunc}, fn, (err, result) => {
            var duration = Date.now() - start;
            expect(duration).to.be.above(300 - times);
            assert.equal(callCount, 3, "did not retry the correct number of times");
            assert.equal(err, error + times, "Incorrect error was returned");
            assert.equal(result, erroredResult + times, "Incorrect result was returned");
            assert.deepEqual(retryCounts, [1, 2])
            done();
        });
    });

    it("should not require a callback", (done) => {
        var called = false;
        async.retry(3, (cb) => {
            called = true;
            cb();
        });
        setTimeout(() => {
            assert(called);
            done();
        }, 10);
    });

    it("should not require a callback and use the default times", (done) => {
        var calls = 0;
        async.retry((cb) => {
            calls++;
            cb("fail");
        }).catch(() => {});
        setTimeout(() => {
            expect(calls).to.equal(5);
            done();
        }, 100);
    });

    it("should be cancelable", (done) => {
        var calls = 0;
        async.retry(2, (cb) => {
            calls++;
            cb(calls > 1 ? false : 'fail');
        }, () => { throw new Error('should not get here') });
        setTimeout(() => {
            expect(calls).to.equal(2);
            done();
        }, 10);
    });

    it('retry does not precompute the intervals (#1226)', (done) => {
        var callTimes = [];
        function intervalFunc() {
            callTimes.push(Date.now());
            return 100;
        }
        function fn(callback) {
            callback({}); // respond with indexed values
        }
        async.retry({ times: 4, interval: intervalFunc}, fn, () => {
            expect(callTimes[1] - callTimes[0]).to.be.above(90);
            expect(callTimes[2] - callTimes[1]).to.be.above(90);
            done();
        });
    });

    it('retry passes all resolve arguments to callback', (done) => {
        function fn(callback) {
            callback(null, 1, 2, 3); // respond with indexed values
        }
        async.retry(5, fn, _.rest((args) => {
            expect(args).to.be.eql([null, 1, 2, 3]);
            done();
        }));
    });

    // note this is a synchronous test ensuring retry is synchrnous in the fastest (most straightforward) case
    it('retry calls fn immediately and will call callback if successful', () => {
        function fn(callback) {
            callback(null, {a: 1});
        }
        async.retry(5, fn, (err, result) => {
            expect(result).to.be.eql({a: 1});
        });
    });

    it('retry when all attempts fail and error continue test returns true',(done) => {
        var times = 3;
        var callCount = 0;
        var error = 'ERROR';
        var special = 'SPECIAL_ERROR';
        var erroredResult = 'RESULT';
        function fn(callback) {
            callCount++;
            callback(error + callCount, erroredResult + callCount);
        }
        function errorTest(err) {
            return err && err !== special;
        }
        var options = {
            times,
            errorFilter: errorTest
        };
        async.retry(options, fn, (err, result) => {
            assert.equal(callCount, 3, "did not retry the correct number of times");
            assert.equal(err, error + times, "Incorrect error was returned");
            assert.equal(result, erroredResult + times, "Incorrect result was returned");
            done();
        });
    });

    it('retry when some attempts fail and error test returns false at some invokation',(done) => {
        var callCount = 0;
        var error = 'ERROR';
        var special = 'SPECIAL_ERROR';
        var erroredResult = 'RESULT';
        function fn(callback) {
            callCount++;
            var err = callCount === 2 ? special : error + callCount;
            callback(err, erroredResult + callCount);
        }
        function errorTest(err) {
            return err && err === error + callCount; // just a different pattern
        }
        var options = {
            errorFilter: errorTest
        };
        async.retry(options, fn, (err, result) => {
            assert.equal(callCount, 2, "did not retry the correct number of times");
            assert.equal(err, special, "Incorrect error was returned");
            assert.equal(result, erroredResult + 2, "Incorrect result was returned");
            done();
        });
    });

    it('retry with interval when some attempts fail and error test returns false at some invokation',function(done) {
        this.retries(3); // flakey test

        var interval = 50;
        var callCount = 0;
        var error = 'ERROR';
        var erroredResult = 'RESULT';
        var special = 'SPECIAL_ERROR';
        var specialCount = 3;
        function fn(callback) {
            callCount++;
            var err = callCount === specialCount ? special : error + callCount;
            callback(err, erroredResult + callCount);
        }
        function errorTest(err) {
            return err && err !== special;
        }
        var start = Date.now();
        async.retry({ interval, errorFilter: errorTest }, fn, (err, result) => {
            var duration = Date.now() - start;
            expect(duration).to.be.above(interval * (specialCount - 1) - specialCount);
            assert.equal(callCount, specialCount, "did not retry the correct number of times");
            assert.equal(err, special, "Incorrect error was returned");
            assert.equal(result, erroredResult + specialCount, "Incorrect result was returned");
            done();
        });
    });

    it('retry when first attempt succeeds and error test should not be called',(done) => {
        var callCount = 0;
        var error = 'ERROR';
        var erroredResult = 'RESULT';
        var continueTestCalled = false;
        function fn(callback) {
            callCount++;
            callback(null, erroredResult + callCount);
        }
        function errorTest(err) {
            continueTestCalled = true;
            return err && err === error;
        }
        var options = {
            errorFilter: errorTest
        };
        async.retry(options, fn, _.rest((args) => {
            assert.equal(callCount, 1, "did not retry the correct number of times");
            expect(args).to.be.eql([null, erroredResult + callCount]);
            assert.equal(continueTestCalled, false, "error test function was called");
            done();
        }));
    });
});