1 var common = require('../common');
2 var assert = common.assert;
3 var fake = common.fake.create();
4 var retry = require(common.dir.lib + '/retry');
6 (function testErrors() {
7 var operation = retry.operation();
9 var error = new Error('some error');
10 var error2 = new Error('some other error');
11 operation._errors.push(error);
12 operation._errors.push(error2);
14 assert.deepEqual(operation.errors(), [error, error2]);
17 (function testMainErrorReturnsMostFrequentError() {
18 var operation = retry.operation();
19 var error = new Error('some error');
20 var error2 = new Error('some other error');
22 operation._errors.push(error);
23 operation._errors.push(error2);
24 operation._errors.push(error);
26 assert.strictEqual(operation.mainError(), error);
29 (function testMainErrorReturnsLastErrorOnEqualCount() {
30 var operation = retry.operation();
31 var error = new Error('some error');
32 var error2 = new Error('some other error');
34 operation._errors.push(error);
35 operation._errors.push(error2);
37 assert.strictEqual(operation.mainError(), error2);
40 (function testAttempt() {
41 var operation = retry.operation();
42 var fn = new Function();
48 operation.attempt(fn, timeoutOpts);
50 assert.strictEqual(fn, operation._fn);
51 assert.strictEqual(timeoutOpts.timeout, operation._operationTimeout);
52 assert.strictEqual(timeoutOpts.cb, operation._operationTimeoutCb);
55 (function testRetry() {
57 var error = new Error('some error');
58 var operation = retry.operation([1, 2, 3]);
61 var finalCallback = fake.callback('finalCallback');
62 fake.expectAnytime(finalCallback);
65 operation.attempt(function(currentAttempt) {
67 assert.equal(currentAttempt, attempts);
68 if (operation.retry(error)) {
72 assert.strictEqual(attempts, 4);
73 assert.strictEqual(operation.attempts(), attempts);
74 assert.strictEqual(operation.mainError(), error);
82 (function testRetryForever() {
83 var error = new Error('some error');
84 var operation = retry.operation({ retries: 3, forever: true });
87 var finalCallback = fake.callback('finalCallback');
88 fake.expectAnytime(finalCallback);
91 operation.attempt(function(currentAttempt) {
93 assert.equal(currentAttempt, attempts);
94 if (attempts !== 6 && operation.retry(error)) {
98 assert.strictEqual(attempts, 6);
99 assert.strictEqual(operation.attempts(), attempts);
100 assert.strictEqual(operation.mainError(), error);
108 (function testRetryForeverNoRetries() {
109 var error = new Error('some error');
111 var operation = retry.operation({
119 var startTime = new Date().getTime();
121 var finalCallback = fake.callback('finalCallback');
122 fake.expectAnytime(finalCallback);
124 var fn = function() {
125 operation.attempt(function(currentAttempt) {
127 assert.equal(currentAttempt, attempts);
128 if (attempts !== 4 && operation.retry(error)) {
132 var endTime = new Date().getTime();
133 var minTime = startTime + (delay * 3);
134 var maxTime = minTime + 20 // add a little headroom for code execution time
135 assert(endTime > minTime)
136 assert(endTime < maxTime)
137 assert.strictEqual(attempts, 4);
138 assert.strictEqual(operation.attempts(), attempts);
139 assert.strictEqual(operation.mainError(), error);
147 (function testStop() {
148 var error = new Error('some error');
149 var operation = retry.operation([1, 2, 3]);
152 var finalCallback = fake.callback('finalCallback');
153 fake.expectAnytime(finalCallback);
155 var fn = function() {
156 operation.attempt(function(currentAttempt) {
158 assert.equal(currentAttempt, attempts);
160 if (attempts === 2) {
163 assert.strictEqual(attempts, 2);
164 assert.strictEqual(operation.attempts(), attempts);
165 assert.strictEqual(operation.mainError(), error);
169 if (operation.retry(error)) {