Unverified Commit e8e020ca authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Cover cache handler validation during concurrent reads (#100908)

The same-route test pauses a cache handler read after it checks implicit
expiration. A second request starts after `revalidatePath()` invalidates
that route. The assertions characterize the existing lookup-sharing bug
for the wrapper rewrite in PR #100511.

The prerender test pauses a finite implicit-tag expiration check and
verifies that cache warming keeps long-lived cached content in the
static shell. PR #100652 added coverage for cache reuse after
revalidation and failed writes. These tests extend that coverage to
concurrent handler validation.
parent 64b6a09a
...@@ -16,6 +16,7 @@ export async function GET(request: Request) { ...@@ -16,6 +16,7 @@ export async function GET(request: Request) {
pausedLookupKeys: cacheSharingState.pausedLookupKeys.filter((key) => pausedLookupKeys: cacheSharingState.pausedLookupKeys.filter((key) =>
key.includes(JSON.stringify(id)) key.includes(JSON.stringify(id))
), ),
selectedEntrySoftTags: cacheSharingState.selectedEntrySoftTags,
}) })
} }
...@@ -27,6 +28,20 @@ export async function GET(request: Request) { ...@@ -27,6 +28,20 @@ export async function GET(request: Request) {
export async function POST(request: Request) { export async function POST(request: Request) {
switch (await request.text()) { switch (await request.text()) {
case 'revalidate-route-a':
revalidatePath('/path-revalidation/a')
break
case 'hold-selected-entry':
cacheSharingState.selectedEntrySoftTags = []
cacheSharingState.pendingSelectedEntry = new Promise((resolve) => {
cacheSharingState.releaseSelectedEntry = resolve
})
break
case 'release-selected-entry':
cacheSharingState.releaseSelectedEntry?.()
cacheSharingState.releaseSelectedEntry = null
cacheSharingState.pendingSelectedEntry = null
break
case 'revalidate-route-b': case 'revalidate-route-b':
revalidatePath('/path-revalidation/b') revalidatePath('/path-revalidation/b')
break break
......
...@@ -24,6 +24,9 @@ ...@@ -24,6 +24,9 @@
* @property {Promise<void> | null} pendingRouteLookupResult * @property {Promise<void> | null} pendingRouteLookupResult
* Pauses route B's `get()` before it returns. * Pauses route B's `get()` before it returns.
* @property {(() => void) | null} releaseRouteLookupResult * @property {(() => void) | null} releaseRouteLookupResult
* @property {Promise<void> | null} pendingSelectedEntry
* @property {(() => void) | null} releaseSelectedEntry
* @property {string[][]} selectedEntrySoftTags
*/ */
/** /**
...@@ -43,4 +46,7 @@ module.exports = reference.cacheSharingState ??= { ...@@ -43,4 +46,7 @@ module.exports = reference.cacheSharingState ??= {
releaseFill: null, releaseFill: null,
pendingRouteLookupResult: null, pendingRouteLookupResult: null,
releaseRouteLookupResult: null, releaseRouteLookupResult: null,
pendingSelectedEntry: null,
releaseSelectedEntry: null,
selectedEntrySoftTags: [],
} }
...@@ -41,6 +41,13 @@ const routeAwareCacheHandler = { ...@@ -41,6 +41,13 @@ const routeAwareCacheHandler = {
return undefined return undefined
} }
if (cacheKey.includes('same-route-')) {
cacheSharingState.selectedEntrySoftTags.push([...softTags])
// This entry already passed expiration checks. Later invalidation must
// cause another request to read the handler independently.
await cacheSharingState.pendingSelectedEntry
}
return entry return entry
}, },
......
...@@ -11,6 +11,7 @@ const isoDateRegExp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/ ...@@ -11,6 +11,7 @@ const isoDateRegExp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/
describe('use-cache-custom-handler', () => { describe('use-cache-custom-handler', () => {
const { next, isNextStart } = nextTestSetup({ const { next, isNextStart } = nextTestSetup({
files: __dirname, files: __dirname,
env: { NEXT_PRIVATE_DEBUG_CACHE: '1' },
}) })
let outputIndex: number let outputIndex: number
...@@ -285,6 +286,63 @@ describe('use-cache-custom-handler', () => { ...@@ -285,6 +286,63 @@ describe('use-cache-custom-handler', () => {
} }
}) })
// The configured handler validates implicit tags before the test pauses it.
// Development's front cache can serve the entry without that handler read.
// @force-gate start
it('currently shares a handler lookup between equal routes separated by path revalidation', async () => {
const id = `same-route-${randomUUID()}`
const pathname = `/path-revalidation/a?id=${id}`
const control = (action: string) =>
next.fetch(`/path-revalidation?id=${id}`, {
method: 'POST',
body: action,
})
const readState = () =>
next.fetch(`/path-revalidation?id=${id}`).then(
(response) =>
response.json() as Promise<{
selectedEntrySoftTags: string[][]
startedRoutes: string[]
writes: string[]
}>
)
const initial = (await next.render$(pathname))('#data').text()
await retry(async () => {
expect((await readState()).writes).toHaveLength(1)
})
await control('hold-selected-entry')
const beforeRevalidation = next.render$(pathname)
let afterRevalidation: ReturnType<typeof next.render$> | undefined
try {
await retry(async () => {
expect((await readState()).selectedEntrySoftTags).toHaveLength(1)
})
await control('revalidate-route-a')
const outputIndex = next.cliOutput.length
afterRevalidation = next.render$(pathname)
await retry(() => {
expect(
next.cliOutput
.slice(outputIndex)
.split('\n')
.some(
(line) =>
line.includes('joining pending cross-request invocation') &&
line.includes(id)
)
).toBe(true)
})
await control('release-selected-entry')
const freshPage = await afterRevalidation
expect(freshPage('#data').text()).toBe(initial)
expect(freshPage('#data').text()).not.toBe('')
expect((await beforeRevalidation)('#data').text()).toBe(initial)
} finally {
await control('release-selected-entry')
await Promise.allSettled([beforeRevalidation, afterRevalidation])
}
})
// This test controls when the custom handler's `get()` returns. Development // This test controls when the custom handler's `get()` returns. Development
// can serve an entry from its in-memory handler before that call completes. // can serve an entry from its in-memory handler before that call completes.
// @force-gate start // @force-gate start
......
...@@ -4,7 +4,12 @@ import { data } from './data' ...@@ -4,7 +4,12 @@ import { data } from './data'
export async function readShortLived(key: string) { export async function readShortLived(key: string) {
'use cache' 'use cache'
cacheTag(`short-lived:${key}`) cacheTag(`short-lived:${key}`)
cacheLife({ stale: 30, revalidate: 120, expire: 180 }) // A long-lived entry keeps the expiration test's content in the static shell.
if (process.env.TEST_HOLD_EXPIRATION === '1') {
cacheLife('hours')
} else {
cacheLife({ stale: 30, revalidate: 120, expire: 180 })
}
return `short-lived ${key}` return `short-lived ${key}`
} }
......
...@@ -37,7 +37,13 @@ module.exports = { ...@@ -37,7 +37,13 @@ module.exports = {
} }
}, },
async refreshTags() {}, async refreshTags() {},
async getExpiration() { async getExpiration(tags) {
if (
process.env.TEST_HOLD_EXPIRATION === '1' &&
tags.includes('_N_T_/page')
) {
await notify('expiration:root')
}
return 0 return 0
}, },
async updateTags() {}, async updateTags() {},
......
...@@ -37,6 +37,33 @@ function setupFixture(appDirectory: string) { ...@@ -37,6 +37,33 @@ function setupFixture(appDirectory: string) {
} }
describe('use-cache-dedupe-prerender', () => { describe('use-cache-dedupe-prerender', () => {
describe('asynchronous handler expiration checks', () => {
const fixture = setupFixture('context')
it('keeps cache warming active while implicit-tag expiration is pending', async () => {
const { next, dataService } = fixture
next.env.TEST_HOLD_EXPIRATION = '1'
dataService.hold('expiration:root')
const building = next.build()
try {
await dataService.waitForRequest('expiration:root')
dataService.release('expiration:root')
expect((await building).exitCode).toBe(0)
expect(next.cliOutput).not.toContain(
'Unexpected cache miss after cache warming phase during prerendering'
)
await next.start({ skipBuild: true })
const [shell] = (await next.render('/')).split(
'<!-- PPR_BOUNDARY_SENTINEL -->'
)
expect(load(shell)('#outer').text()).toBe('short-lived shared')
} finally {
dataService.releaseAll()
await building
}
})
})
describe('direct and nested calls during static prerendering', () => { describe('direct and nested calls during static prerendering', () => {
const fixture = setupFixture('context') const fixture = setupFixture('context')
......
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