-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
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
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
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,70 @@ | ||
import { describe, expect } from 'vitest' | ||
|
||
import { waitAndRetry } from './waitUtils' | ||
|
||
class Counter { | ||
private readonly timeOfSucces: number | ||
public executionCounter: number | ||
constructor(msecsTillSuccess: number) { | ||
this.timeOfSucces = Date.now() + msecsTillSuccess | ||
this.executionCounter = 0 | ||
} | ||
|
||
process() { | ||
this.executionCounter++ | ||
return Date.now() >= this.timeOfSucces | ||
} | ||
} | ||
|
||
describe('waitUtils', () => { | ||
describe('waitAndRetry', () => { | ||
it('executes once if there is an instant condition match', async () => { | ||
const counter = new Counter(0) | ||
|
||
const result = await waitAndRetry(() => { | ||
return counter.process() | ||
}) | ||
|
||
expect(result).toBe(true) | ||
expect(counter.executionCounter).toBe(1) | ||
}) | ||
|
||
it('executes until there is a condition match', async () => { | ||
const counter = new Counter(1000) | ||
|
||
const result = await waitAndRetry( | ||
() => { | ||
return counter.process() | ||
}, | ||
50, | ||
30, | ||
) | ||
|
||
expect(result).toBe(true) | ||
expect(counter.executionCounter > 0).toBe(true) | ||
}) | ||
|
||
it('times out of there is never a condition match', async () => { | ||
const counter = new Counter(1000) | ||
|
||
const result = await waitAndRetry( | ||
() => { | ||
return counter.process() | ||
}, | ||
20, | ||
30, | ||
) | ||
|
||
expect(result).toBe(false) | ||
expect(counter.executionCounter > 0).toBe(true) | ||
}) | ||
|
||
it('handles an error', async () => { | ||
await expect( | ||
waitAndRetry(() => { | ||
throw new Error('it broke') | ||
}), | ||
).rejects.toThrowError('it broke') | ||
}) | ||
}) | ||
}) |
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,34 @@ | ||
export const waitAndRetry = async <T>( | ||
predicateFn: () => T, | ||
sleepTime = 20, | ||
maxRetryCount = 15, | ||
): Promise<T> => { | ||
return new Promise((resolve, reject) => { | ||
let retryCount = 0 | ||
function performCheck() { | ||
// amount of retries exceeded | ||
if (maxRetryCount !== 0 && retryCount > maxRetryCount) { | ||
resolve(predicateFn()) | ||
} | ||
|
||
// Try executing predicateFn | ||
Promise.resolve() | ||
.then(() => { | ||
return predicateFn() | ||
}) | ||
.then((result) => { | ||
if (result) { | ||
resolve(result) | ||
} else { | ||
retryCount++ | ||
setTimeout(performCheck, sleepTime) | ||
} | ||
}) | ||
.catch((err) => { | ||
reject(err) | ||
}) | ||
} | ||
|
||
performCheck() | ||
}) | ||
} |