chore: retry util on tests (#12126)
This commit is contained in:
committed by
GitHub
parent
cb26c224ea
commit
f615ebb7e8
32
integration-tests/helpers/retry.ts
Normal file
32
integration-tests/helpers/retry.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { setTimeout } from "timers/promises"
|
||||
|
||||
export async function fetchAndRetry(
|
||||
func: () => Promise<any>,
|
||||
validation?: (data: any) => boolean,
|
||||
{
|
||||
retries = 3,
|
||||
waitSeconds = 1,
|
||||
}: {
|
||||
retries?: number
|
||||
waitSeconds?: number
|
||||
} = {}
|
||||
) {
|
||||
while (retries >= 0) {
|
||||
try {
|
||||
const res = await func()
|
||||
|
||||
if (validation && !validation(res)) {
|
||||
throw new Error("Validation failed. Retry...")
|
||||
}
|
||||
|
||||
return res
|
||||
} catch (err) {
|
||||
if (retries > 0) {
|
||||
retries--
|
||||
await setTimeout(waitSeconds * 1000)
|
||||
continue
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user