From f615ebb7e852d7a1e8317672a9a03ec4311360aa Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Wed, 9 Apr 2025 13:07:05 -0300 Subject: [PATCH] chore: retry util on tests (#12126) --- integration-tests/helpers/retry.ts | 32 ++++ .../__tests__/index/query-index.spec.ts | 165 ++++++++++-------- .../modules/__tests__/index/search.spec.ts | 115 ++++++------ 3 files changed, 193 insertions(+), 119 deletions(-) create mode 100644 integration-tests/helpers/retry.ts diff --git a/integration-tests/helpers/retry.ts b/integration-tests/helpers/retry.ts new file mode 100644 index 0000000000..fc2ed3ddf9 --- /dev/null +++ b/integration-tests/helpers/retry.ts @@ -0,0 +1,32 @@ +import { setTimeout } from "timers/promises" + +export async function fetchAndRetry( + func: () => Promise, + 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 + } + } +} diff --git a/integration-tests/modules/__tests__/index/query-index.spec.ts b/integration-tests/modules/__tests__/index/query-index.spec.ts index 41716490bb..3bb6cef251 100644 --- a/integration-tests/modules/__tests__/index/query-index.spec.ts +++ b/integration-tests/modules/__tests__/index/query-index.spec.ts @@ -6,6 +6,7 @@ import { adminHeaders, createAdminUser, } from "../../../helpers/create-admin-user" +import { fetchAndRetry } from "../../../helpers/retry" jest.setTimeout(120000) @@ -108,34 +109,42 @@ medusaIntegrationTestRunner({ ContainerRegistrationKeys.QUERY ) as RemoteQueryFunction - const resultset = await query.index({ - entity: "product", - fields: [ - "id", - "description", - "status", - "title", - "variants.sku", - "variants.barcode", - "variants.material", - "variants.options.value", - "variants.prices.amount", - "variants.prices.currency_code", - "variants.inventory_items.inventory.sku", - "variants.inventory_items.inventory.description", - ], - filters: { - "variants.sku": { $like: "%-1" }, - "variants.prices.amount": { $gt: 30 }, - }, - pagination: { - take: 10, - skip: 0, - order: { - "variants.prices.amount": "DESC", - }, - }, - }) + const resultset = await fetchAndRetry( + async () => + await query.index({ + entity: "product", + fields: [ + "id", + "description", + "status", + "title", + "variants.sku", + "variants.barcode", + "variants.material", + "variants.options.value", + "variants.prices.amount", + "variants.prices.currency_code", + "variants.inventory_items.inventory.sku", + "variants.inventory_items.inventory.description", + ], + filters: { + "variants.sku": { $like: "%-1" }, + "variants.prices.amount": { $gt: 30 }, + }, + pagination: { + take: 10, + skip: 0, + order: { + "variants.prices.amount": "DESC", + }, + }, + }), + ({ data }) => data.length > 0, + { + retries: 3, + waitSeconds: 3, + } + ) expect(resultset.metadata).toEqual({ count: 2, @@ -261,24 +270,32 @@ medusaIntegrationTestRunner({ ContainerRegistrationKeys.QUERY ) as RemoteQueryFunction - const resultset = await query.index({ - entity: "product", - fields: [ - "id", - "variants.prices.amount", - "variants.prices.currency_code", - ], - filters: { - "variants.prices.currency_code": "USD", - }, - pagination: { - take: 1, - skip: 0, - order: { - "variants.prices.amount": "DESC", - }, - }, - }) + const resultset = await fetchAndRetry( + async () => + await query.index({ + entity: "product", + fields: [ + "id", + "variants.prices.amount", + "variants.prices.currency_code", + ], + filters: { + "variants.prices.currency_code": "USD", + }, + pagination: { + take: 1, + 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 expect(resultset.data).toEqual([ @@ -303,32 +320,40 @@ medusaIntegrationTestRunner({ }, ]) - const resultset2 = await query.index({ - entity: "product", - fields: [ - "id", - "variants.prices.amount", - "variants.prices.currency_code", - ], - filters: { - variants: { - prices: { - currency_code: "USD", - }, - }, - }, - pagination: { - take: 1, - skip: 0, - order: { - variants: { - prices: { - amount: "ASC", + const resultset2 = await fetchAndRetry( + async () => + query.index({ + entity: "product", + fields: [ + "id", + "variants.prices.amount", + "variants.prices.currency_code", + ], + filters: { + variants: { + prices: { + currency_code: "USD", + }, }, }, - }, - }, - }) + 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 expect(resultset2.data).toEqual([ diff --git a/integration-tests/modules/__tests__/index/search.spec.ts b/integration-tests/modules/__tests__/index/search.spec.ts index f4cb96f5db..13ac6a71a1 100644 --- a/integration-tests/modules/__tests__/index/search.spec.ts +++ b/integration-tests/modules/__tests__/index/search.spec.ts @@ -6,6 +6,7 @@ import { adminHeaders, createAdminUser, } from "../../../helpers/create-admin-user" +import { fetchAndRetry } from "../../../helpers/retry" jest.setTimeout(100000) @@ -67,33 +68,41 @@ medusaIntegrationTestRunner({ // Timeout to allow indexing to finish await setTimeout(4000) - const { data: results } = await indexEngine.query<"product">({ - fields: [ - "product.*", - "product.variants.*", - "product.variants.prices.*", - ], - filters: { - product: { - variants: { - prices: { - amount: { $gt: 50 }, - }, - }, - }, - }, - pagination: { - order: { - product: { - variants: { - prices: { - amount: "DESC", + const { data: results } = await fetchAndRetry( + async () => + indexEngine.query<"product">({ + fields: [ + "product.*", + "product.variants.*", + "product.variants.prices.*", + ], + filters: { + product: { + variants: { + prices: { + amount: { $gt: 50 }, + }, }, }, }, - }, - }, - }) + pagination: { + order: { + product: { + variants: { + prices: { + amount: "DESC", + }, + }, + }, + }, + }, + }), + ({ data }) => data.length > 0, + { + retries: 3, + waitSeconds: 3, + } + ) expect(results.length).toBe(1) @@ -146,34 +155,42 @@ medusaIntegrationTestRunner({ // Timeout to allow indexing to finish await setTimeout(4000) - const { data: results } = await indexEngine.query<"product">({ - fields: [ - "product.*", - "product.variants.*", - "product.variants.prices.*", - ], - filters: { - product: { - variants: { - prices: { - amount: { $gt: 50 }, - currency_code: { $eq: "AUD" }, - }, - }, - }, - }, - pagination: { - order: { - product: { - variants: { - prices: { - amount: "DESC", + const { data: results } = await fetchAndRetry( + async () => + indexEngine.query<"product">({ + fields: [ + "product.*", + "product.variants.*", + "product.variants.prices.*", + ], + filters: { + product: { + variants: { + prices: { + amount: { $gt: 50 }, + currency_code: { $eq: "AUD" }, + }, }, }, }, - }, - }, - }) + pagination: { + order: { + product: { + variants: { + prices: { + amount: "DESC", + }, + }, + }, + }, + }, + }), + ({ data }) => data.length > 0, + { + retries: 3, + waitSeconds: 3, + } + ) expect(results.length).toBe(1)