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