Add retry to prevent race conditions

This commit is contained in:
adrinr 2023-01-30 18:12:06 +00:00
parent 55de45e86b
commit 60c3e522fe
1 changed files with 21 additions and 2 deletions

View File

@ -20,7 +20,6 @@ import {
auth,
constants,
env as coreEnv,
context,
utils,
DEFAULT_TENANT_ID,
} from "@budibase/backend-core"
@ -34,6 +33,25 @@ enum Mode {
SELF = "self",
}
export async function retry<T extends (...arg0: any[]) => any>(
fn: T,
maxTry: number = 5,
retryCount = 1
): Promise<Awaited<ReturnType<T>>> {
const currRetry = typeof retryCount === "number" ? retryCount : 1
try {
const result = await fn()
return result
} catch (e) {
console.log(`Retry ${currRetry} failed.`)
if (currRetry > maxTry) {
console.log(`All ${maxTry} retry attempts exhausted`)
throw e
}
return retry(fn, maxTry, currRetry + 1)
}
}
class TestConfiguration {
server: any
request: any
@ -119,7 +137,8 @@ class TestConfiguration {
async beforeAll() {
this.#tenantId = `tenant-${utils.newid()}`
await this.createDefaultUser()
// Running tests in parallel causes issues creating the globaldb twice. This ensures the db is properly created before starting
await retry(async () => await this.createDefaultUser())
await this.createSession(this.defaultUser!)
await tenancy.doInTenant(this.#tenantId, async () => {