Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor: update leaseDurationMs and handlerTimeoutMs in cron job tests to enforce timeout constraints

Victor Hugo dos Santos0192003bbc20
diff --git a/backend/e2e-test/cron-job.spec.ts b/backend/e2e-test/cron-job.spec.ts
index 19cb9e8627..7410284d57 100644
--- a/backend/e2e-test/cron-job.spec.ts
+++ b/backend/e2e-test/cron-job.spec.ts
@@ -46,7 +46,7 @@ const makeFactory = (overrides?: Partial<Parameters<typeof cronJobFactory>[0]>)
slotRefreshMs: 100,
enqueueIntervalMs: 500,
processIntervalMs: 200,
- leaseDurationMs: 2000,
+ leaseDurationMs: 5_000,
retryBackoffBaseMs: 100,
retryBackoffMaxMs: 500,
handlerTimeoutMs: 5_000,
@@ -408,7 +408,7 @@ describe("graceful shutdown", () => {
})
);
- const f = factory({ handlerTimeoutMs: 30_000, drainTimeoutMs: 5_000 });
+ const f = factory({ leaseDurationMs: 30_000, handlerTimeoutMs: 30_000, drainTimeoutMs: 5_000 });
f.register({ name: "drain-job", pattern: FAST_PATTERN, handler, runHashTtlS: 3600 });
f.start();
@@ -447,7 +447,7 @@ describe("graceful shutdown", () => {
// drain timer expires.
test("stop() releases the slot even when an in-flight handler hangs past drainTimeoutMs", async () => {
const handler = vi.fn().mockImplementation(() => new Promise<void>(() => {})); // never resolves
- const f = factory({ handlerTimeoutMs: 30_000, drainTimeoutMs: 300 });
+ const f = factory({ leaseDurationMs: 30_000, handlerTimeoutMs: 30_000, drainTimeoutMs: 300 });
f.register({ name: "hang-drain-job", pattern: FAST_PATTERN, handler, runHashTtlS: 3600 });
f.start();
diff --git a/backend/src/lib/cron/cron-job.test.ts b/backend/src/lib/cron/cron-job.test.ts
index 1fb3101610..1cde759afc 100644
--- a/backend/src/lib/cron/cron-job.test.ts
+++ b/backend/src/lib/cron/cron-job.test.ts
@@ -44,7 +44,8 @@ const makeFactory = (deps?: Partial<FakeDeps>) => {
enqueueIntervalMs: 100,
processIntervalMs: 100,
slotTtlMs: 200,
- leaseDurationMs: 1000
+ leaseDurationMs: 1000,
+ handlerTimeoutMs: 1000
}),
redis,
redlock
@@ -85,6 +86,134 @@ describe("register", () => {
expect(redis.eval).not.toHaveBeenCalled();
await stop();
});
+
+ test("throws when per-entry handlerTimeoutMs exceeds factory leaseDurationMs", () => {
+ const { register } = makeFactory(); // factory leaseDurationMs=1000
+ expect(() =>
+ register({
+ name: "x",
+ pattern: "0 0 * * *",
+ handler: vi.fn(),
+ runHashTtlS: 3600,
+ handlerTimeoutMs: 5_000
+ })
+ ).toThrow(/handlerTimeoutMs.*must be <=.*leaseDurationMs/);
+ });
+
+ test("accepts per-entry handlerTimeoutMs when matched by per-entry leaseDurationMs", () => {
+ const { register } = makeFactory();
+ expect(() =>
+ register({
+ name: "x",
+ pattern: "0 0 * * *",
+ handler: vi.fn(),
+ runHashTtlS: 3600,
+ handlerTimeoutMs: 30 * 60_000,
+ leaseDurationMs: 30 * 60_000
+ })
+ ).not.toThrow();
+ });
+
+ test("throws when factory handlerTimeoutMs default exceeds factory leaseDurationMs", () => {
+ const redis = makeRedis();
+ const redlock = makeRedlock();
+ const { register } = cronJobFactory({
+ redis: redis as never,
+ redlock: redlock as never,
+ slotRefreshMs: 50,
+ enqueueIntervalMs: 100,
+ processIntervalMs: 100,
+ slotTtlMs: 200,
+ // No per-knob overrides — handlerTimeoutMs (default 5min) > leaseDurationMs (1s).
+ leaseDurationMs: 1000
+ });
+ expect(() => register({ name: "x", pattern: "0 0 * * *", handler: vi.fn(), runHashTtlS: 3600 })).toThrow(
+ /handlerTimeoutMs.*must be <=.*leaseDurationMs/
+ );
+ });
+});
+
+describe("per-entry lease duration", () => {
+ test("redlock.using receives per-entry leaseDurationMs when set", async () => {
+ vi.setSystemTime(new Date("2024-01-01T00:00:30Z"));
+ const enqueuedAt = Date.parse("2024-01-01T00:00:00Z");
+
+ const redis = makeRedis();
+ redis.set.mockResolvedValue("OK");
+ redis.eval.mockResolvedValue(1);
+ redis.zrangebyscore.mockResolvedValue([`x:${enqueuedAt}`]);
+ redis.hgetall.mockResolvedValue({
+ name: "x",
+ status: "pending",
+ attempts: "0",
+ enqueued_at_ms: "0"
+ });
+
+ const redlock = makeRedlock();
+ const f = cronJobFactory({
+ redis: redis as never,
+ redlock: redlock as never,
+ slotRefreshMs: 50,
+ enqueueIntervalMs: 100,
+ processIntervalMs: 100,
+ slotTtlMs: 200,
+ leaseDurationMs: 1000,
+ handlerTimeoutMs: 1000
+ });
+ f.register({
+ name: "x",
+ pattern: "* * * * *",
+ handler: vi.fn(),
+ runHashTtlS: 3600,
+ handlerTimeoutMs: 30 * 60_000,
+ leaseDurationMs: 30 * 60_000
+ });
+ f.start();
+ await vi.advanceTimersByTimeAsync(300);
+
+ expect(redlock.using).toHaveBeenCalled();
+ const [, durationArg] = redlock.using.mock.calls[0] as [unknown, number, unknown];
+ expect(durationArg).toBe(30 * 60_000);
+
+ await f.stop();
+ });
+
+ test("redlock.using falls back to factory leaseDurationMs when entry omits it", async () => {
+ vi.setSystemTime(new Date("2024-01-01T00:00:30Z"));
+ const enqueuedAt = Date.parse("2024-01-01T00:00:00Z");
+
+ const redis = makeRedis();
+ redis.set.mockResolvedValue("OK");
+ redis.eval.mockResolvedValue(1);
+ redis.zrangebyscore.mockResolvedValue([`x:${enqueuedAt}`]);
+ redis.hgetall.mockResolvedValue({
+ name: "x",
+ status: "pending",
+ attempts: "0",
+ enqueued_at_ms: "0"
+ });
+
+ const redlock = makeRedlock();
+ const f = cronJobFactory({
+ redis: redis as never,
+ redlock: redlock as never,
+ slotRefreshMs: 50,
+ enqueueIntervalMs: 100,
+ processIntervalMs: 100,
+ slotTtlMs: 200,
+ leaseDurationMs: 1234,
+ handlerTimeoutMs: 1000
+ });
+ f.register({ name: "x", pattern: "* * * * *", handler: vi.fn(), runHashTtlS: 3600 });
+ f.start();
+ await vi.advanceTimersByTimeAsync(300);
+
+ expect(redlock.using).toHaveBeenCalled();
+ const [, durationArg] = redlock.using.mock.calls[0] as [unknown, number, unknown];
+ expect(durationArg).toBe(1234);
+
+ await f.stop();
+ });
});
describe("slot election", () => {
@@ -379,6 +508,7 @@ describe("min-age gate", () => {
processIntervalMs: 100,
slotTtlMs: 200,
leaseDurationMs: 1000,
+ handlerTimeoutMs: 1000,
minProcessAgeMs: 500
});
f.register({ name: "x", pattern: "* * * * *", handler: vi.fn(), runHashTtlS: 3600 });
@@ -458,7 +588,9 @@ describe("stop", () => {
enqueueIntervalMs: 100,
processIntervalMs: 100,
slotTtlMs: 200,
- leaseDurationMs: 1000,
+ // Lease must cover handlerTimeoutMs; raised together so the
+ // registration-time invariant accepts the entry.
+ leaseDurationMs: 60_000,
handlerTimeoutMs: 60_000,
drainTimeoutMs: 5_000
});
@@ -509,7 +641,7 @@ describe("stop", () => {
enqueueIntervalMs: 100,
processIntervalMs: 100,
slotTtlMs: 200,
- leaseDurationMs: 1000,
+ leaseDurationMs: 60_000,
// Long enough that handlerTimeoutMs doesn't naturally drain inFlight.
handlerTimeoutMs: 60_000,
drainTimeoutMs: 200