Skip to content

Commit c0eb64c

Browse files
committed
perf(test): fast-forward cron receipt handoff
1 parent f61ec66 commit c0eb64c

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

‎src/cron/service/owner-hardening.test.ts‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import {
2525
releaseLocalCronRunReceiptOwnership,
2626
} from "../store/run-receipt-store.js";
2727
import type { CronJob } from "../types.js";
28+
import { listForeignReceipts } from "./foreign-receipt-monitor.js";
2829
import type { CronServiceState } from "./state.js";
2930
import { findCronTaskRunRecoveryInDatabase } from "./task-runs.js";
31+
import { stopTimer } from "./timer.js";
3032

3133
const { makeStorePath } = createCronStoreHarness({ prefix: "cron-owner-hardening-" });
3234
const children = new Set<ChildProcess>();
@@ -203,6 +205,22 @@ async function waitForExit(child: ChildProcess): Promise<void> {
203205
});
204206
}
205207

208+
async function waitForImmediate(
209+
predicate: () => boolean,
210+
description: string,
211+
timeoutMs = 1_000,
212+
): Promise<void> {
213+
const deadline = Date.now() + timeoutMs;
214+
while (!predicate()) {
215+
if (Date.now() >= deadline) {
216+
throw new Error(`Timed out waiting for ${description}`);
217+
}
218+
await new Promise<void>((resolve) => {
219+
setImmediate(resolve);
220+
});
221+
}
222+
}
223+
206224
function makeParentService(storePath: string, runCommandJob = vi.fn()) {
207225
return new CronService({
208226
storePath,
@@ -510,6 +528,7 @@ describe("cron durable run ownership", () => {
510528
let second: ChildProcess | undefined;
511529
try {
512530
await replacement.start();
531+
const replacementState = (replacement as unknown as { state: CronServiceState }).state;
513532
replacement.pauseScheduling();
514533
first.kill("SIGKILL");
515534
await waitForExit(first);
@@ -521,22 +540,31 @@ describe("cron durable run ownership", () => {
521540
outputPath: path.join(scriptRoot, `second-output-${now}`),
522541
});
523542
await waitForLine(second, "started");
543+
const secondReceiptId = receipts(storePath, job.id)[0]?.receiptId;
544+
expect(secondReceiptId).toBeDefined();
545+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
524546
replacement.resumeScheduling();
525-
await new Promise<void>((resolve) => {
526-
setTimeout(resolve, 2_500);
527-
});
547+
// Isolate the lifecycle-owned foreign receipt monitor from the ordinary job timer.
548+
stopTimer(replacementState);
549+
await vi.advanceTimersByTimeAsync(2_000);
550+
await waitForImmediate(
551+
() => listForeignReceipts(replacementState)[0]?.receiptId === secondReceiptId,
552+
"replacement foreign receipt enrollment",
553+
);
528554
second.kill("SIGKILL");
529555
await waitForExit(second);
556+
await vi.advanceTimersByTimeAsync(2_000);
530557

531558
await vi.waitFor(
532559
async () => {
533560
expect(receipts(storePath, job.id)[0]).toMatchObject({ status: "interrupted" });
534561
expect((await loadCronStore(storePath)).jobs[0]?.state.runningAtMs).toBeUndefined();
535562
},
536-
{ timeout: 6_000, interval: 50 },
563+
{ timeout: 1_000, interval: 10 },
537564
);
538565
} finally {
539566
replacement.stop();
567+
vi.useRealTimers();
540568
for (const child of [first, second]) {
541569
if (child && child.exitCode === null && child.signalCode === null) {
542570
child.kill("SIGKILL");

0 commit comments

Comments
 (0)