Unverified Commit 3a0a7caf authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Avoid port collisions in concurrent keep-alive tests (#100537)

The suite bound every mock server to port 44001, so the concurrent runs
in #100525 could fail with EADDRINUSE. Each run now uses an OS-assigned
port and passes its server URL to the fixture.

The suite also awaits server shutdown.
parent 70e94d8e
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { createServer, Server } from 'http' import { createServer, type Server } from 'http'
import type { AddressInfo } from 'net'
// TODO(deploy-test-completion): Re-enable this suite in deploy mode. // TODO(deploy-test-completion): Re-enable this suite in deploy mode.
// Vercel deployment fails to build/deploy this fixture in CI; skip in deploy mode. // Vercel deployment fails to build/deploy this fixture in CI; skip in deploy mode.
...@@ -17,12 +18,20 @@ describe('fetch-keep-alive', () => { ...@@ -17,12 +18,20 @@ describe('fetch-keep-alive', () => {
const { connection } = req.headers const { connection } = req.headers
res.end(JSON.stringify({ connection })) res.end(JSON.stringify({ connection }))
}) })
await new Promise<void>((resolve) => mockServer.listen(44001, resolve)) await new Promise<void>((resolve, reject) => {
mockServer.once('error', reject)
mockServer.listen(0, '127.0.0.1', resolve)
})
next.env.TEST_SERVER_URL = `http://127.0.0.1:${(mockServer.address() as AddressInfo).port}`
await next.start() await next.start()
}) })
afterAll(() => { afterAll(async () => {
mockServer?.close() if (mockServer) {
await new Promise<void>((resolve, reject) => {
mockServer.close((error) => (error ? reject(error) : resolve()))
})
}
}) })
it('should send keep-alive for json API', async () => { it('should send keep-alive for json API', async () => {
......
export default async function handler(_req, res) { export default async function handler(_req, res) {
const fetchRes = await fetch('http://localhost:44001') const fetchRes = await fetch(process.env.TEST_SERVER_URL)
const props = await fetchRes.json() const props = await fetchRes.json()
res.json(props) res.json(props)
} }
...@@ -7,7 +7,7 @@ export async function getStaticProps({ params: { slug } }) { ...@@ -7,7 +7,7 @@ export async function getStaticProps({ params: { slug } }) {
} }
export async function getStaticPaths() { export async function getStaticPaths() {
const res = await fetch('http://localhost:44001') const res = await fetch(process.env.TEST_SERVER_URL)
const obj = await res.json() const obj = await res.json()
if (obj.connection === 'keep-alive') { if (obj.connection === 'keep-alive') {
return { return {
......
...@@ -3,7 +3,7 @@ export default function SSG(props) { ...@@ -3,7 +3,7 @@ export default function SSG(props) {
} }
export async function getStaticProps() { export async function getStaticProps() {
const res = await fetch('http://localhost:44001') const res = await fetch(process.env.TEST_SERVER_URL)
const props = await res.json() const props = await res.json()
return { props } return { props }
} }
...@@ -3,7 +3,7 @@ export default function SSR(props) { ...@@ -3,7 +3,7 @@ export default function SSR(props) {
} }
export async function getServerSideProps() { export async function getServerSideProps() {
const res = await fetch('http://localhost:44001') const res = await fetch(process.env.TEST_SERVER_URL)
const props = await res.json() const props = await res.json()
return { props } return { props }
} }
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