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

describe('retryable', () => {
    it('basics', (done) => {
        var calls = 0;
        var retryableTask = async.retryable(3, (arg, cb) => {
            calls++;
            expect(arg).to.equal(42);
            cb('fail');
        });

        retryableTask(42, (err) => {
            expect(err).to.equal('fail');
            expect(calls).to.equal(3);
            done();
        });
    });

    it('basics with error test function', (done) => {
        var calls = 0;
        var special = 'special';
        var opts = {
            errorFilter(err) {
                return err == special;
            }
        };
        var retryableTask = async.retryable(opts, (arg, cb) => {
            calls++;
            expect(arg).to.equal(42);
            cb(calls === 3 ? 'fail' : special);
        });

        retryableTask(42, (err) => {
            expect(err).to.equal('fail');
            expect(calls).to.equal(3);
            done();
        });
    });

    it('should work as an embedded task', (done) => {
        var retryResult = 'RETRY';
        var fooResults;
        var retryResults;

        async.auto({
            dep: async.constant('dep'),
            foo: ['dep', function(results, callback){
                fooResults = results;
                callback(null, 'FOO');
            }],
            retry: ['dep', async.retryable((results, callback) => {
                retryResults = results;
                callback(null, retryResult);
            })]
        }, (err, results) => {
            assert.equal(results.retry, retryResult, "Incorrect result was returned from retry function");
            assert.equal(fooResults, retryResults, "Incorrect results were passed to retry function");
            done();
        });
    });

    it('should work as an embedded task with interval', (done) => {
        var start = new Date().getTime();
        var opts = {times: 5, interval: 20};

        async.auto({
            foo(callback){
                callback(null, 'FOO');
            },
            retry: async.retryable(opts, (callback) => {
                callback('err');
            })
        }, () => {
            var duration = new Date().getTime() - start;
            var expectedMinimumDuration = (opts.times -1) * opts.interval;
            assert(duration >= expectedMinimumDuration,
                "The duration should have been greater than " +
                expectedMinimumDuration + ", but was " + duration);
            done();
        });
    });
});