From a2caf004b4853d23d3c9d47ad4036f12b909eb5f Mon Sep 17 00:00:00 2001 From: Jason Roy Date: Thu, 29 Oct 2026 20:28:34 -0700 Subject: [PATCH 1/4] test: preserve hard-expiry boundary regression --- .../src/data-cache/expiry-boundary.test.ts | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 packages/cache-handler/src/data-cache/expiry-boundary.test.ts diff --git a/packages/cache-handler/src/data-cache/expiry-boundary.test.ts b/packages/cache-handler/src/data-cache/expiry-boundary.test.ts new file mode 100644 index 0000000..8c1225c --- /dev/null +++ b/packages/cache-handler/src/data-cache/expiry-boundary.test.ts @@ -0,0 +1,64 @@ +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; +import { asClient, resetCacheState } from "../testing/env.js"; +import { FakeRedis } from "../testing/fake-redis.js"; +import { createRemoteCacheHandler } from "./remote.js"; +import type { DataCacheEntry } from "./types.js"; + +describe("remote data entry render-start expiry", () => { + beforeEach(async () => { + await resetCacheState(); + vi.useFakeTimers({ toFake: ["Date"] }); + vi.setSystemTime(1_800_000_000_000); + }); + + afterEach(async () => { + vi.useRealTimers(); + vi.restoreAllMocks(); + await resetCacheState(); + }); + + describe.each(["memory", "redis", "outage"] as const)("%s", (lane) => { + // Memory and outage fallback currently serve past expire after rounding the remaining TTL up. + const check = lane === "redis" ? test : test.fails; + check("never serves past expire when rendering finishes between whole seconds", async () => { + const redis = new FakeRedis(); + const handler = createRemoteCacheHandler( + lane === "memory" ? {} : { client: asClient(redis) }, + ); + if (lane === "outage") { + vi.spyOn(console, "error").mockImplementation(() => {}); + vi.spyOn(console, "warn").mockImplementation(() => {}); + // Establish the connection before taking it down mid-render. + await handler.get("entry", []); + } + const started = Date.now(); + let finish!: (entry: DataCacheEntry) => void; + const writing = handler.set( + "entry", + new Promise((resolve) => { + finish = resolve; + }), + ); + vi.setSystemTime(started + 250); + if (lane === "outage") redis.goDown(); + finish({ + value: new Response("rendered before expiry").body as ReadableStream, + timestamp: started, + tags: [], + stale: 0, + revalidate: 1, + expire: 2, + }); + await writing; + + // SWR remains allowed after revalidate, but only until the render's hard deadline. + vi.setSystemTime(started + 1_999); + const stale = await handler.get("entry", []); + expect(stale?.timestamp).toBe(started); + expect(await new Response(stale?.value).text()).toBe("rendered before expiry"); + + vi.setSystemTime(started + 2_001); + expect(await handler.get("entry", [])).toBeUndefined(); + }); + }); +}); From 7f2ec346b8ff1cf1bbb4b05657ac3a6227fd3e0f Mon Sep 17 00:00:00 2001 From: Jason Roy Date: Sun, 1 Nov 2026 19:42:29 -0800 Subject: [PATCH 2/4] test: exercise cross-process cache invalidation and lifetimes --- docs/development.md | 22 ++ .../src/data-cache/expiry-boundary.test.ts | 4 +- .../src/integration/concurrency.redis.test.ts | 363 ++++++++++++++++++ .../src/testing/concurrency-worker.mjs | 135 +++++++ .../cache-handler/src/testing/concurrency.ts | 92 +++++ 5 files changed, 613 insertions(+), 3 deletions(-) create mode 100644 packages/cache-handler/src/integration/concurrency.redis.test.ts create mode 100644 packages/cache-handler/src/testing/concurrency-worker.mjs create mode 100644 packages/cache-handler/src/testing/concurrency.ts diff --git a/docs/development.md b/docs/development.md index 7d8b609..a83b9ca 100644 --- a/docs/development.md +++ b/docs/development.md @@ -244,3 +244,25 @@ After the workflow has run, the repository owner should add these exact status c Select GitHub Actions as their source. Do not make the negative-control failure itself a required failed check: its job succeeds only when it observes the specific expected regression. Workflow changes do not change branch protection automatically. + +### Isolated-process invalidation tests + +The concurrency suite forks real Node processes with separate cache runtimes and controls render +completion through IPC. Memory runs deliver invalidations to each process explicitly; Redis runs +write the invalidation once and require peer reads to observe it. The suite covers full and postponed +page shells, build output, local and remote data, request timestamps, implicit path tags, delayed +invalidation and lifetime boundaries. These are handler-contract tests, not browser tests. + +```bash +CACHE_TEST_REDIS_URL=redis://localhost:6390 \ + pnpm --filter @mrjasonroy/cache-components-cache-handler exec vitest run \ + --no-file-parallelism --maxWorkers=1 \ + src/integration/concurrency.redis.test.ts src/data-cache/expiry-boundary.test.ts +``` + +Without `CACHE_TEST_REDIS_URL`, only the memory lane runs. The existing Redis integration CI matrix +runs both lanes. Six model tests exercise 180 deterministic seeds each (1,080 scenarios), varying +render start/completion order and invalidation placement across two processes. A failed assertion +includes the seed, handler, lane, process and operation trace. Reads allow the documented 100 ms local +tag-sync interval to elapse and assert the first result, without polling until fresh. Child processes, +temporary compiled modules, build fixtures and the test's uniquely prefixed Redis keys are cleaned up. diff --git a/packages/cache-handler/src/data-cache/expiry-boundary.test.ts b/packages/cache-handler/src/data-cache/expiry-boundary.test.ts index 8c1225c..dfa9da0 100644 --- a/packages/cache-handler/src/data-cache/expiry-boundary.test.ts +++ b/packages/cache-handler/src/data-cache/expiry-boundary.test.ts @@ -18,9 +18,7 @@ describe("remote data entry render-start expiry", () => { }); describe.each(["memory", "redis", "outage"] as const)("%s", (lane) => { - // Memory and outage fallback currently serve past expire after rounding the remaining TTL up. - const check = lane === "redis" ? test : test.fails; - check("never serves past expire when rendering finishes between whole seconds", async () => { + test("never serves past expire when rendering finishes between whole seconds", async () => { const redis = new FakeRedis(); const handler = createRemoteCacheHandler( lane === "memory" ? {} : { client: asClient(redis) }, diff --git a/packages/cache-handler/src/integration/concurrency.redis.test.ts b/packages/cache-handler/src/integration/concurrency.redis.test.ts new file mode 100644 index 0000000..b6bac66 --- /dev/null +++ b/packages/cache-handler/src/integration/concurrency.redis.test.ts @@ -0,0 +1,363 @@ +import { randomUUID } from "node:crypto"; +import { rmSync } from "node:fs"; +import { dirname } from "node:path"; +import { Redis } from "ioredis"; +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "vitest"; +import { + BLOG, + POSTS_FALLBACK, + appPageCtx, + fallbackCtx, + writeBuildOutput, +} from "../testing/build-output.js"; +import { CacheProcess, compileConcurrencyWorker } from "../testing/concurrency.js"; + +const url = process.env.CACHE_TEST_REDIS_URL; +const epoch = 1_800_000_000_000; +let compiled: ReturnType; +const processes: CacheProcess[] = []; +const buildDirs: string[] = []; +let raw: Redis | undefined; +let prefix: string; + +beforeAll(() => { + compiled = compileConcurrencyWorker(); +}, 120_000); +afterAll(() => { + compiled?.cleanup(); + raw?.disconnect(); +}); +beforeEach(() => { + prefix = `concurrency:${randomUUID()}:`; +}); +afterEach(async () => { + await Promise.all(processes.splice(0).map((worker) => worker.stop())); + for (const directory of buildDirs.splice(0)) rmSync(directory, { recursive: true, force: true }); + if (raw) { + const keys = await raw.keys(`${prefix}*`); + if (keys.length) await raw.del(...keys); + } +}); + +async function start(lane: string, extra: Record = {}) { + const worker = new CacheProcess(compiled.directory); + processes.push(worker); + if (lane === "redis" && !raw) raw = new Redis(url as string); + await worker.call({ + op: "init", + prefix, + now: epoch, + options: lane === "redis" ? { url } : {}, + ...extra, + }); + return worker; +} + +for (const lane of ["memory", "redis"]) { + describe.skipIf(lane === "redis" && !url)(`${lane} isolated processes`, () => { + test.each(["page-full", "page-postponed", "remote", "local"])( + "%s: overlapping same-key renders before, across and after an invalidation burst", + async (shape) => { + const kind = shape.startsWith("page") ? "page" : shape; + const a = await start(lane); + const b = await start(lane); + const invalidator = await start(lane); + const writers = [a, b]; + const begin = (worker: CacheProcess, request: string, now: number, value: string) => + worker.call({ + op: "begin", + kind, + key: "same", + request, + now, + value, + tags: ["burst"], + postponed: shape === "page-postponed", + }); + const read = (worker: CacheProcess, now: number) => + worker.call({ op: "read", kind, key: "same", request: `read-${now}`, now }); + for (const worker of writers) { + await begin(worker, "before", epoch, "before"); + await worker.call({ op: "finish", request: "before", now: epoch + 10 }); + expect((await read(worker, epoch + 20))?.value).toBe("before"); + } + // Eight genuinely pending renders, each page render on its own request handler. + for (let n = 0; n < 8; n++) await begin(writers[n % 2], `held-${n}`, epoch + 30 + n, "old"); + for (let n = 0; n < 4; n++) { + const command = { op: "invalidate", tags: ["burst"], now: epoch + 50 + n * 10 }; + await invalidator.call(command); + // Without a shared store there is no cross-process invalidation guarantee. + if (lane === "memory") for (const worker of writers) await worker.call(command); + } + await Promise.all( + writers.map(async (worker, index) => { + for (let n = index; n < 8; n += 2) + await worker.call({ op: "finish", request: `held-${n}`, now: epoch + 200 + n }); + }), + ); + for (const worker of writers) expect(await read(worker, epoch + 400)).toBeNull(); + for (const worker of writers) { + await begin(worker, "after", epoch + 500, "fresh"); + await worker.call({ op: "finish", request: "after", now: epoch + 510 }); + } + // No retry-until-fresh: every first read following the completed burst must be fresh. + for (const worker of writers) + expect((await read(worker, epoch + 600))?.value).toBe("fresh"); + }, + ); + + test.each(["full", "postponed"])( + "%s build output cannot resurrect after an invalidation", + async (shape) => { + const build = writeBuildOutput(); + buildDirs.push(dirname(build.serverDistDir)); + const key = shape === "full" ? BLOG : POSTS_FALLBACK; + const context = shape === "full" ? appPageCtx : fallbackCtx; + const tags = shape === "full" ? ["products"] : ["_N_T_/posts/[slug]/page"]; + const a = await start(lane, { serverDistDir: build.serverDistDir }); + const b = await start(lane, { serverDistDir: build.serverDistDir }); + const read = { op: "read", kind: "page", key, context, request: "built", now: epoch }; + expect(await a.call(read)).not.toBeNull(); + expect(await b.call(read)).not.toBeNull(); + await a.call({ op: "invalidate", tags, now: epoch + 1 }); + if (lane === "memory") await b.call({ op: "invalidate", tags, now: epoch + 1 }); + expect(await b.call({ ...read, now: epoch + 200 })).toBeNull(); + expect(await a.call({ ...read, now: epoch + 200 })).toBeNull(); + if (lane === "redis") { + const restarted = await start(lane, { serverDistDir: build.serverDistDir }); + expect(await restarted.call({ ...read, now: epoch + 300 })).toBeNull(); + } + }, + ); + + test.each(["page", "remote", "local"])( + "%s retains render start across later lookups and resets", + async (kind) => { + const worker = await start(lane); + const begin = { + op: "begin", + kind, + key: "timestamp", + request: "request", + tags: [], + value: "first", + }; + const read = { op: "read", kind, key: "timestamp", request: "request" }; + await worker.call({ ...begin, now: epoch }); + if (kind === "page") await worker.call({ ...read, now: epoch + 100 }); + await worker.call({ op: "finish", request: "request", now: epoch + 250 }); + expect((await worker.call({ ...read, now: epoch + 400 }))?.timestamp).toBe(epoch); + if (kind === "page") { + await worker.call({ ...begin, now: epoch + 500 }); + await worker.call({ op: "finish", request: "request", now: epoch + 600 }); + expect((await worker.call({ ...read, now: epoch + 700 }))?.timestamp).toBe(epoch); + await worker.call({ op: "reset", request: "request" }); + } + await worker.call({ ...begin, now: epoch + 800 }); + await worker.call({ op: "finish", request: "request", now: epoch + 900 }); + expect((await worker.call({ ...read, now: epoch + 1000 }))?.timestamp).toBe(epoch + 800); + }, + ); + test.each(["page", "remote", "local"])( + "%s: seeded interleavings match render-time freshness (180 seeds)", + async (kind) => { + const workers = [await start(lane), await start(lane)]; + expect((await workers[0].call({ op: "identity" }))?.pid).not.toBe( + (await workers[1].call({ op: "identity" }))?.pid, + ); + for (let seed = 1; seed <= 180; seed++) { + let state = seed; + const random = () => { + state = (Math.imul(state, 1664525) + 1013904223) >>> 0; + return state; + }; + let now = epoch + seed * 10000; + const key = `seed-${seed}`; + const tag = `tag-${seed}`; + let invalidated = 0; + const model: ({ value: string; timestamp: number } | null)[] = [null, null]; + const pending = new Map(); + const actions = ["begin-0", "begin-1", "invalidate", "invalidate"]; + const history: string[] = []; + while (actions.length) { + const action = actions.splice(random() % actions.length, 1)[0]; + history.push(action); + now += 150; // Permit the documented local-tag refresh interval to elapse. + if (action === "invalidate") { + invalidated = now; + await workers[0].call({ op: "invalidate", tags: [tag], now }); + if (lane === "memory") await workers[1].call({ op: "invalidate", tags: [tag], now }); + } else { + const index = Number(action.slice(-1)); + const request = `${key}-${index}`; + if (action.startsWith("begin")) { + const entry = { value: request, timestamp: now }; + pending.set(index, entry); + await workers[index].call({ + op: "begin", + kind, + key, + request, + tags: [tag], + now, + value: entry.value, + }); + actions.push(`finish-${index}`); + } else { + await workers[index].call({ op: "finish", request, now }); + const entry = pending.get(index); + if (!entry) throw new Error(`No pending render for seed ${seed}`); + pending.delete(index); + model[index] = entry; + if (lane === "redis" && kind !== "local") model[1 - index] = entry; + } + } + // get() waits for pending data writes; inspect each process once both writes complete. + if (pending.size === 0) { + now += 150; + for (let index = 0; index < 2; index++) { + const entry = model[index]; + const expected = entry && entry.timestamp >= invalidated ? entry.value : undefined; + const actual = await workers[index].call({ + op: "read", + kind, + key, + request: `read-${key}-${now}`, + now, + }); + expect( + actual?.value, + `seed=${seed} lane=${lane} kind=${kind} process=${index} trace=${history.join(",")}`, + ).toBe(expected); + } + } + } + } + }, + 120_000, + ); + + test.each(["page", "remote", "local"])( + "%s: delayed invalidation serves stale only before its hard deadline", + async (kind) => { + const a = await start(lane); + const b = await start(lane); + for (const worker of [a, b]) { + await worker.call({ + op: "begin", + kind, + key: "window", + request: "old", + tags: ["window"], + value: "old", + now: epoch, + }); + await worker.call({ op: "finish", request: "old", now: epoch + 20 }); + } + const invalidate = { + op: "invalidate", + tags: ["window"], + durations: { expire: 2 }, + now: epoch + 100, + }; + await a.call(invalidate); + if (lane === "memory") await b.call(invalidate); + for (const worker of [a, b]) { + const read = { op: "read", kind, key: "window", request: "window-read" }; + expect((await worker.call({ ...read, now: epoch + 2099 }))?.value).toBe("old"); + expect(await worker.call({ ...read, now: epoch + 2100 })).toBeNull(); + } + }, + ); + + test("implicit path invalidation reaches a peer page read", async () => { + const a = await start(lane); + const b = await start(lane); + await b.call({ + op: "begin", + kind: "page", + key: "path", + request: "path-render", + tags: [], + value: "old", + now: epoch, + }); + await b.call({ op: "finish", request: "path-render", now: epoch + 20 }); + const read = { + op: "read", + kind: "page", + key: "path", + request: "path-read", + context: { softTags: ["_N_T_/products"] }, + }; + expect((await b.call({ ...read, now: epoch + 30 }))?.value).toBe("old"); + const invalidate = { op: "invalidate", tags: ["_N_T_/products"], now: epoch + 100 }; + await a.call(invalidate); + if (lane === "memory") await b.call(invalidate); + expect(await b.call({ ...read, now: epoch + 200 })).toBeNull(); + }); + test.each(lane === "redis" ? ["remote", "local"] : ["page", "remote", "local"])( + "%s: cache lifetime boundaries use render start", + async (kind) => { + const worker = await start(lane); + await worker.call({ + op: "begin", + kind, + key: "life", + request: "life-render", + tags: [], + value: "old", + life: { stale: 1, revalidate: 2, expire: 3 }, + now: epoch, + }); + await worker.call({ op: "finish", request: "life-render", now: epoch + 250 }); + const read = { op: "read", kind, key: "life", request: "life-read" }; + for (const offset of [999, 1000, 1999]) + expect((await worker.call({ ...read, now: epoch + offset }))?.value).toBe("old"); + for (const offset of [2000, 2999]) { + const entry = await worker.call({ ...read, now: epoch + offset }); + if (kind === "local") expect(entry).toBeNull(); + else expect(entry?.value).toBe("old"); + } + expect(await worker.call({ ...read, now: epoch + 3000 })).toBeNull(); + }, + ); + if (lane === "redis") { + // Redis page TTL starts at write completion, so a slow render extends the hard lifetime. + test.fails("page hard expiry includes time spent rendering (real clocks)", async () => { + const worker = await start(lane, { realtime: true }); + const begun = await worker.call({ + op: "begin", + kind: "page", + key: "real-life", + request: "real-life", + tags: [], + value: "old", + life: { revalidate: 2, expire: 2 }, + }); + await new Promise((resolve) => setTimeout(resolve, 1200)); + await worker.call({ op: "finish", request: "real-life" }); + expect( + ( + await worker.call({ + op: "read", + kind: "page", + key: "real-life", + request: "before-expiry", + }) + )?.value, + ).toBe("old"); + const deadline = (begun?.timestamp ?? 0) + 2100; + await new Promise((resolve) => setTimeout(resolve, Math.max(0, deadline - Date.now()))); + expect( + await worker.call({ + op: "read", + kind: "page", + key: "real-life", + request: "after-expiry", + }), + ).toBeNull(); + }); + } + }); +} diff --git a/packages/cache-handler/src/testing/concurrency-worker.mjs b/packages/cache-handler/src/testing/concurrency-worker.mjs new file mode 100644 index 0000000..8bf20b7 --- /dev/null +++ b/packages/cache-handler/src/testing/concurrency-worker.mjs @@ -0,0 +1,135 @@ +import { createRequire } from "node:module"; +import { pathToFileURL } from "node:url"; + +// A real process with its own runtime, memory stores, tag state and Redis connection. +const compiled = pathToFileURL(`${process.argv[2]}/`); +const { createPageCacheHandler } = await import(new URL("handlers/redis.js", compiled)); +const { createLocalCacheHandler } = await import(new URL("data-cache/local.js", compiled)); +const { createRemoteCacheHandler } = await import(new URL("data-cache/remote.js", compiled)); +const { setClockForTests } = await import(new URL("shared/clock.js", compiled)); +const { closeCache } = await import(new URL("index.js", compiled)); +const require = createRequire(import.meta.url); +let now = Date.now(); +let realtime = false; +setClockForTests(() => (realtime ? Date.now() : now)); +let Page; +let local; +let remote; +let pageOptions; +const renders = new Map(); +const requests = new Map(); + +function page(request) { + if (!requests.has(request)) requests.set(request, new Page(pageOptions)); + return requests.get(request); +} + +async function execute(m) { + now = realtime ? Date.now() : (m.now ?? now); + const handler = m.kind === "local" ? local : remote; + switch (m.op) { + case "identity": + return { pid: process.pid }; + case "init": { + realtime = m.realtime ?? false; + await closeCache(); + requests.clear(); + renders.clear(); + if (m.deployment) process.env.NEXT_DEPLOYMENT_ID = m.deployment; + const options = { keyPrefix: m.prefix, commandTimeout: 100, ...m.options }; + Page = createPageCacheHandler(options); + pageOptions = m.serverDistDir + ? { + serverDistDir: m.serverDistDir, + fs: require("next/dist/server/lib/node-fs-methods.js").nodeFs, + } + : {}; + local = createLocalCacheHandler(options); + remote = createRemoteCacheHandler(options); + await local.refreshTags(); + return { pid: process.pid }; + } + case "begin": { + const timestamp = now; + if (m.kind === "page") { + await page(m.request).get(m.key, m.context); + renders.set(m.request, { ...m, timestamp }); + } else { + let resolve; + const pending = new Promise((done) => { + resolve = done; + }); + const writing = handler.set(m.key, pending); + renders.set(m.request, { ...m, timestamp, resolve, writing }); + } + return { timestamp }; + } + case "finish": { + const render = renders.get(m.request); + if (!render) throw new Error(`Unknown render ${m.request}`); + if (render.kind === "page") { + await page(m.request).set( + render.key, + { + kind: "APP_PAGE", + html: render.value, + rscData: Buffer.from(render.value), + headers: { "x-next-cache-tags": render.tags.join(",") }, + postponed: render.postponed ? "held-shell" : undefined, + status: 200, + }, + { cacheControl: render.life ?? { revalidate: 60, expire: 300 } }, + ); + } else { + render.resolve({ + value: new Response(render.value).body, + timestamp: render.timestamp, + tags: render.tags, + stale: 0, + revalidate: 60, + expire: 300, + ...render.life, + }); + await render.writing; + } + renders.delete(m.request); + return {}; + } + case "read": { + if (m.kind === "page") { + const entry = await page(m.request).get(m.key, m.context); + return entry ? { value: entry.value.html, timestamp: entry.lastModified } : null; + } + await handler.refreshTags(); + const entry = await handler.get(m.key, m.softTags ?? []); + return entry + ? { + value: await new Response(entry.value).text(), + timestamp: entry.timestamp, + revalidate: entry.revalidate, + stale: entry.stale, + expire: entry.expire, + } + : null; + } + case "invalidate": + await page("invalidator").revalidateTag(m.tags, m.durations ?? { expire: 0 }); + return {}; + case "reset": + page(m.request).resetRequestCache(); + return {}; + case "close": + await closeCache(); + return {}; + default: + throw new Error(`Unknown operation ${m.op}`); + } +} + +process.on("message", async (message) => { + try { + process.send({ id: message.id, result: await execute(message) }); + } catch (error) { + process.send({ id: message.id, error: error.stack ?? String(error) }); + } +}); diff --git a/packages/cache-handler/src/testing/concurrency.ts b/packages/cache-handler/src/testing/concurrency.ts new file mode 100644 index 0000000..8ec7a62 --- /dev/null +++ b/packages/cache-handler/src/testing/concurrency.ts @@ -0,0 +1,92 @@ +import { type ChildProcess, execFileSync, fork } from "node:child_process"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +const packageDir = join(dirname(fileURLToPath(import.meta.url)), "..", ".."); + +export function compileConcurrencyWorker(): { directory: string; cleanup: () => void } { + const directory = mkdtempSync(join(packageDir, "node_modules", ".concurrency-")); + execFileSync(join(packageDir, "node_modules", ".bin", "tsc"), [ + "-p", + join(packageDir, "tsconfig.json"), + "--outDir", + directory, + "--declaration", + "false", + "--declarationMap", + "false", + "--sourceMap", + "false", + ]); + return { directory, cleanup: () => rmSync(directory, { recursive: true, force: true }) }; +} + +export interface Reply { + value?: string; + timestamp?: number; + revalidate?: number; + pid?: number; +} + +/** Bounded IPC; a crash rejects outstanding calls instead of hanging the suite. */ +export class CacheProcess { + private child: ChildProcess; + private serial = 0; + private pending = new Map< + number, + { resolve: (value: Reply | null) => void; reject: (error: Error) => void } + >(); + private stderr = ""; + + constructor(directory: string) { + this.child = fork(join(packageDir, "src/testing/concurrency-worker.mjs"), [directory], { + execArgv: [], + env: { PATH: process.env.PATH, NODE_ENV: "test" }, + stdio: ["ignore", "ignore", "pipe", "ipc"], + }); + this.child.stderr?.on("data", (data) => { + this.stderr = (this.stderr + data).slice(-8000); + }); + this.child.on("message", (reply: { id: number; result: Reply | null; error?: string }) => { + const pending = this.pending.get(reply.id); + if (!pending) return; + this.pending.delete(reply.id); + if (reply.error) pending.reject(new Error(reply.error)); + else pending.resolve(reply.result); + }); + this.child.on("exit", () => { + for (const pending of this.pending.values()) + pending.reject(new Error(`Worker exited: ${this.stderr}`)); + this.pending.clear(); + }); + } + + async call(message: Record): Promise { + const id = ++this.serial; + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + this.pending.delete(id); + reject(new Error(`IPC timeout: ${JSON.stringify(message)} ${this.stderr}`)); + }, 10_000); + this.pending.set(id, { + resolve: (value) => { + clearTimeout(timer); + resolve(value); + }, + reject: (error) => { + clearTimeout(timer); + reject(error); + }, + }); + this.child.send({ ...message, id }); + }); + } + + async stop(): Promise { + if (this.child.exitCode !== null || this.child.signalCode !== null) return; + const exited = new Promise((resolve) => this.child.once("exit", () => resolve())); + this.child.kill("SIGKILL"); + await exited; + } +} From c027d58d426f380449643f6b9893ba2fdb90c312 Mon Sep 17 00:00:00 2001 From: Jason Roy Date: Thu, 5 Nov 2026 20:56:01 -0800 Subject: [PATCH 3/4] test: fix review findings in the cross-process suite - Stamp build-output fixtures relative to the workers' fixed clock, so the resurrection cases stop depending on today's date. - Split the known page hard-expiry bug: setup and positive checks run as an ordinary test; test.fails asserts only the stored Redis TTL against render start, instead of a timed read that a slow run could flip. - Remove the compiled worker directory when tsc fails. - Renumber the duplicate DEC-16 (#106's entry is DEC-17, the runner pin DEC-18). Co-Authored-By: Claude Opus 5.5 --- implementation/DECISIONS.md | 4 +- packages/cache-handler/node_modules | 1 + .../src/integration/concurrency.redis.test.ts | 43 +++++++++---------- .../cache-handler/src/testing/concurrency.ts | 32 ++++++++------ 4 files changed, 43 insertions(+), 37 deletions(-) create mode 120000 packages/cache-handler/node_modules diff --git a/implementation/DECISIONS.md b/implementation/DECISIONS.md index 86f56d1..a6fd67e 100644 --- a/implementation/DECISIONS.md +++ b/implementation/DECISIONS.md @@ -7,7 +7,7 @@ > or "the 16.5 path" is the legacy `createCacheHandler` data stack and the `redis: RedisOptions` > form. The headings of DEC-1 and DEC-2 keep the numbers those releases had. -## DEC-17: CI runs on `ubuntu-24.04`, not `ubuntu-latest` +## DEC-18: CI runs on `ubuntu-24.04`, not `ubuntu-latest` On 31 Oct 2026 `ubuntu-latest` moved to Ubuntu 26.04, and the locked Playwright (1.56.1) refuses to install Chromium there (`Playwright does not support chromium on ubuntu26.04-x64`). The daily @@ -16,7 +16,7 @@ no version bump. Every job now names `ubuntu-24.04`, so a runner image change ca the release loop unannounced. Move to a newer image deliberately, with a Playwright that supports it, in one PR that CI proves. -## DEC-16: Opt-in Redis storage for plain `"use cache"` +## DEC-17: Opt-in Redis storage for plain `"use cache"` Restore `withCache`'s `useCache: "memory" | "redis"` with `"memory"` as the default. This supersedes DEC-13's removal of that option only. Applications can share existing plain cache diff --git a/packages/cache-handler/node_modules b/packages/cache-handler/node_modules new file mode 120000 index 0000000..252a0e4 --- /dev/null +++ b/packages/cache-handler/node_modules @@ -0,0 +1 @@ +/Users/jroy/Documents/GitHub/cache-components-cache-handler/packages/cache-handler/node_modules \ No newline at end of file diff --git a/packages/cache-handler/src/integration/concurrency.redis.test.ts b/packages/cache-handler/src/integration/concurrency.redis.test.ts index b6bac66..f7584a0 100644 --- a/packages/cache-handler/src/integration/concurrency.redis.test.ts +++ b/packages/cache-handler/src/integration/concurrency.redis.test.ts @@ -109,7 +109,8 @@ for (const lane of ["memory", "redis"]) { test.each(["full", "postponed"])( "%s build output cannot resurrect after an invalidation", async (shape) => { - const build = writeBuildOutput(); + // Built an hour before the workers' fixed clock, not the real one. + const build = writeBuildOutput({ ageMs: Date.now() - epoch + 60 * 60 * 1000 }); buildDirs.push(dirname(build.serverDistDir)); const key = shape === "full" ? BLOG : POSTS_FALLBACK; const context = shape === "full" ? appPageCtx : fallbackCtx; @@ -324,7 +325,11 @@ for (const lane of ["memory", "redis"]) { ); if (lane === "redis") { // Redis page TTL starts at write completion, so a slow render extends the hard lifetime. - test.fails("page hard expiry includes time spent rendering (real clocks)", async () => { + // The setup and its positive checks run as an ordinary test; only the known bug is + // asserted under test.fails, against the stored TTL rather than a timed read. + let renderMs: number | undefined; + let storedTtlMs: number | undefined; + test("page hard expiry setup: a slow render is stored and readable", async () => { const worker = await start(lane, { realtime: true }); const begun = await worker.call({ op: "begin", @@ -337,26 +342,20 @@ for (const lane of ["memory", "redis"]) { }); await new Promise((resolve) => setTimeout(resolve, 1200)); await worker.call({ op: "finish", request: "real-life" }); - expect( - ( - await worker.call({ - op: "read", - kind: "page", - key: "real-life", - request: "before-expiry", - }) - )?.value, - ).toBe("old"); - const deadline = (begun?.timestamp ?? 0) + 2100; - await new Promise((resolve) => setTimeout(resolve, Math.max(0, deadline - Date.now()))); - expect( - await worker.call({ - op: "read", - kind: "page", - key: "real-life", - request: "after-expiry", - }), - ).toBeNull(); + renderMs = Date.now() - (begun?.timestamp ?? 0); + const read = { op: "read", kind: "page", key: "real-life", request: "before-expiry" }; + expect((await worker.call(read))?.value).toBe("old"); + const ttls = await Promise.all( + (await raw?.keys(`${prefix}*`))?.map((key) => raw?.pttl(key)) ?? [], + ); + const expiring = ttls.filter((ttl): ttl is number => (ttl ?? 0) > 0); + expect(expiring.length).toBeGreaterThan(0); + storedTtlMs = Math.max(...expiring); + }); + // If the setup test fails, it fails the suite on its own. + test.fails("page hard expiry includes time spent rendering (real clocks)", () => { + // 300ms covers the gap between finish and the PTTL read. + expect(storedTtlMs).toBeLessThanOrEqual(2000 - (renderMs ?? 0) + 300); }); } }); diff --git a/packages/cache-handler/src/testing/concurrency.ts b/packages/cache-handler/src/testing/concurrency.ts index 8ec7a62..575083e 100644 --- a/packages/cache-handler/src/testing/concurrency.ts +++ b/packages/cache-handler/src/testing/concurrency.ts @@ -7,19 +7,25 @@ const packageDir = join(dirname(fileURLToPath(import.meta.url)), "..", ".."); export function compileConcurrencyWorker(): { directory: string; cleanup: () => void } { const directory = mkdtempSync(join(packageDir, "node_modules", ".concurrency-")); - execFileSync(join(packageDir, "node_modules", ".bin", "tsc"), [ - "-p", - join(packageDir, "tsconfig.json"), - "--outDir", - directory, - "--declaration", - "false", - "--declarationMap", - "false", - "--sourceMap", - "false", - ]); - return { directory, cleanup: () => rmSync(directory, { recursive: true, force: true }) }; + const cleanup = () => rmSync(directory, { recursive: true, force: true }); + try { + execFileSync(join(packageDir, "node_modules", ".bin", "tsc"), [ + "-p", + join(packageDir, "tsconfig.json"), + "--outDir", + directory, + "--declaration", + "false", + "--declarationMap", + "false", + "--sourceMap", + "false", + ]); + } catch (error) { + cleanup(); + throw error; + } + return { directory, cleanup }; } export interface Reply { From bd66c2d457bd6c29a3fd35c102a9c480325d66a0 Mon Sep 17 00:00:00 2001 From: Jason Roy Date: Thu, 5 Nov 2026 20:56:09 -0800 Subject: [PATCH 4/4] chore: drop an accidentally committed node_modules symlink Co-Authored-By: Claude Opus 5.5 --- packages/cache-handler/node_modules | 1 - 1 file changed, 1 deletion(-) delete mode 120000 packages/cache-handler/node_modules diff --git a/packages/cache-handler/node_modules b/packages/cache-handler/node_modules deleted file mode 120000 index 252a0e4..0000000 --- a/packages/cache-handler/node_modules +++ /dev/null @@ -1 +0,0 @@ -/Users/jroy/Documents/GitHub/cache-components-cache-handler/packages/cache-handler/node_modules \ No newline at end of file