Unverified Commit 42ea2b32 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Cover cache sharing after handler failures (#100519)

The existing handler-error tests cover error reporting and rendering
after a failed write. The new tests also cover intra-request lookup
retries and a joiner that needs a different root key after the
producer's write fails.

A held write verifies that the retried calls can consume the same
generated value before persistence finishes. The root-param case
verifies that the joiner persists and reuses its own value. Both cases
pass before the wrapper rewrite in #100511.
parent 589aae73
import { randomUUID } from 'crypto'
import { nextTestSetup } from 'e2e-utils'
import { hasErrorToast, retry, waitFor, waitForNoRedbox } from 'next-test-utils'
import stripAnsi from 'strip-ansi'
......@@ -130,3 +131,64 @@ describe('app-custom-cache-handler-errors - set throws', () => {
expect(cliOutput).toIncludeRepeated(escapeStringRegexp(errorBlock), 1)
})
})
// This suite checks the configured handler without development's in-memory
// front handler. The control route and handler must share a server process.
// @force-gate start
describe('app-custom-cache-handler-errors - lookup recovery', () => {
const { next } = nextTestSetup({
files: __dirname,
env: { CACHE_HANDLER_THROW_ON: 'retry-get' },
})
it('retries a shared failed lookup and shares its successful fill while the write is pending', async () => {
const id = randomUUID()
const outputIndex = next.cliOutput.length
const control = (action: string) =>
next.fetch(`/lookup-retry?id=${id}`, { method: 'POST', body: action })
const readState = () =>
next.fetch('/lookup-retry?state').then(
(response) =>
response.json() as Promise<{
lookupCount: number
writeStarted: boolean
writeCompleted: boolean
cacheReadsCompleted: boolean
}>
)
await control('fail-lookup-and-hold-write')
const rendering = next.fetch(`/lookup-retry?id=${id}`)
try {
// The retry must resolve before `set()` completes. A lookup count of two
// also checks that the initial calls shared the rejected `get()`.
await retry(async () => {
expect(await readState()).toEqual({
lookupCount: 2,
writeStarted: true,
writeCompleted: false,
cacheReadsCompleted: true,
})
})
await control('release-write')
const response = await rendering
expect(response.status).toBe(200)
const body = await response.json()
// Both initial calls must reject even though only one `get()` throws.
expect(body.failures).toEqual([
'expected cache lookup failure',
'expected cache lookup failure',
])
expect(body.first).toEqual(expect.any(Number))
expect(body.second).toBe(body.first)
await retry(async () => {
expect((await readState()).writeCompleted).toBe(true)
})
expect(next.cliOutput.slice(outputIndex)).not.toContain(
'unhandledRejection'
)
} finally {
await control('release-write')
await rendering
}
})
})
import { cacheLife, cacheTag } from 'next/cache'
import { connection } from 'next/server'
import lookupRetryState from '../../lookup-retry-state'
async function readCachedValue(id: string) {
'use cache'
cacheLife('hours')
cacheTag(`lookup-retry:${id}`)
return Math.random()
}
export async function GET(request: Request) {
await connection()
if (new URL(request.url).searchParams.has('state')) {
return Response.json({
lookupCount: lookupRetryState.lookupCount,
writeStarted: lookupRetryState.writeStarted,
writeCompleted: lookupRetryState.writeCompleted,
cacheReadsCompleted: lookupRetryState.cacheReadsCompleted,
})
}
const id = new URL(request.url).searchParams.get('id')
if (id === null) {
return new Response(null, { status: 400 })
}
// Both concurrent calls must reject from the same handler `get()`.
const results = await Promise.allSettled([
readCachedValue(id),
readCachedValue(id),
])
// Retry in the same request before `pendingWrite` resolves. The second read
// must reuse the generated value while `set()` is still pending.
const first = await readCachedValue(id)
const second = await readCachedValue(id)
lookupRetryState.cacheReadsCompleted = true
return Response.json({
failures: results.map((result) =>
result.status === 'rejected' ? result.reason.message : 'fulfilled'
),
first,
second,
})
}
export async function POST(request: Request) {
switch (await request.text()) {
case 'fail-lookup-and-hold-write': {
const id = new URL(request.url).searchParams.get('id')
if (id === null) {
return new Response(null, { status: 400 })
}
lookupRetryState.id = id
lookupRetryState.failNextLookup = true
lookupRetryState.lookupCount = 0
lookupRetryState.writeStarted = false
lookupRetryState.writeCompleted = false
lookupRetryState.cacheReadsCompleted = false
lookupRetryState.pendingWrite = new Promise((resolve) => {
lookupRetryState.releaseWrite = resolve
})
break
}
case 'release-write':
lookupRetryState.releaseWrite?.()
lookupRetryState.releaseWrite = null
lookupRetryState.pendingWrite = null
break
default:
return new Response(null, { status: 400 })
}
return new Response(null, { status: 204 })
}
// @ts-check
/**
* The route and cache handler share these lookup and write controls.
*
* @typedef {object} LookupRetryState
* @property {string} id
* @property {boolean} failNextLookup
* @property {number} lookupCount
* @property {boolean} writeStarted
* @property {boolean} writeCompleted
* @property {boolean} cacheReadsCompleted
* Records that the route's cached calls have settled.
* @property {Promise<void> | null} pendingWrite
* Keeps `set()` pending after it receives the cache entry.
* @property {(() => void) | null} releaseWrite
*/
/**
* @type {typeof globalThis & { lookupRetryState?: LookupRetryState }}
*/
const reference = globalThis
module.exports = reference.lookupRetryState ??= {
id: '',
failNextLookup: false,
lookupCount: 0,
writeStarted: false,
writeCompleted: false,
cacheReadsCompleted: false,
pendingWrite: null,
releaseWrite: null,
}
......@@ -4,7 +4,10 @@
const nextConfig = {
cacheComponents: true,
cacheHandlers: {
default: require.resolve('./throwing-cache-handler.js'),
default:
process.env.CACHE_HANDLER_THROW_ON === 'retry-get'
? require.resolve('./retrying-cache-handler.js')
: require.resolve('./throwing-cache-handler.js'),
},
}
......
// @ts-check
// This handler rejects the first `get()` for the selected key. It pauses
// `set()` so the test can check that cached calls resolve before it completes.
const cacheHandler = require('./throwing-cache-handler')
const lookupRetryState = require('./lookup-retry-state')
/**
* @type {import('next/cache').CacheHandler}
*/
module.exports = {
...cacheHandler,
async get(cacheKey, softTags) {
if (
lookupRetryState.id !== '' &&
cacheKey.includes(JSON.stringify(lookupRetryState.id))
) {
lookupRetryState.lookupCount++
if (lookupRetryState.failNextLookup) {
lookupRetryState.failNextLookup = false
throw new Error('expected cache lookup failure')
}
}
return cacheHandler.get(cacheKey)
},
async set(cacheKey, pendingEntry) {
const entry = await pendingEntry
if (!entry.tags.includes(`lookup-retry:${lookupRetryState.id}`)) {
return cacheHandler.set(cacheKey, pendingEntry)
}
lookupRetryState.writeStarted = true
await lookupRetryState.pendingWrite
await cacheHandler.set(cacheKey, pendingEntry)
lookupRetryState.writeCompleted = true
},
}
import { cacheLife, cacheTag } from 'next/cache'
import { lang } from 'next/root-params'
import { connection } from 'next/server'
import writeFailureState from '../../../../write-failure-state'
async function readCachedValue(id: string) {
'use cache'
cacheLife('hours')
const language = await lang()
cacheTag(`write-failure:${id}`, `write-failure-language:${language}`)
writeFailureState.generatedLanguages.push(language)
await writeFailureState.pendingFill
return `${language}:${crypto.randomUUID()}`
}
export default async function Page({
params,
searchParams,
}: {
params: Promise<{ countryCode: string; lang: string }>
searchParams: Promise<{ id?: string | string[] }>
}) {
await connection()
const { id } = await searchParams
if (id === undefined) {
return null
}
if (typeof id !== 'string') {
throw new Error('Expected a single id search parameter')
}
const { lang: language } = await params
writeFailureState.requestedLanguages.push(language)
return <p id="value">{await readCachedValue(id)}</p>
}
import { connection } from 'next/server'
import writeFailureState from '../../write-failure-state'
export async function GET() {
await connection()
return Response.json({
requestedLanguages: writeFailureState.requestedLanguages,
generatedLanguages: writeFailureState.generatedLanguages,
failedWriteLanguages: writeFailureState.failedWriteLanguages,
writtenLanguages: writeFailureState.writtenLanguages,
})
}
export async function POST(request: Request) {
switch (await request.text()) {
case 'hold-fill': {
const id = new URL(request.url).searchParams.get('id')
if (id === null) {
return new Response(null, { status: 400 })
}
writeFailureState.id = id
writeFailureState.requestedLanguages = []
writeFailureState.generatedLanguages = []
writeFailureState.failedWriteLanguages = []
writeFailureState.writtenLanguages = []
writeFailureState.pendingFill = new Promise((resolve) => {
writeFailureState.releaseFill = resolve
})
break
}
case 'release-fill':
writeFailureState.releaseFill?.()
writeFailureState.releaseFill = null
writeFailureState.pendingFill = null
break
default:
return new Response(null, { status: 400 })
}
return new Response(null, { status: 204 })
}
......@@ -2,6 +2,7 @@
const defaultCacheHandler =
require('next/dist/server/lib/cache-handlers/default.external').default
const writeFailureState = require('./write-failure-state')
const { EventEmitter, once } = require('node:events')
const outerWrites = new EventEmitter()
const pendingOuterWrites = once(outerWrites, 'complete')
......@@ -20,6 +21,17 @@ module.exports = {
...defaultCacheHandler,
async set(cacheKey, pendingEntry) {
const { tags } = await pendingEntry
// Reject the English `set()` calls. The French request must still generate
// and store an entry with its own root parameter value.
if (tags.includes(`write-failure:${writeFailureState.id}`)) {
if (tags.includes('write-failure-language:en')) {
writeFailureState.failedWriteLanguages.push('en')
throw new Error('expected English cache write failure')
}
await defaultCacheHandler.set(cacheKey, pendingEntry)
writeFailureState.writtenLanguages.push('fr')
return
}
const outer = tags.find(
(tag) => tag === 'nested-outer-one' || tag === 'nested-outer-two'
)
......
// @ts-check
/**
* The page, cached function, and handler share these generation controls and
* language records.
*
* @typedef {object} WriteFailureState
* @property {string} id
* @property {string[]} requestedLanguages
* Languages requested by the pages.
* @property {string[]} generatedLanguages
* Languages read by the cached function.
* @property {string[]} failedWriteLanguages
* Languages whose `set()` calls reject.
* @property {string[]} writtenLanguages
* Languages written by the default handler.
* @property {Promise<void> | null} pendingFill
* Keeps `readCachedValue()` pending.
* @property {(() => void) | null} releaseFill
*/
/**
* @type {typeof globalThis & { writeFailureState?: WriteFailureState }}
*/
const reference = globalThis
module.exports = reference.writeFailureState ??= {
id: '',
requestedLanguages: [],
generatedLanguages: [],
failedWriteLanguages: [],
writtenLanguages: [],
pendingFill: null,
releaseFill: null,
}
......@@ -385,6 +385,73 @@ describe('app-root-param-getters - cache dedup with root params', () => {
files: join(__dirname, 'fixtures', 'use-cache-dedup'),
})
beforeAll(async () => {
if (isNextDev) {
// Compile the page before the concurrent test requests. Without an `id`,
// the page skips `readCachedValue()`, so its root parameter dependencies
// remain unknown.
await next.render('/ca/en/write-failure')
}
})
// The control route and cache handler share process-local test state.
// @force-gate !deploy
it('persists a joining root value after the shared producer cache write fails', async () => {
const id = `write-failure-${randomUUID()}`
const control = (action: string) =>
next.fetch(`/write-failure-control?id=${id}`, {
method: 'POST',
body: action,
})
const readState = () =>
next.fetch('/write-failure-control').then(
(response) =>
response.json() as Promise<{
requestedLanguages: string[]
generatedLanguages: string[]
failedWriteLanguages: string[]
writtenLanguages: string[]
}>
)
// Keep the English cached call pending until the French request starts.
// Both calls initially use the key without root parameters because the
// wrapper has not collected the English call's `lang` dependency yet.
await control('hold-fill')
const english = next.render$(`/ca/en/write-failure?id=${id}`)
let french: ReturnType<typeof next.render$> | undefined
try {
await retry(async () => {
expect((await readState()).generatedLanguages).toEqual(['en'])
})
french = next.render$(`/ca/fr/write-failure?id=${id}`)
await retry(async () => {
const state = await readState()
expect(state.requestedLanguages).toEqual(['en', 'fr'])
expect(state.generatedLanguages).toEqual(['en'])
})
// The English `set()` calls reject. The French request must still retry
// the lookup with its own `lang` value and return French data.
await control('release-fill')
const [englishPage, frenchPage] = await Promise.all([english, french])
expect(englishPage('#value').text()).toStartWith('en:')
const frenchValue = frenchPage('#value').text()
expect(frenchValue).toStartWith('fr:')
await retry(async () => {
const state = await readState()
expect(state.failedWriteLanguages).toContain('en')
expect(state.writtenLanguages).toContain('fr')
})
// The next request must reuse the stored French entry. Another execution
// of `readCachedValue()` would generate a new UUID.
expect(
(await next.render$(`/ca/fr/write-failure?id=${id}`))('#value').text()
).toBe(frenchValue)
} finally {
await control('release-fill')
await Promise.allSettled([english, french])
}
})
it('should key a joining outer cache by the root params read by its inner cache', async () => {
const english = await next.render$('/ca/en/nested?prime=1')
expect(english('#first').text()).toBe('en')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment