Unverified Commit 542b2b02 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Fix `edge-async-local-storage` deploy test timeout & race condition (#91643)

The test was calling `createNext` inside each `it.each` test body, which
meant deployment setup time counted against the 60-second per-test
timeout introduced in #89929. In deploy mode, `createNext` deploys to
Vercel which can exceed 60 seconds. When the test timed out, the `next`
local variable was never assigned (the `await createNext(...)` hadn't
resolved), so the `afterEach` hook would throw a `TypeError` on
`next.destroy()`. This left the module-level `nextInstance` variable
pointing at the stale, half-initialized deploy instance. When the next
test case started, `createNext` saw the leftover `nextInstance` and
threw `"createNext called without destroying previous instance"`.

The test now uses `nextTestSetup`, which runs `createNext` in
`beforeAll` where it has the longer 120-second `setupTimeout`. Both API
route variants (`/api/single` and `/api/multiple`) are real fixture
files served from a single instance, so deployment only happens once.
parent 9f181bd1
import { createNext } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { NextInstance } from 'e2e-utils'
import { fetchViaHTTP } from 'next-test-utils' import { fetchViaHTTP } from 'next-test-utils'
describe('edge api can use async local storage', () => { describe('edge api can use async local storage', () => {
let next: NextInstance const { next } = nextTestSetup({
files: __dirname,
})
const cases = [ const cases = [
{ {
title: 'a single instance', title: 'a single instance',
code: ` route: '/api/single',
export const config = { runtime: 'edge' } expectResponse: (response: any, id: string) =>
const storage = new AsyncLocalStorage()
export default async function handler(request) {
const id = request.headers.get('req-id')
return storage.run({ id }, async () => {
await getSomeData()
return Response.json(storage.getStore())
})
}
async function getSomeData() {
try {
const response = await fetch('https://example.vercel.sh')
await response.text()
} finally {
return true
}
}
`,
expectResponse: (response, id) =>
expect(response).toMatchObject({ status: 200, json: { id } }), expect(response).toMatchObject({ status: 200, json: { id } }),
}, },
{ {
title: 'multiple instances', title: 'multiple instances',
code: ` route: '/api/multiple',
export const config = { runtime: 'edge' } expectResponse: (response: any, id: string) =>
const topStorage = new AsyncLocalStorage()
export default async function handler(request) {
const id = request.headers.get('req-id')
return topStorage.run({ id }, async () => {
const nested = await getSomeData(id)
return Response.json({ ...nested, ...topStorage.getStore() })
})
}
async function getSomeData(id) {
const nestedStorage = new AsyncLocalStorage()
return nestedStorage.run('nested-' + id, async () => {
try {
const response = await fetch('https://example.vercel.sh')
await response.text()
} finally {
return { nestedId: nestedStorage.getStore() }
}
})
}
`,
expectResponse: (response, id) =>
expect(response).toMatchObject({ expect(response).toMatchObject({
status: 200, status: 200,
json: { id: id, nestedId: `nested-${id}` }, json: { id: id, nestedId: `nested-${id}` },
...@@ -66,40 +24,28 @@ describe('edge api can use async local storage', () => { ...@@ -66,40 +24,28 @@ describe('edge api can use async local storage', () => {
}, },
] ]
afterEach(() => next.destroy())
it.each(cases)( it.each(cases)(
'cans use $title per request', 'can use $title per request',
async ({ code, expectResponse }) => { async ({ route, expectResponse }) => {
next = await createNext({
files: {
'pages/index.js': `
export default function () { return <div>Hello, world!</div> }
`,
'pages/api/async.js': code,
},
})
const ids = Array.from({ length: 100 }, (_, i) => `req-${i}`) const ids = Array.from({ length: 100 }, (_, i) => `req-${i}`)
const responses = await Promise.all( const responses = await Promise.all(
ids.map((id) => ids.map((id) =>
fetchViaHTTP( fetchViaHTTP(next.url, route, {}, { headers: { 'req-id': id } }).then(
next.url, (response) =>
'/api/async', response.headers
{}, .get('content-type')
{ headers: { 'req-id': id } } ?.startsWith('application/json')
).then((response) => ? response.json().then((json) => ({
response.headers.get('content-type')?.startsWith('application/json') status: response.status,
? response.json().then((json) => ({ json,
status: response.status, text: null,
json, }))
text: null, : response.text().then((text) => ({
})) status: response.status,
: response.text().then((text) => ({ json: null,
status: response.status, text,
json: null, }))
text,
}))
) )
) )
) )
......
export const config = { runtime: 'edge' }
// eslint-disable-next-line no-undef
const topStorage = new AsyncLocalStorage()
export default async function handler(request) {
const id = request.headers.get('req-id')
return topStorage.run({ id }, async () => {
const nested = await getSomeData(id)
return Response.json({ ...nested, ...topStorage.getStore() })
})
}
async function getSomeData(id) {
// eslint-disable-next-line no-undef
const nestedStorage = new AsyncLocalStorage()
return nestedStorage.run('nested-' + id, async () => {
try {
const response = await fetch('https://example.vercel.sh')
await response.text()
} finally {
return { nestedId: nestedStorage.getStore() }
}
})
}
export const config = { runtime: 'edge' }
// eslint-disable-next-line no-undef
const storage = new AsyncLocalStorage()
export default async function handler(request) {
const id = request.headers.get('req-id')
return storage.run({ id }, async () => {
await getSomeData()
return Response.json(storage.getStore())
})
}
async function getSomeData() {
try {
const response = await fetch('https://example.vercel.sh')
await response.text()
} finally {
return true
}
}
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