|
1 | 1 | import { sidequestTest, SidequestTestFixture } from "@/tests/fixture"; |
2 | 2 | import { Backend } from "@sidequest/backend"; |
3 | | -import { CompletedResult, CompleteTransition, JobData, RetryTransition, RunTransition } from "@sidequest/core"; |
| 3 | +import { |
| 4 | + CancelTransition, |
| 5 | + CompletedResult, |
| 6 | + CompleteTransition, |
| 7 | + JobData, |
| 8 | + RetryTransition, |
| 9 | + RunTransition, |
| 10 | +} from "@sidequest/core"; |
4 | 11 | import { JobTransitioner } from "../job/job-transitioner"; |
5 | 12 | import { grantQueueConfig } from "../queue/grant-queue-config"; |
6 | 13 | import { DummyJob } from "../test-jobs/dummy-job"; |
@@ -94,6 +101,54 @@ describe("ExecutorManager", () => { |
94 | 101 | inlineRunMock.mockReset(); |
95 | 102 | }); |
96 | 103 |
|
| 104 | + sidequestTest( |
| 105 | + "inline: a job that ignores the timeout completes instead of being retried", |
| 106 | + async ({ backend, config }) => { |
| 107 | + jobData = await backend.updateJob({ ...jobData, state: "claimed", claimed_at: new Date(), timeout: 20 }); |
| 108 | + const queryConfig = await grantQueueConfig(backend, { name: "default", concurrency: 1 }); |
| 109 | + const executorManager = new ExecutorManager(backend, { ...config, runner: "inline" }); |
| 110 | + |
| 111 | + // The job ignores the abort signal and completes after the timeout has already fired. |
| 112 | + inlineRunMock.mockImplementationOnce( |
| 113 | + () => |
| 114 | + new Promise((resolve) => |
| 115 | + setTimeout(() => resolve({ __is_job_transition__: true, type: "completed", result: "done" }), 60), |
| 116 | + ), |
| 117 | + ); |
| 118 | + |
| 119 | + await executorManager.execute(queryConfig, jobData); |
| 120 | + |
| 121 | + // Timeout fired (signal aborted) but inline applies the job's own result: completed, not a retry. |
| 122 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 123 | + expect(JobTransitioner.apply).toHaveBeenCalledWith(backend, jobData, expect.any(CompleteTransition)); |
| 124 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 125 | + expect(JobTransitioner.apply).not.toHaveBeenCalledWith(backend, jobData, expect.any(RetryTransition)); |
| 126 | + await executorManager.destroy(); |
| 127 | + inlineRunMock.mockReset(); |
| 128 | + }, |
| 129 | + ); |
| 130 | + |
| 131 | + sidequestTest( |
| 132 | + "inline: a job that ignores cancellation completes (its result wins)", |
| 133 | + async ({ backend, config }) => { |
| 134 | + // Pre-cancel in the DB so the first cancellation poll observes it immediately. JobTransitioner is |
| 135 | + // mocked here, so the RunTransition does not overwrite the persisted state. |
| 136 | + jobData = await backend.updateJob({ ...jobData, state: "canceled" }); |
| 137 | + const queryConfig = await grantQueueConfig(backend, { name: "default", concurrency: 1 }); |
| 138 | + const executorManager = new ExecutorManager(backend, { ...config, runner: "inline" }); |
| 139 | + |
| 140 | + inlineRunMock.mockResolvedValue({ __is_job_transition__: true, type: "completed", result: "done" }); |
| 141 | + |
| 142 | + await executorManager.execute(queryConfig, jobData); |
| 143 | + |
| 144 | + // Inline cannot force-stop the job; it completed, so its result is applied. |
| 145 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 146 | + expect(JobTransitioner.apply).toHaveBeenCalledWith(backend, jobData, expect.any(CompleteTransition)); |
| 147 | + await executorManager.destroy(); |
| 148 | + inlineRunMock.mockReset(); |
| 149 | + }, |
| 150 | + ); |
| 151 | + |
97 | 152 | sidequestTest("should abort job execution on job cancel", async ({ backend, config }) => { |
98 | 153 | await backend.updateJob({ ...jobData, state: "claimed", claimed_at: new Date() }); |
99 | 154 |
|
@@ -132,36 +187,54 @@ describe("ExecutorManager", () => { |
132 | 187 | await executorManager.destroy(); |
133 | 188 | }); |
134 | 189 |
|
135 | | - sidequestTest( |
136 | | - "does not overwrite the canceled state when an aborted job ignores the signal", |
137 | | - async ({ backend, config }) => { |
138 | | - await backend.updateJob({ ...jobData, state: "claimed", claimed_at: new Date() }); |
| 190 | + sidequestTest("respects a job's returned result even after a cancel was signaled", async ({ backend, config }) => { |
| 191 | + await backend.updateJob({ ...jobData, state: "claimed", claimed_at: new Date() }); |
139 | 192 |
|
140 | | - const queryConfig = await grantQueueConfig(backend, { name: "default", concurrency: 1 }); |
141 | | - const executorManager = new ExecutorManager(backend, config); |
142 | | - |
143 | | - // The job is canceled mid-run but ignores the abort signal and runs to completion. |
144 | | - runMock.mockImplementationOnce(async (job: JobData, signal: AbortSignal) => { |
145 | | - await backend.updateJob({ ...job, state: "canceled" }); |
146 | | - while (!signal.aborted) { |
147 | | - await new Promise((r) => setTimeout(r, 50)); |
148 | | - } |
149 | | - return { __is_job_transition__: true, type: "completed", result: "result" } as CompletedResult; |
| 193 | + const queryConfig = await grantQueueConfig(backend, { name: "default", concurrency: 1 }); |
| 194 | + const executorManager = new ExecutorManager(backend, config); |
| 195 | + |
| 196 | + // The job is canceled mid-run but ignores the abort signal and returns a completed result. |
| 197 | + runMock.mockImplementationOnce(async (job: JobData, signal: AbortSignal) => { |
| 198 | + await backend.updateJob({ ...job, state: "canceled" }); |
| 199 | + while (!signal.aborted) { |
| 200 | + await new Promise((r) => setTimeout(r, 50)); |
| 201 | + } |
| 202 | + return { __is_job_transition__: true, type: "completed", result: "result" } as CompletedResult; |
| 203 | + }); |
| 204 | + |
| 205 | + await executorManager.execute(queryConfig, jobData); |
| 206 | + |
| 207 | + // The job returned a state, so it is respected: the completion is applied. |
| 208 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 209 | + expect(JobTransitioner.apply).toHaveBeenCalledWith(backend, jobData, expect.any(CompleteTransition)); |
| 210 | + |
| 211 | + await executorManager.destroy(); |
| 212 | + }); |
| 213 | + |
| 214 | + sidequestTest("a hard-killed canceled job transitions to canceled", async ({ backend, config }) => { |
| 215 | + await backend.updateJob({ ...jobData, state: "claimed", claimed_at: new Date() }); |
| 216 | + |
| 217 | + const queryConfig = await grantQueueConfig(backend, { name: "default", concurrency: 1 }); |
| 218 | + const executorManager = new ExecutorManager(backend, config); |
| 219 | + |
| 220 | + // The job is canceled and never produces a result (the worker is terminated -> the run rejects). |
| 221 | + runMock.mockImplementationOnce(async (job: JobData, signal: AbortSignal) => { |
| 222 | + await backend.updateJob({ ...job, state: "canceled" }); |
| 223 | + return new Promise((_, reject) => { |
| 224 | + signal.addEventListener("abort", () => reject(new Error("The task has been aborted"))); |
150 | 225 | }); |
| 226 | + }); |
151 | 227 |
|
152 | | - await executorManager.execute(queryConfig, jobData); |
| 228 | + await executorManager.execute(queryConfig, jobData); |
153 | 229 |
|
154 | | - // The terminal completion transition must be skipped so the canceled state is preserved. |
155 | | - // eslint-disable-next-line @typescript-eslint/unbound-method |
156 | | - expect(JobTransitioner.apply).not.toHaveBeenCalledWith( |
157 | | - backend, |
158 | | - expect.anything(), |
159 | | - expect.any(CompleteTransition), |
160 | | - ); |
| 230 | + // No result: the abort reason (canceled) decides the terminal state. |
| 231 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 232 | + expect(JobTransitioner.apply).toHaveBeenCalledWith(backend, jobData, expect.any(CancelTransition)); |
| 233 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 234 | + expect(JobTransitioner.apply).not.toHaveBeenCalledWith(backend, jobData, expect.any(CompleteTransition)); |
161 | 235 |
|
162 | | - await executorManager.destroy(); |
163 | | - }, |
164 | | - ); |
| 236 | + await executorManager.destroy(); |
| 237 | + }); |
165 | 238 |
|
166 | 239 | sidequestTest("should abort job execution on timeout", async ({ backend, config }) => { |
167 | 240 | jobData = await backend.updateJob({ ...jobData, state: "claimed", claimed_at: new Date(), timeout: 100 }); |
|
0 commit comments