[test] Calculate rate-limit timeout from error response

Feature:

  Instead of guessing of a proper timeout, we shall use the error
  response to calculate it. The error provides us with the number
  of milliseconds we need to wait before retrying the request.
experimental
GochoMugo 9 years ago
parent 80a25c0e6e
commit 6dabdb47d8
  1. 17
      test/utils.js

@ -153,11 +153,12 @@ function handleRatelimit(bot, methodName, suite) {
const backupMethodName = `__${methodName}`; const backupMethodName = `__${methodName}`;
if (!bot[backupMethodName]) bot[backupMethodName] = bot[methodName]; if (!bot[backupMethodName]) bot[backupMethodName] = bot[methodName];
const maxRetries = 3;
const addSecs = 5;
const method = bot[backupMethodName]; const method = bot[backupMethodName];
assert.equal(typeof method, 'function'); assert.equal(typeof method, 'function');
bot[methodName] = (...args) => { bot[methodName] = (...args) => {
const minute = 60 * 1000;
let retry = 0; let retry = 0;
function exec() { function exec() {
return method.call(bot, ...args) return method.call(bot, ...args)
@ -166,16 +167,20 @@ function handleRatelimit(bot, methodName, suite) {
throw error; throw error;
} }
retry++; retry++;
if (retry > 3) { if (retry > maxRetries) {
throw error; throw error;
} }
console.error('tests: Handling rate-limit error'); // eslint-disable-line no-console if (typeof error.response.body === 'string') {
const timeout = minute * retry; error.response.body = JSON.parse(error.response.body);
suite.timeout(timeout); }
const retrySecs = error.response.body.parameters.retry_after;
const timeout = (1000 * retrySecs) + (1000 * addSecs);
console.error('tests: Handling rate-limit error. Retrying after %d secs', timeout / 1000); // eslint-disable-line no-console
suite.timeout(timeout * 2);
return new Promise(function timeoutPromise(resolve, reject) { return new Promise(function timeoutPromise(resolve, reject) {
setTimeout(function execTimeout() { setTimeout(function execTimeout() {
return exec().then(resolve).catch(reject); return exec().then(resolve).catch(reject);
}, timeout / 2); }, timeout);
}); });
}); });
} }

Loading…
Cancel
Save