mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-04 22:40:24 +00:00
fix(cron): add spin-loop regression coverage
This commit is contained in:
@@ -2,7 +2,9 @@ import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import * as schedule from "./schedule.js";
|
||||
import { CronService } from "./service.js";
|
||||
import { computeJobNextRunAtMs } from "./service/jobs.js";
|
||||
import { createCronServiceState, type CronEvent } from "./service/state.js";
|
||||
import { onTimer } from "./service/timer.js";
|
||||
import type { CronJob, CronJobState } from "./types.js";
|
||||
@@ -593,6 +595,87 @@ describe("Cron issue regressions", () => {
|
||||
expect(fireCount).toBe(1);
|
||||
});
|
||||
|
||||
it("enforces a minimum refire gap for second-granularity cron schedules (#17821)", async () => {
|
||||
const store = await makeStorePath();
|
||||
const scheduledAt = Date.parse("2026-02-15T13:00:00.000Z");
|
||||
|
||||
const cronJob: CronJob = {
|
||||
id: "spin-gap-17821",
|
||||
name: "second-granularity",
|
||||
enabled: true,
|
||||
createdAtMs: scheduledAt - 86_400_000,
|
||||
updatedAtMs: scheduledAt - 86_400_000,
|
||||
schedule: { kind: "cron", expr: "* * * * * *", tz: "UTC" },
|
||||
sessionTarget: "isolated",
|
||||
wakeMode: "next-heartbeat",
|
||||
payload: { kind: "agentTurn", message: "pulse" },
|
||||
delivery: { mode: "announce" },
|
||||
state: { nextRunAtMs: scheduledAt },
|
||||
};
|
||||
await fs.writeFile(
|
||||
store.storePath,
|
||||
JSON.stringify({ version: 1, jobs: [cronJob] }, null, 2),
|
||||
"utf-8",
|
||||
);
|
||||
|
||||
let now = scheduledAt;
|
||||
const state = createCronServiceState({
|
||||
cronEnabled: true,
|
||||
storePath: store.storePath,
|
||||
log: noopLogger,
|
||||
nowMs: () => now,
|
||||
enqueueSystemEvent: vi.fn(),
|
||||
requestHeartbeatNow: vi.fn(),
|
||||
runIsolatedAgentJob: vi.fn(async () => {
|
||||
now += 100;
|
||||
return { status: "ok" as const, summary: "done" };
|
||||
}),
|
||||
});
|
||||
|
||||
await onTimer(state);
|
||||
|
||||
const job = state.store?.jobs.find((j) => j.id === "spin-gap-17821");
|
||||
expect(job).toBeDefined();
|
||||
const endedAt = now;
|
||||
const minNext = endedAt + 2_000;
|
||||
expect(job!.state.nextRunAtMs).toBeDefined();
|
||||
expect(job!.state.nextRunAtMs).toBeGreaterThanOrEqual(minNext);
|
||||
});
|
||||
|
||||
it("retries cron schedule computation from the next second when the first attempt returns undefined (#17821)", () => {
|
||||
const scheduledAt = Date.parse("2026-02-15T13:00:00.000Z");
|
||||
const cronJob: CronJob = {
|
||||
id: "retry-next-second-17821",
|
||||
name: "retry",
|
||||
enabled: true,
|
||||
createdAtMs: scheduledAt - 86_400_000,
|
||||
updatedAtMs: scheduledAt - 86_400_000,
|
||||
schedule: { kind: "cron", expr: "0 13 * * *", tz: "UTC" },
|
||||
sessionTarget: "isolated",
|
||||
wakeMode: "next-heartbeat",
|
||||
payload: { kind: "agentTurn", message: "briefing" },
|
||||
delivery: { mode: "announce" },
|
||||
state: {},
|
||||
};
|
||||
|
||||
const original = schedule.computeNextRunAtMs;
|
||||
const spy = vi.spyOn(schedule, "computeNextRunAtMs");
|
||||
try {
|
||||
spy
|
||||
.mockImplementationOnce(() => undefined)
|
||||
.mockImplementation((sched, nowMs) => original(sched, nowMs));
|
||||
|
||||
const expected = original(cronJob.schedule, scheduledAt + 1_000);
|
||||
expect(expected).toBeDefined();
|
||||
|
||||
const next = computeJobNextRunAtMs(cronJob, scheduledAt);
|
||||
expect(next).toBe(expected);
|
||||
expect(spy).toHaveBeenCalledTimes(2);
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it("records per-job start time and duration for batched due jobs", async () => {
|
||||
const store = await makeStorePath();
|
||||
const dueAt = Date.parse("2026-02-06T10:05:01.000Z");
|
||||
|
||||
Reference in New Issue
Block a user