Unverified Commit 4ebb2b7a authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Cover cache reuse after revalidation and failed writes (#100652)

The tests verify that an embedded runtime prefetch reuses a valid
handler entry after tag revalidation invalidates the old Resume Data
Cache entry. They also verify that a server action returns a shared
value and completes its re-render when the producer's handler write
fails.

These tests cover existing behavior that the `use cache` wrapper rewrite
in #100511 must preserve.
parent 79107ebd
...@@ -192,3 +192,77 @@ describe('app-custom-cache-handler-errors - lookup recovery', () => { ...@@ -192,3 +192,77 @@ describe('app-custom-cache-handler-errors - lookup recovery', () => {
} }
}) })
}) })
// The handler and control route share process-local state. Development's
// in-memory front handler does not exercise the configured write failure.
// @force-gate start
describe('app-custom-cache-handler-errors - shared write failure', () => {
const { next } = nextTestSetup({
files: __dirname,
env: { CACHE_HANDLER_THROW_ON: 'shared-write-failure' },
})
it("completes a joining server action and its re-render when the producer's cache write fails", async () => {
const id = randomUUID()
const control = (action: string) =>
next.fetch(`/shared-write-control?id=${id}`, {
method: 'POST',
body: action,
})
const readState = () =>
next.fetch('/shared-write-control').then(
(response) =>
response.json() as Promise<{
generatedValues: string[]
writeStarted: boolean
writeFailed: boolean
joinerStarted: boolean
joinerReadCompleted: boolean
}>
)
const browser = await next.browser(`/shared-write-failure?id=${id}`)
const initialRender = await browser
.elementById('shared-write-render')
.text()
// The held write keeps the producer's fill available for the action to join.
await control('hold-write')
const producer = next.render$(`/shared-write-failure/producer?id=${id}`)
try {
await retry(async () => {
expect((await readState()).writeStarted).toBe(true)
})
await browser.elementById('join-fill').click()
await retry(async () => {
expect((await readState()).joinerStarted).toBe(true)
})
await control('release-write')
const producerPage = await producer
const value = producerPage('#shared-write-producer').text()
await retry(async () => {
expect(
await browser.elementById('shared-write-result').text()
).not.toBe('')
})
expect(await browser.elementById('shared-write-result').text()).toBe(
value
)
// The shared read can succeed before the action fails while awaiting
// writes. The action must also complete the re-render it requests with
// refresh().
expect(await browser.elementById('shared-write-render').text()).not.toBe(
initialRender
)
const state = await readState()
expect(state.generatedValues).toEqual([value])
expect(state.writeFailed).toBe(true)
expect(state.joinerReadCompleted).toBe(true)
} finally {
await control('release-write')
await Promise.allSettled([producer])
}
})
})
import { connection } from 'next/server'
import sharedWriteFailureState from '../../shared-write-failure-state'
export async function GET() {
await connection()
return Response.json({
generatedValues: sharedWriteFailureState.generatedValues,
writeStarted: sharedWriteFailureState.writeStarted,
writeFailed: sharedWriteFailureState.writeFailed,
joinerStarted: sharedWriteFailureState.joinerStarted,
joinerReadCompleted: sharedWriteFailureState.joinerReadCompleted,
})
}
export async function POST(request: Request) {
switch (await request.text()) {
case 'hold-write': {
const id = new URL(request.url).searchParams.get('id')
if (id === null) {
return new Response(null, { status: 400 })
}
sharedWriteFailureState.id = id
sharedWriteFailureState.generatedValues = []
sharedWriteFailureState.writeStarted = false
sharedWriteFailureState.writeFailed = false
sharedWriteFailureState.joinerStarted = false
sharedWriteFailureState.joinerReadCompleted = false
sharedWriteFailureState.pendingWrite = new Promise((resolve) => {
sharedWriteFailureState.releaseWrite = resolve
})
break
}
case 'release-write':
sharedWriteFailureState.releaseWrite?.()
sharedWriteFailureState.releaseWrite = null
sharedWriteFailureState.pendingWrite = null
break
default:
return new Response(null, { status: 400 })
}
return new Response(null, { status: 204 })
}
'use server'
import { refresh } from 'next/cache'
import { readSharedValue } from './data'
import sharedWriteFailureState from '../../shared-write-failure-state'
export async function joinFill(id: string) {
const pendingValue = readSharedValue(id)
sharedWriteFailureState.joinerStarted = true
const value = await pendingValue
sharedWriteFailureState.joinerReadCompleted = true
refresh()
return value
}
import { randomUUID } from 'node:crypto'
import { cacheLife, cacheTag } from 'next/cache'
import sharedWriteFailureState from '../../shared-write-failure-state'
export async function readSharedValue(id: string) {
'use cache'
cacheLife('hours')
cacheTag(`shared-write-failure:${id}`)
const value = randomUUID()
sharedWriteFailureState.generatedValues.push(value)
return value
}
'use client'
import { useState } from 'react'
export function JoinButton({
joinFillAction,
}: {
joinFillAction: () => Promise<string>
}) {
const [result, setResult] = useState('')
return (
<>
<button
id="join-fill"
onClick={async () => {
try {
setResult(await joinFillAction())
} catch {
setResult('action failed')
}
}}
>
Join fill
</button>
<p id="shared-write-result">{result}</p>
</>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { joinFill } from './actions'
import { JoinButton } from './join-button'
async function Content({
searchParams,
}: {
searchParams: Promise<{ id?: string | string[] }>
}) {
await connection()
const { id } = await searchParams
if (typeof id !== 'string') {
throw new Error('Expected a single id search parameter')
}
return (
<>
<p id="shared-write-render">{Math.random()}</p>
<JoinButton joinFillAction={joinFill.bind(null, id)} />
</>
)
}
export default function Page({
searchParams,
}: {
searchParams: Promise<{ id?: string | string[] }>
}) {
return (
<Suspense fallback={<p>Loading action</p>}>
<Content searchParams={searchParams} />
</Suspense>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { readSharedValue } from '../data'
async function Content({
searchParams,
}: {
searchParams: Promise<{ id?: string | string[] }>
}) {
await connection()
const { id } = await searchParams
if (typeof id !== 'string') {
throw new Error('Expected a single id search parameter')
}
return <p id="shared-write-producer">{await readSharedValue(id)}</p>
}
export default function Page({
searchParams,
}: {
searchParams: Promise<{ id?: string | string[] }>
}) {
return (
<Suspense fallback={<p>Loading producer</p>}>
<Content searchParams={searchParams} />
</Suspense>
)
}
...@@ -7,7 +7,9 @@ const nextConfig = { ...@@ -7,7 +7,9 @@ const nextConfig = {
default: default:
process.env.CACHE_HANDLER_THROW_ON === 'retry-get' process.env.CACHE_HANDLER_THROW_ON === 'retry-get'
? require.resolve('./retrying-cache-handler.js') ? require.resolve('./retrying-cache-handler.js')
: require.resolve('./throwing-cache-handler.js'), : process.env.CACHE_HANDLER_THROW_ON === 'shared-write-failure'
? require.resolve('./shared-write-failure-handler.js')
: require.resolve('./throwing-cache-handler.js'),
}, },
} }
......
// @ts-check
const cacheHandler = require('./throwing-cache-handler')
const sharedWriteFailureState = require('./shared-write-failure-state')
/**
* @type {import('next/cache').CacheHandler}
*/
module.exports = {
...cacheHandler,
async set(cacheKey, pendingEntry) {
const entry = await pendingEntry
if (
!entry.tags.includes(`shared-write-failure:${sharedWriteFailureState.id}`)
) {
return cacheHandler.set(cacheKey, pendingEntry)
}
sharedWriteFailureState.writeStarted = true
await sharedWriteFailureState.pendingWrite
sharedWriteFailureState.writeFailed = true
throw new Error('expected shared cache write failure')
},
}
// @ts-check
/**
* @typedef {object} SharedWriteFailureState
* @property {string} id
* @property {string[]} generatedValues
* @property {boolean} writeStarted
* @property {boolean} writeFailed
* @property {boolean} joinerStarted
* @property {boolean} joinerReadCompleted
* @property {Promise<void> | null} pendingWrite
* @property {(() => void) | null} releaseWrite
*/
/**
* @type {typeof globalThis & { sharedWriteFailureState?: SharedWriteFailureState }}
*/
const reference = globalThis
module.exports = reference.sharedWriteFailureState ??= {
id: '',
generatedValues: [],
writeStarted: false,
writeFailed: false,
joinerStarted: false,
joinerReadCompleted: false,
pendingWrite: null,
releaseWrite: null,
}
'use server'
import { updateTag } from 'next/cache'
import { redirect } from 'next/navigation'
export async function revalidateData(tag: string) {
updateTag(tag)
// The redirect renders the page with an embedded runtime prefetch. An
// ordinary action re-render does not include that prefetch.
redirect('/handler-handoff')
}
import { randomUUID } from 'node:crypto'
import { cacheLife, cacheTag } from 'next/cache'
export async function readData(key: string) {
'use cache: handoff'
// This entry survives RDC serialization. Its stale time excludes its value
// and tag from the static shell, so invalidation can leave the shell cached
// while this RDC entry becomes obsolete.
cacheLife({ stale: 0, revalidate: 120, expire: 3600 })
const value = `handler-handoff-${randomUUID()}`
cacheTag(value)
return value
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { revalidateData } from './actions'
import { readData } from './data'
async function DynamicContent() {
await connection()
return <p>Dynamic content</p>
}
async function CachedContent() {
const value = await readData('rdc-handler-handoff')
return (
<>
<p id="handoff-value">{value}</p>
<form action={revalidateData.bind(null, value)}>
<button id="revalidate-handoff" type="submit">
Revalidate cached value
</button>
</form>
</>
)
}
export default function Page() {
return (
<>
<Suspense fallback={<p>Loading cached content</p>}>
<CachedContent />
</Suspense>
<Suspense fallback={<p>Loading dynamic content</p>}>
<DynamicContent />
</Suspense>
</>
)
}
import { connection } from 'next/server'
import { readData } from '../data'
export async function GET() {
await connection()
return new Response(await readData('rdc-handler-handoff'))
}
// @ts-check
/**
* @typedef {import('next/cache').CacheEntry} CacheEntry
* @typedef {Omit<CacheEntry, 'value'> & { chunks: Uint8Array[] }} StoredEntry
*/
/**
* @type {Map<string, StoredEntry>}
*/
const entries = new Map()
/**
* @type {import('next/cache').CacheHandler}
*/
module.exports = {
async get(cacheKey) {
const storedEntry = entries.get(cacheKey)
if (storedEntry === undefined) {
return undefined
}
console.log('RDC handoff handler hit', cacheKey)
const { chunks, ...metadata } = storedEntry
return {
...metadata,
value: new ReadableStream({
start(controller) {
for (const chunk of chunks) {
controller.enqueue(chunk)
}
controller.close()
},
}),
}
},
async set(cacheKey, pendingEntry) {
const { value, ...metadata } = await pendingEntry
/**
* @type {Uint8Array[]}
*/
const chunks = []
const reader = value.getReader()
for (let result; !(result = await reader.read()).done; ) {
chunks.push(result.value)
}
entries.set(cacheKey, { ...metadata, chunks })
},
async refreshTags() {},
async getExpiration() {
return 0
},
async updateTags() {},
}
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
*/ */
const nextConfig = { const nextConfig = {
cacheComponents: true, cacheComponents: true,
cacheHandlers: {
handoff: require.resolve('./handoff-handler.js'),
},
} }
module.exports = nextConfig module.exports = nextConfig
...@@ -2,6 +2,7 @@ import { NextInstance, nextTestSetup } from 'e2e-utils' ...@@ -2,6 +2,7 @@ import { NextInstance, nextTestSetup } from 'e2e-utils'
import { assertNoConsoleErrors, gate, retry } from 'next-test-utils' import { assertNoConsoleErrors, gate, retry } from 'next-test-utils'
import { computeCacheBustingSearchParam } from 'next/dist/shared/lib/router/utils/cache-busting-search-param' import { computeCacheBustingSearchParam } from 'next/dist/shared/lib/router/utils/cache-busting-search-param'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import type { Page } from 'playwright'
// RDC and resume renders do not function in the same way in dev. // RDC and resume renders do not function in the same way in dev.
// This test suite asserts on production behavior. // This test suite asserts on production behavior.
...@@ -9,6 +10,7 @@ import cheerio from 'cheerio' ...@@ -9,6 +10,7 @@ import cheerio from 'cheerio'
describe('resume-data-cache', () => { describe('resume-data-cache', () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
env: { NEXT_PRIVATE_DEBUG_CACHE: '1' },
}) })
describe('should have consistent data between static and dynamic renders', () => { describe('should have consistent data between static and dynamic renders', () => {
...@@ -287,6 +289,62 @@ describe('resume-data-cache', () => { ...@@ -287,6 +289,62 @@ describe('resume-data-cache', () => {
}) })
}) })
// The discard-count assertion requires local server logs.
// @force-gate start && partialPrefetchingGlobal
it('replaces an invalidated RDC entry with a handler hit for the embedded runtime prefetch', async () => {
const initialPage = await next.render$('/handler-handoff')
const initialValue = initialPage('#handoff-value').text()
// The warm route populates the handler without changing the prerendered RDC.
const warmResponse = await next.fetch('/handler-handoff/warm')
expect(warmResponse.status).toBe(200)
const warmedValue = await warmResponse.text()
expect(warmedValue).not.toBe(initialValue)
let page: Page
const browser = await next.browser('/handler-handoff', {
beforePageLoad(browserPage) {
page = browserPage
},
})
expect(await browser.elementById('handoff-value').text()).toBe(initialValue)
const outputIndex = next.cliOutput.length
// Each revision has its own tag. The action invalidates the prerendered RDC
// entry while the cache handler's replacement remains valid.
const pendingActionResponse = page.waitForResponse(
(response) =>
new URL(response.url()).pathname === '/handler-handoff' &&
response.request().method() === 'POST'
)
await browser.elementById('revalidate-handoff').click()
const actionResponse = await pendingActionResponse
expect(actionResponse.status()).toBe(200)
// Reading the complete response waits for the embedded runtime prefetch.
await actionResponse.text()
await retry(async () => {
expect(await browser.elementById('handoff-value').text()).toBe(
warmedValue
)
})
const cacheKey = JSON.stringify('rdc-handler-handoff')
const cacheEntryOutput = next.cliOutput
.slice(outputIndex)
.split('\n')
.filter((line) => line.includes(cacheKey))
.join('\n')
expect(cacheEntryOutput).toIncludeRepeated('RDC handoff handler hit', 1)
// The retained handler lookup can hide a missing RDC update. A second RDC
// discard detects that failure even when the rendered value is correct.
expect(cacheEntryOutput).toIncludeRepeated(
'discarding RDC entry due to recently revalidated tags',
1
)
})
it('should see fresh data after updateTag in server action with fetch cache', async () => { it('should see fresh data after updateTag in server action with fetch cache', async () => {
// This test verifies that when a server action calls updateTag(), // This test verifies that when a server action calls updateTag(),
// the subsequent re-render sees fresh data instead of stale RDC data. // the subsequent re-render sees fresh data instead of stale RDC data.
......
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