






















@@ -4,7 +4,9 @@ import {
44clearCronScheduleCacheForTest,
55computeNextRunAtMs,
66computePreviousRunAtMs,
7+getCronScheduleCacheMaxForTest,
78getCronScheduleCacheSizeForTest,
9+hasCronInCacheForTest,
810} from "./schedule.js";
9111012describe("cron schedule", () => {
@@ -144,6 +146,39 @@ describe("cron schedule", () => {
144146expect(getCronScheduleCacheSizeForTest()).toBe(2);
145147});
146148149+it("promotes accessed entries to avoid premature LRU eviction", () => {
150+const nowMs = Date.parse("2026-03-01T00:00:00.000Z");
151+const cacheMax = getCronScheduleCacheMaxForTest();
152+153+// Fill cache to capacity with unique expressions.
154+// i=0 → "0 0 * * *", i=1 → "1 0 * * *", ..., i=511 → "31 8 * * *"
155+for (let i = 0; i < cacheMax; i++) {
156+computeNextRunAtMs(
157+{ kind: "cron", expr: `${i % 60} ${Math.floor(i / 60)} * * *`, tz: "UTC" },
158+nowMs,
159+);
160+}
161+expect(getCronScheduleCacheSizeForTest()).toBe(cacheMax);
162+163+// Entry #0 ("0 0 * * *") is the oldest by insertion order.
164+// Access it so LRU promotes it (delete + re-insert at end of Map).
165+computeNextRunAtMs({ kind: "cron", expr: "0 0 * * *", tz: "UTC" }, nowMs);
166+167+// Entry #1 ("1 0 * * *") is now the least-recently-used.
168+// Insert a new entry to trigger one eviction.
169+computeNextRunAtMs({ kind: "cron", expr: "0 0 1 1 *", tz: "UTC" }, nowMs);
170+expect(getCronScheduleCacheSizeForTest()).toBe(cacheMax);
171+172+// Under LRU: entry #0 survived (was promoted), entry #1 was evicted.
173+// Under FIFO: entry #0 would be evicted instead — this assertion would fail.
174+expect(hasCronInCacheForTest("0 0 * * *", "UTC")).toBe(true);
175+expect(hasCronInCacheForTest("1 0 * * *", "UTC")).toBe(false);
176+177+// The new entry and a non-evicted middle entry should both be present.
178+expect(hasCronInCacheForTest("0 0 1 1 *", "UTC")).toBe(true);
179+expect(hasCronInCacheForTest("2 0 * * *", "UTC")).toBe(true);
180+});
181+147182describe("cron with specific seconds (6-field pattern)", () => {
148183// Pattern: fire at exactly second 0 of minute 0 of hour 12 every day
149184const dailyNoon = { kind: "cron" as const, expr: "0 0 12 * * *", tz: "UTC" };
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。