chore: Make scheduled job tests less likely to fail (#8241)

This commit is contained in:
Stevche Radevski
2024-07-23 16:59:27 +02:00
committed by GitHub
parent a9abf448c6
commit bcba5b3a6c
3 changed files with 62 additions and 16 deletions
@@ -5,8 +5,13 @@ import {
StepResponse, StepResponse,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
export const createScheduled = (name: string, schedule?: SchedulerOptions) => { export const createScheduled = (
name: string,
next: () => void,
schedule?: SchedulerOptions
) => {
const workflowScheduledStepInvoke = jest.fn((input, { container }) => { const workflowScheduledStepInvoke = jest.fn((input, { container }) => {
next()
return new StepResponse({ return new StepResponse({
testValue: container.resolve("test-value"), testValue: container.resolve("test-value"),
}) })
@@ -24,6 +24,7 @@ import "../__fixtures__"
import { createScheduled } from "../__fixtures__/workflow_scheduled" import { createScheduled } from "../__fixtures__/workflow_scheduled"
import { DB_URL, TestDatabase } from "../utils" import { DB_URL, TestDatabase } from "../utils"
import { WorkflowsModuleService } from "@medusajs/workflow-engine-inmemory/dist/services" import { WorkflowsModuleService } from "@medusajs/workflow-engine-inmemory/dist/services"
import Redis from "ioredis"
jest.setTimeout(100000) jest.setTimeout(100000)
@@ -40,6 +41,24 @@ const afterEach_ = async () => {
await TestDatabase.clearTables(sharedPgConnection) await TestDatabase.clearTables(sharedPgConnection)
} }
function times(num) {
let resolver
let counter = 0
const promise = new Promise((resolve) => {
resolver = resolve
})
return {
next: () => {
counter += 1
if (counter === num) {
resolver()
}
},
promise,
}
}
describe("Workflow Orchestrator module", function () { describe("Workflow Orchestrator module", function () {
let workflowOrcModule: IWorkflowEngineService let workflowOrcModule: IWorkflowEngineService
let query: RemoteQueryFunction let query: RemoteQueryFunction
@@ -49,6 +68,10 @@ describe("Workflow Orchestrator module", function () {
const container = createMedusaContainer() const container = createMedusaContainer()
container.register(ContainerRegistrationKeys.LOGGER, asValue(console)) container.register(ContainerRegistrationKeys.LOGGER, asValue(console))
// Clear any residual data in Redis
const redisClient = new Redis()
redisClient.flushall()
const { const {
runMigrations, runMigrations,
query: remoteQuery, query: remoteQuery,
@@ -83,10 +106,6 @@ describe("Workflow Orchestrator module", function () {
workflowOrcModule = modules.workflows as unknown as IWorkflowEngineService workflowOrcModule = modules.workflows as unknown as IWorkflowEngineService
}) })
afterAll(async () => {
await WorkflowManager.unregisterAll()
})
it(`should export the appropriate linkable configuration`, () => { it(`should export the appropriate linkable configuration`, () => {
const linkable = Module(Modules.WORKFLOW_ENGINE, { const linkable = Module(Modules.WORKFLOW_ENGINE, {
service: WorkflowsModuleService, service: WorkflowsModuleService,
@@ -352,33 +371,44 @@ describe("Workflow Orchestrator module", function () {
// Mocking bullmq, however, would make the tests close to useless, so we can keep them very minimal and serve as smoke tests. // Mocking bullmq, however, would make the tests close to useless, so we can keep them very minimal and serve as smoke tests.
describe("Scheduled workflows", () => { describe("Scheduled workflows", () => {
it("should execute a scheduled workflow", async () => { it("should execute a scheduled workflow", async () => {
const spy = createScheduled("standard") const wait = times(2)
await setTimeout(3100) const spy = createScheduled("standard", wait.next)
expect(spy).toHaveBeenCalledTimes(3)
await wait.promise
expect(spy).toHaveBeenCalledTimes(2)
WorkflowManager.unregister("standard")
}) })
it("should stop executions after the set number of executions", async () => { it("should stop executions after the set number of executions", async () => {
const spy = await createScheduled("num-executions", { const wait = times(2)
const spy = await createScheduled("num-executions", wait.next, {
cron: "* * * * * *", cron: "* * * * * *",
numberOfExecutions: 2, numberOfExecutions: 2,
}) })
await setTimeout(3100)
await wait.promise
expect(spy).toHaveBeenCalledTimes(2) expect(spy).toHaveBeenCalledTimes(2)
// Make sure that on the next tick it doesn't execute again
await setTimeout(1100)
expect(spy).toHaveBeenCalledTimes(2)
WorkflowManager.unregister("num-execution")
}) })
it("should remove scheduled workflow if workflow no longer exists", async () => { it("should remove scheduled workflow if workflow no longer exists", async () => {
const wait = times(1)
const logger = sharedContainer_.resolve<Logger>( const logger = sharedContainer_.resolve<Logger>(
ContainerRegistrationKeys.LOGGER ContainerRegistrationKeys.LOGGER
) )
const spy = await createScheduled("remove-scheduled", { const spy = await createScheduled("remove-scheduled", wait.next, {
cron: "* * * * * *", cron: "* * * * * *",
}) })
const logSpy = jest.spyOn(logger, "warn") const logSpy = jest.spyOn(logger, "warn")
await setTimeout(1100) await wait.promise
expect(spy).toHaveBeenCalledTimes(1) expect(spy).toHaveBeenCalledTimes(1)
WorkflowManager["workflows"].delete("remove-scheduled") WorkflowManager["workflows"].delete("remove-scheduled")
await setTimeout(1100) await setTimeout(1100)
@@ -389,19 +419,23 @@ describe("Workflow Orchestrator module", function () {
}) })
it("the scheduled workflow should have access to the shared container", async () => { it("the scheduled workflow should have access to the shared container", async () => {
const wait = times(1)
sharedContainer_.register( sharedContainer_.register(
"test-value", "test-value",
asFunction(() => "test") asFunction(() => "test")
) )
const spy = await createScheduled("shared-container-job", { const spy = await createScheduled("shared-container-job", wait.next, {
cron: "* * * * * *", cron: "* * * * * *",
}) })
await setTimeout(1100)
await wait.promise
expect(spy).toHaveBeenCalledTimes(1) expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveReturnedWith( expect(spy).toHaveReturnedWith(
expect.objectContaining({ output: { testValue: "test" } }) expect.objectContaining({ output: { testValue: "test" } })
) )
WorkflowManager.unregister("shared-container-job")
}) })
}) })
}) })
@@ -395,7 +395,14 @@ export class RedisDistributedTransactionStorage
} }
async remove(jobId: string): Promise<void> { async remove(jobId: string): Promise<void> {
await this.queue.removeRepeatableByKey(`${JobType.SCHEDULE}_${jobId}`) const repeatableJobs = await this.queue.getRepeatableJobs()
const job = repeatableJobs.find(
(job) => job.id === `${JobType.SCHEDULE}_${jobId}`
)
if (job) {
await this.queue.removeRepeatableByKey(job.key)
}
} }
async removeAll(): Promise<void> { async removeAll(): Promise<void> {