|
4 | 4 | clearCronScheduleCacheForTest, |
5 | 5 | computeNextRunAtMs, |
6 | 6 | computePreviousRunAtMs, |
| 7 | + getCronScheduleCacheMaxForTest, |
7 | 8 | getCronScheduleCacheSizeForTest, |
| 9 | + hasCronInCacheForTest, |
8 | 10 | } from "./schedule.js"; |
9 | 11 |
|
10 | 12 | describe("cron schedule", () => { |
@@ -144,6 +146,39 @@ describe("cron schedule", () => { |
144 | 146 | expect(getCronScheduleCacheSizeForTest()).toBe(2); |
145 | 147 | }); |
146 | 148 |
|
| 149 | + 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 | + |
147 | 182 | describe("cron with specific seconds (6-field pattern)", () => { |
148 | 183 | // Pattern: fire at exactly second 0 of minute 0 of hour 12 every day |
149 | 184 | const dailyNoon = { kind: "cron" as const, expr: "0 0 12 * * *", tz: "UTC" }; |
|
0 commit comments