-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add retry function and its test cases
- Loading branch information
1 parent
f8b0940
commit b271df5
Showing
3 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import Collector from './Collector.js'; | ||
import { retry } from './retry.js'; | ||
|
||
export { Collector }; | ||
export { Collector, retry }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { retry } from './retry.js'; | ||
|
||
test('retry - 1', async () => { | ||
let numCalled = 0; | ||
const fn = () => | ||
new Promise((resolve, reject) => { | ||
numCalled++; | ||
if (numCalled < 2) { | ||
reject('error'); | ||
return; | ||
} | ||
resolve('success'); | ||
}); | ||
|
||
const start = performance.now(); | ||
await expect(retry(fn)).resolves.toBe('success'); | ||
const end = performance.now(); | ||
expect(end - start).toBeGreaterThan(100); | ||
expect(numCalled).toBe(2); | ||
}); | ||
|
||
test('retry - 2', async () => { | ||
let numCalled = 0; | ||
const fn = () => | ||
new Promise((resolve, reject) => { | ||
numCalled++; | ||
if (numCalled < 4) { | ||
reject('error'); | ||
return; | ||
} | ||
resolve('success'); | ||
}); | ||
|
||
const start = performance.now(); | ||
await expect( | ||
retry(fn, { maxRetries: 5, minDelay: 10, delayFactor: 1 }) | ||
).resolves.toBe('success'); | ||
const end = performance.now(); | ||
expect(end - start).toBeGreaterThan(30); | ||
expect(numCalled).toBe(4); | ||
}); | ||
|
||
test('retry - 3', async () => { | ||
let numCalled = 0; | ||
const fn = () => | ||
new Promise(() => { | ||
numCalled++; | ||
throw new Error('error'); | ||
}); | ||
|
||
const start = performance.now(); | ||
await expect( | ||
retry(fn, { maxRetries: 5, minDelay: 10, delayFactor: 2 }) | ||
).rejects.toThrow('error'); | ||
const end = performance.now(); | ||
expect(end - start).toBeGreaterThan(10 + 20 + 40 + 80 + 160); | ||
expect(numCalled).toBe(6); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export type RetryOptions = { | ||
/** | ||
* Maximum number of retries. Defaults to 3. | ||
*/ | ||
maxRetries: number; | ||
/** | ||
* Minimum delay in milliseconds between retries. Defaults to 100 (ms). | ||
*/ | ||
minDelay?: number; | ||
/** | ||
* Factor by which to multiply the delay after each retry. Defaults to 2. | ||
*/ | ||
delayFactor?: number; | ||
}; | ||
|
||
/** | ||
* Retry a async function up to `maxRetries` times with exponential backoff. | ||
*/ | ||
export const retry = <T>( | ||
/** | ||
* The async function to retry. | ||
*/ | ||
fn: () => Promise<T>, | ||
/** | ||
* Options. | ||
*/ | ||
options?: RetryOptions | ||
): Promise<T> => { | ||
// retry `promise` up to `maxRetries` times with exponential backoff | ||
const { maxRetries = 3, minDelay = 100, delayFactor = 2 } = options ?? {}; | ||
return new Promise((resolve, reject) => { | ||
let retries = 0; | ||
const tryPromise = () => { | ||
fn() | ||
.then(resolve) | ||
.catch((error) => { | ||
if (retries < maxRetries) { | ||
retries++; | ||
const delay = minDelay * Math.pow(delayFactor, retries); | ||
setTimeout(tryPromise, delay); | ||
} else { | ||
reject(error); | ||
} | ||
}); | ||
}; | ||
tryPromise(); | ||
}); | ||
}; |