it("enforces a minimum delay when the next wake time is in the past", () => { const timeoutSpy = vi.spyOn(globalThis, "setTimeout"); const now = Date.parse("2026-02-28T12:32:00.000Z"); const pastDueMs = 17 * 60 * 1000; // 17 minutes past due
// Before the fix, delay would be 0 (tight loop). // After the fix, delay must be >= MIN_REFIRE_GAP_MS (2000 ms).
expect(delays.length).toBeGreaterThan(0); for (const d of delays) {
expect(d).toBeGreaterThanOrEqual(2_000);
}
timeoutSpy.mockRestore();
});
it("does not add extra delay when the next wake time is in the future", () => { const timeoutSpy = vi.spyOn(globalThis, "setTimeout"); const now = Date.parse("2026-02-28T12:32:00.000Z");
const state = createTimerState({
storePath: "/tmp/test-cron/jobs.json",
now,
});
state.store = {
version: 1,
jobs: [
{
id: "future-job",
name: "future-job",
enabled: true,
deleteAfterRun: false,
createdAtMs: now,
updatedAtMs: now,
schedule: { kind: "cron", expr: "*/15 * * * *" },
sessionTarget: "isolated" as const,
wakeMode: "next-heartbeat" as const,
payload: { kind: "agentTurn" as const, message: "test" },
delivery: { mode: "none" as const },
state: { nextRunAtMs: now + 10_000 }, // 10 seconds in the future
},
],
};
armTimer(state);
const delays = extractTimeoutDelays(timeoutSpy);
// The natural delay (10 s) should be used, not the floor.
expect(delays).toContain(10_000);
timeoutSpy.mockRestore();
});
it("keeps a maintenance wake armed when enabled jobs have no nextRunAtMs", () => { const timeoutSpy = vi.spyOn(globalThis, "setTimeout"); const now = Date.parse("2026-02-28T12:32:00.000Z");
const state = createTimerState({
storePath: store.storePath,
now,
});
// Simulate the onTimer path: it will find no runnable jobs (blocked by // runningAtMs) and re-arm the timer in its finally block.
await onTimer(state);
// The re-armed timer must NOT use delay=0. It should use at least // MIN_REFIRE_GAP_MS to prevent the hot-loop. const allDelays = extractTimeoutDelays(timeoutSpy);
// The last setTimeout call is from the finally→armTimer path. const lastDelay = allDelays[allDelays.length - 1];
expect(lastDelay).toBeGreaterThanOrEqual(2_000);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.