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

describe("retry", function () {

    // Issue 306 on github: https://github.com/caolan/async/issues/306
    it('retry when attempt succeeds',function(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, function(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',function(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, function(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',function(done) {
        expect(function() {
            async.retry("");
        }).to.throw();
        expect(function() {
            async.retry();
        }).to.throw();
        expect(function() {
            async.retry(function() {}, 2, function() {});
        }).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 = new Date().getTime();
        async.retry({ times: times, interval: interval}, fn, function(err, result){
            var now = new Date().getTime();
            var duration = now - start;
            assert(duration >= (interval * (times -1)),  'did not include interval');
            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',function(done) {
        var times = 3;
        var intervalFunc = function(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 = new Date().getTime();
        async.retry({ times: times, interval: intervalFunc}, fn, function(err, result){
            var now = new Date().getTime();
            var duration = now - start;
            assert(duration >= 300,  'did not include custom interval');
            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("should not require a callback", function (done) {
        var called = false;
        async.retry(3, function(cb) {
            called = true;
            cb();
        });
        setTimeout(function () {
            assert(called);
            done();
        }, 10);
    });

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

    it('retry does not precompute the intervals (#1226)', function(done) {
        var callTimes = [];
        function intervalFunc() {
            callTimes.push(new Date().getTime());
            return 100;
        };
        function fn(callback) {
            callback({}); // respond with indexed values
        }
        async.retry({ times: 4, interval: intervalFunc}, fn, function(){
            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', function(done) {
        function fn(callback) {
            callback(null, 1, 2, 3); // respond with indexed values
        }
        async.retry(5, fn, _.rest(function(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() {
        function fn(callback) {
            callback(null, {a: 1});
        }
        async.retry(5, fn, function(err, result) {
            expect(result).to.be.eql({a: 1});
        });
    });

    it('retry when all attempts fail and error continue test returns true',function(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: times,
            errorFilter: errorTest
        };
        async.retry(options, fn, function(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',function(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, function(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 = new Date().getTime();
        async.retry({ interval: interval, errorFilter: errorTest }, fn, function(err, result){
            var now = new Date().getTime();
            var duration = now - start;
            assert(duration >= (interval * (specialCount - 1)),  'did not include interval');
            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',function(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(function(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();
        }));
    });
});