diff --git a/lib/common/util.js b/lib/common/util.js index 762f39db469..ffd1d5e8079 100644 --- a/lib/common/util.js +++ b/lib/common/util.js @@ -507,13 +507,15 @@ function makeAuthorizedRequest(config) { } function handleRateLimitResp(err, res, body) { - if (shouldRetry(err) && autoRetry && MAX_RETRIES > attemptedRetries) { - setTimeout(function() { - request(authorizedReqOpts, handleRateLimitResp); - }, getNextRetryWait(attemptedRetries++)); - } else { - handleResp(err, res, body, callback); - } + handleResp(err, res, body, function(err, body, resp) { + if (shouldRetry(err) && autoRetry && MAX_RETRIES > attemptedRetries) { + setTimeout(function() { + request(authorizedReqOpts, handleRateLimitResp); + }, getNextRetryWait(attemptedRetries++)); + } else { + callback(err, body, resp); + } + }); } if (callback.onAuthorized) { diff --git a/package.json b/package.json index 7cfbb5bd9c5..820ab837ca6 100644 --- a/package.json +++ b/package.json @@ -77,9 +77,9 @@ "docs": "./scripts/docs.sh", "lint": "jshint lib/ regression/ test/", "test": "mocha test/*", - "regression-test": "mocha regression/* --timeout 20000", - "cover": "istanbul cover -x 'regression/*' _mocha -- --timeout 20000 test/* regression/*", - "coveralls": "istanbul cover -x 'regression/*' _mocha --report lcovonly -- --timeout 20000 test/* regression/* -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" + "regression-test": "mocha regression/* --timeout 30000", + "cover": "istanbul cover -x 'regression/*' _mocha -- --timeout 30000 test/* regression/*", + "coveralls": "istanbul cover -x 'regression/*' _mocha --report lcovonly -- --timeout 30000 test/* regression/* -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" }, "license": "Apache 2" } diff --git a/test/common/util.js b/test/common/util.js index e7c287f307a..0db33c6083f 100644 --- a/test/common/util.js +++ b/test/common/util.js @@ -536,6 +536,43 @@ describe('common/util', function() { }); }); + it('should retry rate limits on API errors', function(done) { + var attemptedRetries = 0; + var codes = [429, 503, 500, 'done']; + var error = new Error('Rate Limit Error.'); + error.code = codes[0]; // Rate limit error + + var authorizedReqOpts = { a: 'b', c: 'd' }; + + var old_setTimeout = setTimeout; + setTimeout = function(callback, time) { + var MIN_TIME = (Math.pow(2, attemptedRetries) * 1000); + var MAX_TIME = (Math.pow(2, attemptedRetries) * 1000) + 1000; + assert(time >= MIN_TIME && time <= MAX_TIME); + attemptedRetries++; + error.code = codes[attemptedRetries]; // test a new code + callback(); // make the request again + }; + + gsa_Override = function() { + return function authorize(reqOpts, callback) { + callback(null, authorizedReqOpts); + }; + }; + + request_Override = function(reqOpts, callback) { + callback(null, null, { error: error }); + }; + + var makeRequest = util.makeAuthorizedRequest({}); + makeRequest({}, function(err) { + setTimeout = old_setTimeout; + assert.equal(err.message, 'Rate Limit Error.'); + assert.equal(err.code, 'done'); + done(); + }); + }); + it('should retry rate limits 3x by default', function(done) { var attemptedRetries = 0; var error = new Error('Rate Limit Error.');