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

[test] Fix flaky file-system-cache-clock tests (#100946)

The tests added in PR #100357 can race background fetch-cache writes
after the HTTP response completes.

The tests now wait for the expected value and `lastModified` timestamp
on disk before checking cache reuse, stopping the server, or inspecting
the rewritten entry. The first request after tag revalidation must still
return fresh data without retries.

[Flakiness
metrics](https://app.datadoghq.com/ci/ci-cd/explorer/executions?query=test_level%3Atest%20%40git.repository.id%3A%22github.com%2Fvercel%2Fnext.js%22%20%40test.name%3A%22file-system-cache-clock%20with%20the%20default%20memory%20cache%20reads%20a%20fetch%20cache%20entry%20written%20without%20a%20stored%20timestamp%22%20%40test.type%3A%22turbopack%22%20%40test.status%3A%22fail%22&agg_m=count&agg_m_source=base&agg_t=count&ci_cd_explorer_tab=tests&fromUser=false&start=1792744453308&end=1793349253308&paused=false)
parent d726535d
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import { load } from 'cheerio' import { load } from 'cheerio'
import { createServer } from 'node:http' import { createServer } from 'node:http'
import type { AddressInfo } from 'node:net' import type { AddressInfo } from 'node:net'
...@@ -67,6 +68,31 @@ describe('file-system-cache-clock', () => { ...@@ -67,6 +68,31 @@ describe('file-system-cache-clock', () => {
return (await request(pathname)).data return (await request(pathname)).data
} }
// Wait for the expected fetch value to reach disk. Dynamic fetch-cache
// writes can finish after the HTTP response.
async function waitForFetchEntry(value: string) {
return retry(async () => {
const fetchCacheDir = join(next.testDir, '.next/cache/fetch-cache')
const entries = await Promise.all(
(await fs.readdir(fetchCacheDir)).map(async (file) => {
const filePath = join(fetchCacheDir, file)
return {
filePath,
entry: JSON.parse(await fs.readFile(filePath, 'utf8')),
}
})
)
const matches = entries.filter(({ entry }) => {
const data = entry.data
return data && Buffer.from(data.body, 'base64').toString() === value
})
expect(matches).toHaveLength(1)
const [match] = matches
expect(match.entry.lastModified).toEqual(expect.any(Number))
return match
})
}
// The response cache entry of the ISR page, as written by the default // The response cache entry of the ISR page, as written by the default
// file-system cache handler. // file-system cache handler.
async function readPageEntry(pathname: string) { async function readPageEntry(pathname: string) {
...@@ -127,36 +153,25 @@ describe('file-system-cache-clock', () => { ...@@ -127,36 +153,25 @@ describe('file-system-cache-clock', () => {
it('serves fresh data for a dynamic page right after revalidateTag', async () => { it('serves fresh data for a dynamic page right after revalidateTag', async () => {
const cached = await getData('/dynamic') const cached = await getData('/dynamic')
expect(cached).toStartWith('/dynamic ') expect(cached).toStartWith('/dynamic ')
await waitForFetchEntry(cached)
expect(await getData('/dynamic')).toBe(cached) expect(await getData('/dynamic')).toBe(cached)
await revalidate() await revalidate()
const fresh = await getData('/dynamic') const fresh = await getData('/dynamic')
expect(fresh).toStartWith('/dynamic ') expect(fresh).toStartWith('/dynamic ')
expect(fresh).not.toBe(cached) expect(fresh).not.toBe(cached)
await waitForFetchEntry(fresh)
}) })
it('reads a fetch cache entry written without a stored timestamp', async () => { it('reads a fetch cache entry written without a stored timestamp', async () => {
const cached = await getData('/dynamic') const cached = await getData('/dynamic')
expect(cached).toStartWith('/dynamic ') expect(cached).toStartWith('/dynamic ')
const { filePath, entry } = await waitForFetchEntry(cached)
await next.stop() await next.stop()
// Find the entry by its cached value, then rewrite it in the format // Rewrite the entry in the format used before timestamps were stored,
// written before timestamps were stored, with an earlier mtime. // with an earlier mtime.
const fetchCacheDir = join(next.testDir, '.next/cache/fetch-cache') delete entry.lastModified
const entries = await Promise.all(
(await fs.readdir(fetchCacheDir)).map(async (file) => {
const filePath = join(fetchCacheDir, file)
return { filePath, content: await fs.readFile(filePath, 'utf8') }
})
)
const matches = entries.filter(({ content }) => {
const data = JSON.parse(content).data
return data && Buffer.from(data.body, 'base64').toString() === cached
})
expect(matches).toHaveLength(1)
const [{ filePath, content }] = matches
const { lastModified, ...entry } = JSON.parse(content)
expect(typeof lastModified).toBe('number')
await fs.writeFile(filePath, JSON.stringify(entry)) await fs.writeFile(filePath, JSON.stringify(entry))
const modified = new Date(Date.now() - 60_000) const modified = new Date(Date.now() - 60_000)
await fs.utimes(filePath, modified, modified) await fs.utimes(filePath, modified, modified)
...@@ -165,11 +180,10 @@ describe('file-system-cache-clock', () => { ...@@ -165,11 +180,10 @@ describe('file-system-cache-clock', () => {
expect(await getData('/dynamic')).toBe(cached) expect(await getData('/dynamic')).toBe(cached)
await revalidate() await revalidate()
expect(await getData('/dynamic')).not.toBe(cached) const fresh = await getData('/dynamic')
expect(fresh).not.toBe(cached)
// The fresh value was written in the new format. // The fresh value was written in the new format.
expect( await waitForFetchEntry(fresh)
JSON.parse(await fs.readFile(filePath, 'utf8')).lastModified
).toEqual(expect.any(Number))
}) })
}) })
}) })
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