fix(): Workflow save to db + index integration instability (#13707)
**What** - Fix index integration tests instability - Fix workflow engine storage save to db
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Helper functions to wait for index synchronization in a deterministic way
|
||||
* by directly checking the index_data table instead of using arbitrary timeouts.
|
||||
*/
|
||||
|
||||
export interface WaitForIndexOptions {
|
||||
timeout?: number
|
||||
pollInterval?: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for specific entities to be indexed by checking the index_data table directly.
|
||||
* This is more reliable than using arbitrary timeouts.
|
||||
*/
|
||||
export async function waitForIndexedEntities(
|
||||
dbConnection: any,
|
||||
entityName: string,
|
||||
entityIds: string[],
|
||||
options: WaitForIndexOptions = {}
|
||||
): Promise<void> {
|
||||
const { timeout = 120000, pollInterval = 100 } = options
|
||||
const startTime = Date.now()
|
||||
|
||||
while (Date.now() - startTime < timeout) {
|
||||
try {
|
||||
// Query the index_data table to check if all entities are indexed
|
||||
const result = await dbConnection.raw(
|
||||
`SELECT id FROM index_data WHERE name = ? AND id = ANY(?) AND staled_at IS NULL`,
|
||||
[entityName, entityIds]
|
||||
)
|
||||
|
||||
const indexedIds = result.rows
|
||||
? result.rows.map((row: any) => row.id)
|
||||
: result.map((row: any) => row.id)
|
||||
|
||||
// Check if all expected entities are indexed
|
||||
const allIndexed = entityIds.every((id) => indexedIds.includes(id))
|
||||
|
||||
if (allIndexed) {
|
||||
return
|
||||
}
|
||||
} catch (error) {
|
||||
// Continue polling on database errors
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, pollInterval))
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Entities [${entityIds.join(
|
||||
", "
|
||||
)}] of type '${entityName}' were not indexed within ${timeout}ms`
|
||||
)
|
||||
}
|
||||
@@ -7,15 +7,16 @@ import {
|
||||
defaultCurrencies,
|
||||
defineLink,
|
||||
Modules,
|
||||
promiseAll,
|
||||
} from "@medusajs/utils"
|
||||
import { setTimeout } from "timers/promises"
|
||||
import {
|
||||
adminHeaders,
|
||||
createAdminUser,
|
||||
} from "../../../helpers/create-admin-user"
|
||||
import { fetchAndRetry } from "../../../helpers/retry"
|
||||
import { waitForIndexedEntities } from "../../../helpers/wait-for-index"
|
||||
|
||||
jest.setTimeout(120000)
|
||||
jest.setTimeout(300000)
|
||||
|
||||
// NOTE: In this tests, both API are used to query, we use object pattern and string pattern
|
||||
|
||||
@@ -91,8 +92,6 @@ async function populateData(api: any) {
|
||||
)
|
||||
const products = response.data.created
|
||||
|
||||
await setTimeout(5000)
|
||||
|
||||
return products
|
||||
}
|
||||
|
||||
@@ -144,12 +143,31 @@ medusaIntegrationTestRunner({
|
||||
},
|
||||
})
|
||||
|
||||
await setTimeout(1000)
|
||||
|
||||
const query = appContainer.resolve(
|
||||
ContainerRegistrationKeys.QUERY
|
||||
) as RemoteQueryFunction
|
||||
|
||||
await promiseAll([
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"Product",
|
||||
products.map((p) => p.id)
|
||||
),
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"ProductVariant",
|
||||
products.flatMap((p) => p.variants.map((v) => v.id))
|
||||
),
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"Price",
|
||||
products.flatMap((p) =>
|
||||
p.variants.flatMap((v) => v.prices.map((p) => p.id))
|
||||
)
|
||||
),
|
||||
waitForIndexedEntities(dbConnection, "Brand", [brand.id]),
|
||||
])
|
||||
|
||||
const resultset = await fetchAndRetry(
|
||||
async () =>
|
||||
await query.index({
|
||||
@@ -338,12 +356,32 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should use query.index to query the index module sorting by price desc", async () => {
|
||||
await populateData(api)
|
||||
const products = await populateData(api)
|
||||
|
||||
const query = appContainer.resolve(
|
||||
ContainerRegistrationKeys.QUERY
|
||||
) as RemoteQueryFunction
|
||||
|
||||
await promiseAll([
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"Product",
|
||||
products.map((p) => p.id)
|
||||
),
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"ProductVariant",
|
||||
products.flatMap((p) => p.variants.map((v) => v.id))
|
||||
),
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"Price",
|
||||
products.flatMap((p) =>
|
||||
p.variants.flatMap((v) => v.prices.map((p) => p.id))
|
||||
)
|
||||
),
|
||||
])
|
||||
|
||||
const resultset = await fetchAndRetry(
|
||||
async () =>
|
||||
await query.index({
|
||||
@@ -446,7 +484,27 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should use query.index to get products by an array of handles", async () => {
|
||||
await populateData(api)
|
||||
const products = await populateData(api)
|
||||
|
||||
await promiseAll([
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"Product",
|
||||
products.map((p) => p.id)
|
||||
),
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"ProductVariant",
|
||||
products.flatMap((p) => p.variants.map((v) => v.id))
|
||||
),
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"Price",
|
||||
products.flatMap((p) =>
|
||||
p.variants.flatMap((v) => v.prices.map((p) => p.id))
|
||||
)
|
||||
),
|
||||
])
|
||||
|
||||
const query = appContainer.resolve(
|
||||
ContainerRegistrationKeys.QUERY
|
||||
@@ -475,7 +533,27 @@ medusaIntegrationTestRunner({
|
||||
})
|
||||
|
||||
it("should query by custom linkable field and default field using query.index", async () => {
|
||||
await populateData(api)
|
||||
const products = await populateData(api)
|
||||
|
||||
await promiseAll([
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"Product",
|
||||
products.map((p) => p.id)
|
||||
),
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"ProductVariant",
|
||||
products.flatMap((p) => p.variants.map((v) => v.id))
|
||||
),
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"Price",
|
||||
products.flatMap((p) =>
|
||||
p.variants.flatMap((v) => v.prices.map((p) => p.id))
|
||||
)
|
||||
),
|
||||
])
|
||||
|
||||
const query = appContainer.resolve(
|
||||
ContainerRegistrationKeys.QUERY
|
||||
@@ -515,12 +593,31 @@ medusaIntegrationTestRunner({
|
||||
},
|
||||
})
|
||||
|
||||
await setTimeout(1000)
|
||||
|
||||
const query = appContainer.resolve(
|
||||
ContainerRegistrationKeys.QUERY
|
||||
) as RemoteQueryFunction
|
||||
|
||||
await promiseAll([
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"Product",
|
||||
products.map((p) => p.id)
|
||||
),
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"ProductVariant",
|
||||
products.flatMap((p) => p.variants.map((v) => v.id))
|
||||
),
|
||||
waitForIndexedEntities(
|
||||
dbConnection,
|
||||
"Price",
|
||||
products.flatMap((p) =>
|
||||
p.variants.flatMap((v) => v.prices.map((p) => p.id))
|
||||
)
|
||||
),
|
||||
waitForIndexedEntities(dbConnection, "Brand", [brand.id]),
|
||||
])
|
||||
|
||||
const resultset = await fetchAndRetry(
|
||||
async () =>
|
||||
await query.index({
|
||||
|
||||
Reference in New Issue
Block a user