|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { CronService } from "./service.js"; |
| 6 | + |
| 7 | +const noopLogger = { |
| 8 | + debug: vi.fn(), |
| 9 | + info: vi.fn(), |
| 10 | + warn: vi.fn(), |
| 11 | + error: vi.fn(), |
| 12 | +}; |
| 13 | + |
| 14 | +async function makeStorePath() { |
| 15 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cron-")); |
| 16 | + return { |
| 17 | + storePath: path.join(dir, "cron", "jobs.json"), |
| 18 | + cleanup: async () => { |
| 19 | + await fs.rm(dir, { recursive: true, force: true }); |
| 20 | + }, |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +describe("CronService interval/cron jobs fire on time", () => { |
| 25 | + beforeEach(() => { |
| 26 | + vi.useFakeTimers(); |
| 27 | + vi.setSystemTime(new Date("2025-12-13T00:00:00.000Z")); |
| 28 | + noopLogger.debug.mockClear(); |
| 29 | + noopLogger.info.mockClear(); |
| 30 | + noopLogger.warn.mockClear(); |
| 31 | + noopLogger.error.mockClear(); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + vi.useRealTimers(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("fires an every-type main job when the timer fires a few ms late", async () => { |
| 39 | + const store = await makeStorePath(); |
| 40 | + const enqueueSystemEvent = vi.fn(); |
| 41 | + const requestHeartbeatNow = vi.fn(); |
| 42 | + |
| 43 | + const cron = new CronService({ |
| 44 | + storePath: store.storePath, |
| 45 | + cronEnabled: true, |
| 46 | + log: noopLogger, |
| 47 | + enqueueSystemEvent, |
| 48 | + requestHeartbeatNow, |
| 49 | + runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" })), |
| 50 | + }); |
| 51 | + |
| 52 | + await cron.start(); |
| 53 | + const job = await cron.add({ |
| 54 | + name: "every 10s check", |
| 55 | + enabled: true, |
| 56 | + schedule: { kind: "every", everyMs: 10_000 }, |
| 57 | + sessionTarget: "main", |
| 58 | + wakeMode: "next-heartbeat", |
| 59 | + payload: { kind: "systemEvent", text: "tick" }, |
| 60 | + }); |
| 61 | + |
| 62 | + const firstDueAt = job.state.nextRunAtMs!; |
| 63 | + expect(firstDueAt).toBe(Date.parse("2025-12-13T00:00:00.000Z") + 10_000); |
| 64 | + |
| 65 | + // Simulate setTimeout firing 5ms late (the race condition). |
| 66 | + vi.setSystemTime(new Date(firstDueAt + 5)); |
| 67 | + await vi.runOnlyPendingTimersAsync(); |
| 68 | + |
| 69 | + // Wait for the async onTimer to complete via the lock queue. |
| 70 | + const jobs = await cron.list(); |
| 71 | + const updated = jobs.find((j) => j.id === job.id); |
| 72 | + |
| 73 | + expect(enqueueSystemEvent).toHaveBeenCalledWith("tick", { agentId: undefined }); |
| 74 | + expect(updated?.state.lastStatus).toBe("ok"); |
| 75 | + // nextRunAtMs must advance by at least one full interval past the due time. |
| 76 | + expect(updated?.state.nextRunAtMs).toBeGreaterThanOrEqual(firstDueAt + 10_000); |
| 77 | + |
| 78 | + cron.stop(); |
| 79 | + await store.cleanup(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("fires a cron-expression job when the timer fires a few ms late", async () => { |
| 83 | + const store = await makeStorePath(); |
| 84 | + const enqueueSystemEvent = vi.fn(); |
| 85 | + const requestHeartbeatNow = vi.fn(); |
| 86 | + |
| 87 | + // Set time to just before a minute boundary. |
| 88 | + vi.setSystemTime(new Date("2025-12-13T00:00:59.000Z")); |
| 89 | + |
| 90 | + const cron = new CronService({ |
| 91 | + storePath: store.storePath, |
| 92 | + cronEnabled: true, |
| 93 | + log: noopLogger, |
| 94 | + enqueueSystemEvent, |
| 95 | + requestHeartbeatNow, |
| 96 | + runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" })), |
| 97 | + }); |
| 98 | + |
| 99 | + await cron.start(); |
| 100 | + const job = await cron.add({ |
| 101 | + name: "every minute check", |
| 102 | + enabled: true, |
| 103 | + schedule: { kind: "cron", expr: "* * * * *" }, |
| 104 | + sessionTarget: "main", |
| 105 | + wakeMode: "next-heartbeat", |
| 106 | + payload: { kind: "systemEvent", text: "cron-tick" }, |
| 107 | + }); |
| 108 | + |
| 109 | + const firstDueAt = job.state.nextRunAtMs!; |
| 110 | + |
| 111 | + // Simulate setTimeout firing 5ms late. |
| 112 | + vi.setSystemTime(new Date(firstDueAt + 5)); |
| 113 | + await vi.runOnlyPendingTimersAsync(); |
| 114 | + |
| 115 | + // Wait for the async onTimer to complete via the lock queue. |
| 116 | + const jobs = await cron.list(); |
| 117 | + const updated = jobs.find((j) => j.id === job.id); |
| 118 | + |
| 119 | + expect(enqueueSystemEvent).toHaveBeenCalledWith("cron-tick", { agentId: undefined }); |
| 120 | + expect(updated?.state.lastStatus).toBe("ok"); |
| 121 | + // nextRunAtMs should be the next whole-minute boundary (60s later). |
| 122 | + expect(updated?.state.nextRunAtMs).toBe(firstDueAt + 60_000); |
| 123 | + |
| 124 | + cron.stop(); |
| 125 | + await store.cleanup(); |
| 126 | + }); |
| 127 | +}); |
0 commit comments