chore: retry util on tests (#12126)
This commit is contained in:
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
adminHeaders,
|
adminHeaders,
|
||||||
createAdminUser,
|
createAdminUser,
|
||||||
} from "../../../helpers/create-admin-user"
|
} from "../../../helpers/create-admin-user"
|
||||||
|
import { fetchAndRetry } from "../../../helpers/retry"
|
||||||
|
|
||||||
jest.setTimeout(120000)
|
jest.setTimeout(120000)
|
||||||
|
|
||||||
@@ -108,7 +109,9 @@ medusaIntegrationTestRunner({
|
|||||||
ContainerRegistrationKeys.QUERY
|
ContainerRegistrationKeys.QUERY
|
||||||
) as RemoteQueryFunction
|
) as RemoteQueryFunction
|
||||||
|
|
||||||
const resultset = await query.index({
|
const resultset = await fetchAndRetry(
|
||||||
|
async () =>
|
||||||
|
await query.index({
|
||||||
entity: "product",
|
entity: "product",
|
||||||
fields: [
|
fields: [
|
||||||
"id",
|
"id",
|
||||||
@@ -135,7 +138,13 @@ medusaIntegrationTestRunner({
|
|||||||
"variants.prices.amount": "DESC",
|
"variants.prices.amount": "DESC",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
|
({ data }) => data.length > 0,
|
||||||
|
{
|
||||||
|
retries: 3,
|
||||||
|
waitSeconds: 3,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
expect(resultset.metadata).toEqual({
|
expect(resultset.metadata).toEqual({
|
||||||
count: 2,
|
count: 2,
|
||||||
@@ -261,7 +270,9 @@ medusaIntegrationTestRunner({
|
|||||||
ContainerRegistrationKeys.QUERY
|
ContainerRegistrationKeys.QUERY
|
||||||
) as RemoteQueryFunction
|
) as RemoteQueryFunction
|
||||||
|
|
||||||
const resultset = await query.index({
|
const resultset = await fetchAndRetry(
|
||||||
|
async () =>
|
||||||
|
await query.index({
|
||||||
entity: "product",
|
entity: "product",
|
||||||
fields: [
|
fields: [
|
||||||
"id",
|
"id",
|
||||||
@@ -278,7 +289,13 @@ medusaIntegrationTestRunner({
|
|||||||
"variants.prices.amount": "DESC",
|
"variants.prices.amount": "DESC",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
|
({ data }) => data.length > 0,
|
||||||
|
{
|
||||||
|
retries: 3,
|
||||||
|
waitSeconds: 3,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// Limiting to 1 on purpose to keep it simple and check the correct order is maintained
|
// Limiting to 1 on purpose to keep it simple and check the correct order is maintained
|
||||||
expect(resultset.data).toEqual([
|
expect(resultset.data).toEqual([
|
||||||
@@ -303,7 +320,9 @@ medusaIntegrationTestRunner({
|
|||||||
},
|
},
|
||||||
])
|
])
|
||||||
|
|
||||||
const resultset2 = await query.index({
|
const resultset2 = await fetchAndRetry(
|
||||||
|
async () =>
|
||||||
|
query.index({
|
||||||
entity: "product",
|
entity: "product",
|
||||||
fields: [
|
fields: [
|
||||||
"id",
|
"id",
|
||||||
@@ -328,7 +347,13 @@ medusaIntegrationTestRunner({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
|
({ data }) => data.length > 0,
|
||||||
|
{
|
||||||
|
retries: 3,
|
||||||
|
waitSeconds: 3,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// Limiting to 1 on purpose to keep it simple and check the correct order is maintained
|
// Limiting to 1 on purpose to keep it simple and check the correct order is maintained
|
||||||
expect(resultset2.data).toEqual([
|
expect(resultset2.data).toEqual([
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
adminHeaders,
|
adminHeaders,
|
||||||
createAdminUser,
|
createAdminUser,
|
||||||
} from "../../../helpers/create-admin-user"
|
} from "../../../helpers/create-admin-user"
|
||||||
|
import { fetchAndRetry } from "../../../helpers/retry"
|
||||||
|
|
||||||
jest.setTimeout(100000)
|
jest.setTimeout(100000)
|
||||||
|
|
||||||
@@ -67,7 +68,9 @@ medusaIntegrationTestRunner({
|
|||||||
// Timeout to allow indexing to finish
|
// Timeout to allow indexing to finish
|
||||||
await setTimeout(4000)
|
await setTimeout(4000)
|
||||||
|
|
||||||
const { data: results } = await indexEngine.query<"product">({
|
const { data: results } = await fetchAndRetry(
|
||||||
|
async () =>
|
||||||
|
indexEngine.query<"product">({
|
||||||
fields: [
|
fields: [
|
||||||
"product.*",
|
"product.*",
|
||||||
"product.variants.*",
|
"product.variants.*",
|
||||||
@@ -93,7 +96,13 @@ medusaIntegrationTestRunner({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
|
({ data }) => data.length > 0,
|
||||||
|
{
|
||||||
|
retries: 3,
|
||||||
|
waitSeconds: 3,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
expect(results.length).toBe(1)
|
expect(results.length).toBe(1)
|
||||||
|
|
||||||
@@ -146,7 +155,9 @@ medusaIntegrationTestRunner({
|
|||||||
// Timeout to allow indexing to finish
|
// Timeout to allow indexing to finish
|
||||||
await setTimeout(4000)
|
await setTimeout(4000)
|
||||||
|
|
||||||
const { data: results } = await indexEngine.query<"product">({
|
const { data: results } = await fetchAndRetry(
|
||||||
|
async () =>
|
||||||
|
indexEngine.query<"product">({
|
||||||
fields: [
|
fields: [
|
||||||
"product.*",
|
"product.*",
|
||||||
"product.variants.*",
|
"product.variants.*",
|
||||||
@@ -173,7 +184,13 @@ medusaIntegrationTestRunner({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
|
({ data }) => data.length > 0,
|
||||||
|
{
|
||||||
|
retries: 3,
|
||||||
|
waitSeconds: 3,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
expect(results.length).toBe(1)
|
expect(results.length).toBe(1)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user