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, auth,
constants, constants,
env as coreEnv, env as coreEnv,
context,
utils, utils,
DEFAULT_TENANT_ID, DEFAULT_TENANT_ID,
} from "@budibase/backend-core" } from "@budibase/backend-core"
@ -34,6 +33,25 @@ enum Mode {
SELF = "self", 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 { class TestConfiguration {
server: any server: any
request: any request: any
@ -119,7 +137,8 @@ class TestConfiguration {
async beforeAll() { async beforeAll() {
this.#tenantId = `tenant-${utils.newid()}` 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 this.createSession(this.defaultUser!)
await tenancy.doInTenant(this.#tenantId, async () => { await tenancy.doInTenant(this.#tenantId, async () => {