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

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

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

        setTimeout(function () {
        }, 15);
    });

    it('should work as an embedded task', function(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(function(results, callback) {
                retryResults = results;
                callback(null, retryResult);
            })]
        }, function(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', function(done) {
        var start = new Date().getTime();
        var opts = {times: 5, interval: 20};

        async.auto({
            foo: function(callback){
                callback(null, 'FOO');
            },
            retry: async.retryable(opts, function(callback) {
                callback('err');
            })
        }, function(){
            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();
        });
    });
});