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,34 +109,42 @@ medusaIntegrationTestRunner({
|
|||||||
ContainerRegistrationKeys.QUERY
|
ContainerRegistrationKeys.QUERY
|
||||||
) as RemoteQueryFunction
|
) as RemoteQueryFunction
|
||||||
|
|
||||||
const resultset = await query.index({
|
const resultset = await fetchAndRetry(
|
||||||
entity: "product",
|
async () =>
|
||||||
fields: [
|
await query.index({
|
||||||
"id",
|
entity: "product",
|
||||||
"description",
|
fields: [
|
||||||
"status",
|
"id",
|
||||||
"title",
|
"description",
|
||||||
"variants.sku",
|
"status",
|
||||||
"variants.barcode",
|
"title",
|
||||||
"variants.material",
|
"variants.sku",
|
||||||
"variants.options.value",
|
"variants.barcode",
|
||||||
"variants.prices.amount",
|
"variants.material",
|
||||||
"variants.prices.currency_code",
|
"variants.options.value",
|
||||||
"variants.inventory_items.inventory.sku",
|
"variants.prices.amount",
|
||||||
"variants.inventory_items.inventory.description",
|
"variants.prices.currency_code",
|
||||||
],
|
"variants.inventory_items.inventory.sku",
|
||||||
filters: {
|
"variants.inventory_items.inventory.description",
|
||||||
"variants.sku": { $like: "%-1" },
|
],
|
||||||
"variants.prices.amount": { $gt: 30 },
|
filters: {
|
||||||
},
|
"variants.sku": { $like: "%-1" },
|
||||||
pagination: {
|
"variants.prices.amount": { $gt: 30 },
|
||||||
take: 10,
|
},
|
||||||
skip: 0,
|
pagination: {
|
||||||
order: {
|
take: 10,
|
||||||
"variants.prices.amount": "DESC",
|
skip: 0,
|
||||||
},
|
order: {
|
||||||
},
|
"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,24 +270,32 @@ medusaIntegrationTestRunner({
|
|||||||
ContainerRegistrationKeys.QUERY
|
ContainerRegistrationKeys.QUERY
|
||||||
) as RemoteQueryFunction
|
) as RemoteQueryFunction
|
||||||
|
|
||||||
const resultset = await query.index({
|
const resultset = await fetchAndRetry(
|
||||||
entity: "product",
|
async () =>
|
||||||
fields: [
|
await query.index({
|
||||||
"id",
|
entity: "product",
|
||||||
"variants.prices.amount",
|
fields: [
|
||||||
"variants.prices.currency_code",
|
"id",
|
||||||
],
|
"variants.prices.amount",
|
||||||
filters: {
|
"variants.prices.currency_code",
|
||||||
"variants.prices.currency_code": "USD",
|
],
|
||||||
},
|
filters: {
|
||||||
pagination: {
|
"variants.prices.currency_code": "USD",
|
||||||
take: 1,
|
},
|
||||||
skip: 0,
|
pagination: {
|
||||||
order: {
|
take: 1,
|
||||||
"variants.prices.amount": "DESC",
|
skip: 0,
|
||||||
},
|
order: {
|
||||||
},
|
"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,32 +320,40 @@ medusaIntegrationTestRunner({
|
|||||||
},
|
},
|
||||||
])
|
])
|
||||||
|
|
||||||
const resultset2 = await query.index({
|
const resultset2 = await fetchAndRetry(
|
||||||
entity: "product",
|
async () =>
|
||||||
fields: [
|
query.index({
|
||||||
"id",
|
entity: "product",
|
||||||
"variants.prices.amount",
|
fields: [
|
||||||
"variants.prices.currency_code",
|
"id",
|
||||||
],
|
"variants.prices.amount",
|
||||||
filters: {
|
"variants.prices.currency_code",
|
||||||
variants: {
|
],
|
||||||
prices: {
|
filters: {
|
||||||
currency_code: "USD",
|
variants: {
|
||||||
},
|
prices: {
|
||||||
},
|
currency_code: "USD",
|
||||||
},
|
},
|
||||||
pagination: {
|
|
||||||
take: 1,
|
|
||||||
skip: 0,
|
|
||||||
order: {
|
|
||||||
variants: {
|
|
||||||
prices: {
|
|
||||||
amount: "ASC",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
pagination: {
|
||||||
},
|
take: 1,
|
||||||
})
|
skip: 0,
|
||||||
|
order: {
|
||||||
|
variants: {
|
||||||
|
prices: {
|
||||||
|
amount: "ASC",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
({ 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,33 +68,41 @@ 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(
|
||||||
fields: [
|
async () =>
|
||||||
"product.*",
|
indexEngine.query<"product">({
|
||||||
"product.variants.*",
|
fields: [
|
||||||
"product.variants.prices.*",
|
"product.*",
|
||||||
],
|
"product.variants.*",
|
||||||
filters: {
|
"product.variants.prices.*",
|
||||||
product: {
|
],
|
||||||
variants: {
|
filters: {
|
||||||
prices: {
|
product: {
|
||||||
amount: { $gt: 50 },
|
variants: {
|
||||||
},
|
prices: {
|
||||||
},
|
amount: { $gt: 50 },
|
||||||
},
|
},
|
||||||
},
|
|
||||||
pagination: {
|
|
||||||
order: {
|
|
||||||
product: {
|
|
||||||
variants: {
|
|
||||||
prices: {
|
|
||||||
amount: "DESC",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
pagination: {
|
||||||
},
|
order: {
|
||||||
})
|
product: {
|
||||||
|
variants: {
|
||||||
|
prices: {
|
||||||
|
amount: "DESC",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
({ data }) => data.length > 0,
|
||||||
|
{
|
||||||
|
retries: 3,
|
||||||
|
waitSeconds: 3,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
expect(results.length).toBe(1)
|
expect(results.length).toBe(1)
|
||||||
|
|
||||||
@@ -146,34 +155,42 @@ 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(
|
||||||
fields: [
|
async () =>
|
||||||
"product.*",
|
indexEngine.query<"product">({
|
||||||
"product.variants.*",
|
fields: [
|
||||||
"product.variants.prices.*",
|
"product.*",
|
||||||
],
|
"product.variants.*",
|
||||||
filters: {
|
"product.variants.prices.*",
|
||||||
product: {
|
],
|
||||||
variants: {
|
filters: {
|
||||||
prices: {
|
product: {
|
||||||
amount: { $gt: 50 },
|
variants: {
|
||||||
currency_code: { $eq: "AUD" },
|
prices: {
|
||||||
},
|
amount: { $gt: 50 },
|
||||||
},
|
currency_code: { $eq: "AUD" },
|
||||||
},
|
},
|
||||||
},
|
|
||||||
pagination: {
|
|
||||||
order: {
|
|
||||||
product: {
|
|
||||||
variants: {
|
|
||||||
prices: {
|
|
||||||
amount: "DESC",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
pagination: {
|
||||||
},
|
order: {
|
||||||
})
|
product: {
|
||||||
|
variants: {
|
||||||
|
prices: {
|
||||||
|
amount: "DESC",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
({ 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