1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/development/lib/retry.js
Mark Stacey 13b1f8c758
Add --retry-until-failure flag to e2e test script (#19532)
The e2e test script now accepts a `--retry-until-failure` flag that can
be useful for locally reproducing intermittent failures.

Normally the `retries` option will only retry upon failure. But if
you want the test to fail to inspect the failure state, you want it to
keep running until the first failure then stop. This flag accomplishes
that, reversing the retry conditions.
2023-07-10 17:10:13 -02:30

54 lines
1.6 KiB
JavaScript

/**
* Re-runs the given function until it returns a resolved promise or the number
* of retries is exceeded, whichever comes first (with an optional delay in
* between retries).
*
* @param {object} args - A set of arguments and options.
* @param {number} args.retries - The maximum number of times to re-run the
* function on failure.
* @param {number} args.delay - The amount of time (in milliseconds) to wait in
* between retries. (Default: 0)
* @param {string} args.rejectionMessage - The message for the rejected promise
* this function will return in the event of failure. (Default: "Retry limit
* reached")
* @param {string} args.retryUntilFailure - Retries until the function fails.
* @param {Function} functionToRetry - The function that is run and tested for
* failure.
* @returns {Promise<null | Error>} a promise that either resolves to null if
* the function is successful or is rejected with rejectionMessage otherwise.
*/
async function retry(
{
retries,
delay = 0,
rejectionMessage = 'Retry limit reached',
retryUntilFailure = false,
},
functionToRetry,
) {
let attempts = 0;
while (attempts <= retries) {
if (attempts > 0 && delay > 0) {
await new Promise((resolve) => setTimeout(resolve, delay));
}
try {
await functionToRetry();
if (!retryUntilFailure) {
return;
}
} catch (error) {
console.error(error);
if (retryUntilFailure) {
return;
}
} finally {
attempts += 1;
}
}
throw new Error(rejectionMessage);
}
module.exports = { retry };