1 /**
2 * Integration test for workflow step retry intervals
3 *
4 * This test verifies the fix for the bug where steps with retry intervals
5 * would get stuck after the first retry attempt due to retryRescheduledAt
6 * not being properly cleared.
7 */
8
9 import { IWorkflowEngineService } from "@medusajs/framework/types"
10 import { Modules } from "@medusajs/framework/utils"
11 import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
12 import { ulid } from "ulid"
13 import {
14 retryIntervalStep1InvokeMock,
15 retryIntervalStep2InvokeMock,
16 workflowRetryIntervalId,
17 } from "../__fixtures__/workflow_retry_interval"
18 import {
19 retryIntervalStep0InvokeMock as retryIntervalStep0InvokeMockSync,
20 retryIntervalStep1InvokeMock as retryIntervalStep1InvokeMockSync,
21 retryIntervalStep2InvokeMock as retryIntervalStep2InvokeMockSync,
22 workflowRetryIntervalId as workflowRetryIntervalIdSync,
23 } from "../__fixtures__/workflow_sync_retry_interval"
24 import { TestDatabase } from "../utils"
25
26 jest.setTimeout(60000) // Increase timeout for async retries
27
28 const testRunId = ulid()
29
30 moduleIntegrationTestRunner<IWorkflowEngineService>({
31 moduleName: Modules.WORKFLOW_ENGINE,
32 resolve: __dirname + "/../..",
33 moduleOptions: {
34 redis: {
35 redisUrl: "localhost:6379",
36 queueName: `medusa-workflows-${
37 process.env.JEST_WORKER_ID ?? "1"
38 }-${testRunId}`,
39 jobQueueName: `medusa-workflows-jobs-${
40 process.env.JEST_WORKER_ID ?? "1"
41 }-${testRunId}`,
42 },
43 },
44 testSuite: ({ service: workflowOrcModule }) => {
45 describe("Workflow Retry Interval", function () {
46 beforeEach(async () => {
47 await TestDatabase.clearTables()
48 jest.clearAllMocks()
49 })
50
51 afterEach(async () => {
52 await TestDatabase.clearTables()
53 })
54
55 it("should properly retry sync step with retry interval after failures", async () => {
56 // Configure step to succeed on 3rd attempt
57 const attemptToSucceedOn = 3
58
59 // Track when each attempt happens
60 const attemptTimestamps: number[] = []
61 retryIntervalStep1InvokeMockSync.mockImplementation(() => {
62 attemptTimestamps.push(Date.now())
63 })
64
65 // Create promise to wait for workflow completion
66 const workflowCompletion = new Promise<{ result: any; errors: any }>(
67 (resolve) => {
68 workflowOrcModule.subscribe({
69 workflowId: workflowRetryIntervalIdSync,
70 subscriber: async (data) => {
71 if (data.eventType === "onFinish") {
72 resolve({
73 result: data.result,
74 errors: data.errors,
75 })
76 }
77 },
78 })
79 }
80 )
81
82 // Execute workflow
83 await workflowOrcModule.run(workflowRetryIntervalIdSync, {
84 input: { attemptToSucceedOn },
85 throwOnError: false,
86 })
87
88 // Wait for async workflow to complete
89 const { result, errors } = await workflowCompletion
90
91 // Assertions
92
93 // Step 0 should have been called once
94 expect(retryIntervalStep0InvokeMockSync).toHaveBeenCalledTimes(1)
95
96 // Step 1 should have been called 3 times (2 failures + 1 success)
97 expect(retryIntervalStep1InvokeMockSync).toHaveBeenCalledTimes(3)
98 expect(retryIntervalStep1InvokeMockSync).toHaveBeenNthCalledWith(1, {
99 attemptToSucceedOn,
100 })
101 expect(retryIntervalStep1InvokeMockSync).toHaveBeenNthCalledWith(2, {
102 attemptToSucceedOn,
103 })
104 expect(retryIntervalStep1InvokeMockSync).toHaveBeenNthCalledWith(3, {
105 attemptToSucceedOn,
106 })
107
108 // Step 2 should have been called once (after step 1 succeeded)
109 expect(retryIntervalStep2InvokeMockSync).toHaveBeenCalledTimes(1)
110
111 // Workflow should complete successfully
112 expect(errors === undefined || errors.length === 0).toBe(true)
113 expect(result).toBeDefined()
114 expect(result.step1).toBeDefined()
115 expect(result.step1.success).toBe(true)
116 expect(result.step1.attempts).toBe(3)
117
118 // Verify retry intervals are approximately 1 second (with some tolerance)
119 if (attemptTimestamps.length >= 2) {
120 const firstRetryInterval = attemptTimestamps[1] - attemptTimestamps[0]
121 expect(firstRetryInterval).toBeGreaterThan(800) // At least 800ms
122 expect(firstRetryInterval).toBeLessThan(2000) // Less than 2s
123 }
124
125 if (attemptTimestamps.length >= 3) {
126 const secondRetryInterval =
127 attemptTimestamps[2] - attemptTimestamps[1]
128 expect(secondRetryInterval).toBeGreaterThan(800)
129 expect(secondRetryInterval).toBeLessThan(2000)
130 }
131 })
132
133 it("should properly retry async step with retry interval after failures", async () => {
134 // Configure step to succeed on 3rd attempt
135 const attemptToSucceedOn = 3
136
137 // Track when each attempt happens
138 const attemptTimestamps: number[] = []
139 retryIntervalStep1InvokeMock.mockImplementation(() => {
140 attemptTimestamps.push(Date.now())
141 })
142
143 // Create promise to wait for workflow completion
144 const workflowCompletion = new Promise<{ result: any; errors: any }>(
145 (resolve) => {
146 workflowOrcModule.subscribe({
147 workflowId: workflowRetryIntervalId,
148 subscriber: async (data) => {
149 if (data.eventType === "onFinish") {
150 resolve({
151 result: data.result,
152 errors: data.errors,
153 })
154 }
155 },
156 })
157 }
158 )
159
160 // Execute workflow
161 await workflowOrcModule.run(workflowRetryIntervalId, {
162 input: { attemptToSucceedOn },
163 throwOnError: false,
164 })
165
166 // Wait for async workflow to complete
167 const { result, errors } = await workflowCompletion
168
169 // Assertions
170 // Step 1 should have been called 3 times (2 failures + 1 success)
171 expect(retryIntervalStep1InvokeMock).toHaveBeenCalledTimes(3)
172
173 // Step 2 should have been called once (after step 1 succeeded)
174 expect(retryIntervalStep2InvokeMock).toHaveBeenCalledTimes(1)
175
176 // Workflow should complete successfully
177 expect(errors === undefined || errors.length === 0).toBe(true)
178 expect(result).toBeDefined()
179 expect(result.step1).toBeDefined()
180 expect(result.step1.success).toBe(true)
181 expect(result.step1.attempts).toBe(3)
182
183 // Verify retry intervals are approximately 1 second (with some tolerance)
184 if (attemptTimestamps.length >= 2) {
185 const firstRetryInterval = attemptTimestamps[1] - attemptTimestamps[0]
186 expect(firstRetryInterval).toBeGreaterThan(800) // At least 800ms
187 expect(firstRetryInterval).toBeLessThan(2000) // Less than 2s
188 }
189
190 if (attemptTimestamps.length >= 3) {
191 const secondRetryInterval =
192 attemptTimestamps[2] - attemptTimestamps[1]
193 expect(secondRetryInterval).toBeGreaterThan(800)
194 expect(secondRetryInterval).toBeLessThan(2000)
195 }
196 })
197 })
198 },
199 })