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
|
var async = require('../lib');
var expect = require('chai').expect;
var assert = require('assert');
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) {
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);
});
});
|