1var common = require('../common'); 2var assert = common.assert; 3var fake = common.fake.create(); 4var retry = require(common.dir.lib + '/retry'); 5 6(function testReset() { 7 var error = new Error('some error'); 8 var operation = retry.operation([1, 2, 3]); 9 var attempts = 0; 10 11 var finalCallback = fake.callback('finalCallback'); 12 fake.expectAnytime(finalCallback); 13 14 var expectedFinishes = 1; 15 var finishes = 0; 16 17 var fn = function() { 18 operation.attempt(function(currentAttempt) { 19 attempts++; 20 assert.equal(currentAttempt, attempts); 21 if (operation.retry(error)) { 22 return; 23 } 24 25 finishes++ 26 assert.equal(expectedFinishes, finishes); 27 assert.strictEqual(attempts, 4); 28 assert.strictEqual(operation.attempts(), attempts); 29 assert.strictEqual(operation.mainError(), error); 30 31 if (finishes < 2) { 32 attempts = 0; 33 expectedFinishes++; 34 operation.reset(); 35 fn() 36 } else { 37 finalCallback(); 38 } 39 }); 40 }; 41 42 fn(); 43})(); 44 45(function testErrors() { 46 var operation = retry.operation(); 47 48 var error = new Error('some error'); 49 var error2 = new Error('some other error'); 50 operation._errors.push(error); 51 operation._errors.push(error2); 52 53 assert.deepEqual(operation.errors(), [error, error2]); 54})(); 55 56(function testMainErrorReturnsMostFrequentError() { 57 var operation = retry.operation(); 58 var error = new Error('some error'); 59 var error2 = new Error('some other error'); 60 61 operation._errors.push(error); 62 operation._errors.push(error2); 63 operation._errors.push(error); 64 65 assert.strictEqual(operation.mainError(), error); 66})(); 67 68(function testMainErrorReturnsLastErrorOnEqualCount() { 69 var operation = retry.operation(); 70 var error = new Error('some error'); 71 var error2 = new Error('some other error'); 72 73 operation._errors.push(error); 74 operation._errors.push(error2); 75 76 assert.strictEqual(operation.mainError(), error2); 77})(); 78 79(function testAttempt() { 80 var operation = retry.operation(); 81 var fn = new Function(); 82 83 var timeoutOpts = { 84 timeout: 1, 85 cb: function() {} 86 }; 87 operation.attempt(fn, timeoutOpts); 88 89 assert.strictEqual(fn, operation._fn); 90 assert.strictEqual(timeoutOpts.timeout, operation._operationTimeout); 91 assert.strictEqual(timeoutOpts.cb, operation._operationTimeoutCb); 92})(); 93 94(function testRetry() { 95 var error = new Error('some error'); 96 var operation = retry.operation([1, 2, 3]); 97 var attempts = 0; 98 99 var finalCallback = fake.callback('finalCallback'); 100 fake.expectAnytime(finalCallback); 101 102 var fn = function() { 103 operation.attempt(function(currentAttempt) { 104 attempts++; 105 assert.equal(currentAttempt, attempts); 106 if (operation.retry(error)) { 107 return; 108 } 109 110 assert.strictEqual(attempts, 4); 111 assert.strictEqual(operation.attempts(), attempts); 112 assert.strictEqual(operation.mainError(), error); 113 finalCallback(); 114 }); 115 }; 116 117 fn(); 118})(); 119 120(function testRetryForever() { 121 var error = new Error('some error'); 122 var operation = retry.operation({ retries: 3, forever: true }); 123 var attempts = 0; 124 125 var finalCallback = fake.callback('finalCallback'); 126 fake.expectAnytime(finalCallback); 127 128 var fn = function() { 129 operation.attempt(function(currentAttempt) { 130 attempts++; 131 assert.equal(currentAttempt, attempts); 132 if (attempts !== 6 && operation.retry(error)) { 133 return; 134 } 135 136 assert.strictEqual(attempts, 6); 137 assert.strictEqual(operation.attempts(), attempts); 138 assert.strictEqual(operation.mainError(), error); 139 finalCallback(); 140 }); 141 }; 142 143 fn(); 144})(); 145 146(function testRetryForeverNoRetries() { 147 var error = new Error('some error'); 148 var delay = 50 149 var operation = retry.operation({ 150 retries: null, 151 forever: true, 152 minTimeout: delay, 153 maxTimeout: delay 154 }); 155 156 var attempts = 0; 157 var startTime = new Date().getTime(); 158 159 var finalCallback = fake.callback('finalCallback'); 160 fake.expectAnytime(finalCallback); 161 162 var fn = function() { 163 operation.attempt(function(currentAttempt) { 164 attempts++; 165 assert.equal(currentAttempt, attempts); 166 if (attempts !== 4 && operation.retry(error)) { 167 return; 168 } 169 170 var endTime = new Date().getTime(); 171 var minTime = startTime + (delay * 3); 172 var maxTime = minTime + 20 // add a little headroom for code execution time 173 assert(endTime >= minTime) 174 assert(endTime < maxTime) 175 assert.strictEqual(attempts, 4); 176 assert.strictEqual(operation.attempts(), attempts); 177 assert.strictEqual(operation.mainError(), error); 178 finalCallback(); 179 }); 180 }; 181 182 fn(); 183})(); 184 185(function testStop() { 186 var error = new Error('some error'); 187 var operation = retry.operation([1, 2, 3]); 188 var attempts = 0; 189 190 var finalCallback = fake.callback('finalCallback'); 191 fake.expectAnytime(finalCallback); 192 193 var fn = function() { 194 operation.attempt(function(currentAttempt) { 195 attempts++; 196 assert.equal(currentAttempt, attempts); 197 198 if (attempts === 2) { 199 operation.stop(); 200 201 assert.strictEqual(attempts, 2); 202 assert.strictEqual(operation.attempts(), attempts); 203 assert.strictEqual(operation.mainError(), error); 204 finalCallback(); 205 } 206 207 if (operation.retry(error)) { 208 return; 209 } 210 }); 211 }; 212 213 fn(); 214})(); 215 216(function testMaxRetryTime() { 217 var error = new Error('some error'); 218 var maxRetryTime = 30; 219 var operation = retry.operation({ 220 minTimeout: 1, 221 maxRetryTime: maxRetryTime 222 }); 223 var attempts = 0; 224 225 var finalCallback = fake.callback('finalCallback'); 226 fake.expectAnytime(finalCallback); 227 228 var longAsyncFunction = function (wait, callback){ 229 setTimeout(callback, wait); 230 }; 231 232 var fn = function() { 233 var startTime = new Date().getTime(); 234 operation.attempt(function(currentAttempt) { 235 attempts++; 236 assert.equal(currentAttempt, attempts); 237 238 if (attempts !== 2) { 239 if (operation.retry(error)) { 240 return; 241 } 242 } else { 243 var curTime = new Date().getTime(); 244 longAsyncFunction(maxRetryTime - (curTime - startTime - 1), function(){ 245 if (operation.retry(error)) { 246 assert.fail('timeout should be occurred'); 247 return; 248 } 249 250 assert.strictEqual(operation.mainError(), error); 251 finalCallback(); 252 }); 253 } 254 }); 255 }; 256 257 fn(); 258})(); 259