Unverified Commit 17c65509 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[test] Bail on sending requests to Next.js instance if it's no longer available (#85557)

parent b07fb864
...@@ -82,8 +82,8 @@ export class NextInstance { ...@@ -82,8 +82,8 @@ export class NextInstance {
public testDir: string public testDir: string
public distDir: string public distDir: string
tmpRepoDir: string tmpRepoDir: string
protected isStopping: boolean = false protected isStopping: Error | null = null
protected isDestroyed: boolean = false protected isDestroyed: Error | null = null
protected childProcess?: ChildProcess protected childProcess?: ChildProcess
protected _url: string protected _url: string
protected _parsedUrl: URL protected _parsedUrl: URL
...@@ -515,14 +515,16 @@ export class NextInstance { ...@@ -515,14 +515,16 @@ export class NextInstance {
signal: 'SIGINT' | 'SIGTERM' | 'SIGKILL' = 'SIGKILL' signal: 'SIGINT' | 'SIGTERM' | 'SIGKILL' = 'SIGKILL'
): Promise<void> { ): Promise<void> {
if (this.childProcess) { if (this.childProcess) {
if (this.isStopping) { if (this.isStopping !== null) {
// warn for debugging, but don't prevent sending two signals in succession // warn for debugging, but don't prevent sending two signals in succession
// (e.g. SIGINT and then SIGKILL) // (e.g. SIGINT and then SIGKILL)
require('console').error( require('console').error(
`Next server is already being stopped (received signal: ${signal})` `Next server is already being stopped (received signal: ${signal}): `,
this.isStopping
) )
} }
this.isStopping = true this.isStopping = Error()
Error.captureStackTrace(this.isStopping, this.stop)
const closePromise = once(this.childProcess, 'close') const closePromise = once(this.childProcess, 'close')
await new Promise<void>((resolve) => { await new Promise<void>((resolve) => {
treeKill(this.childProcess!.pid!, signal, (err) => { treeKill(this.childProcess!.pid!, signal, (err) => {
...@@ -535,7 +537,7 @@ export class NextInstance { ...@@ -535,7 +537,7 @@ export class NextInstance {
this.childProcess.kill(signal) this.childProcess.kill(signal)
await closePromise await closePromise
this.childProcess = undefined this.childProcess = undefined
this.isStopping = false this.isStopping = null
require('console').log(`Stopped next server`) require('console').log(`Stopped next server`)
} }
} }
...@@ -545,9 +547,13 @@ export class NextInstance { ...@@ -545,9 +547,13 @@ export class NextInstance {
require('console').time('destroyed next instance') require('console').time('destroyed next instance')
if (this.isDestroyed) { if (this.isDestroyed) {
throw new Error(`next instance already destroyed`) throw new Error(`next instance already destroyed`, {
cause: this.isDestroyed,
})
} }
this.isDestroyed = true this.isDestroyed = Error()
Error.captureStackTrace(this.isDestroyed, this.destroy)
this.emit('destroy', []) this.emit('destroy', [])
await this.stop().catch(console.error) await this.stop().catch(console.error)
...@@ -602,6 +608,22 @@ export class NextInstance { ...@@ -602,6 +608,22 @@ export class NextInstance {
return '' return ''
} }
protected throwIfUnavailable(): void | never {
if (this.isStopping !== null) {
throw new Error('Next.js is no longer available.', {
cause: this.isStopping,
})
}
if (this.isDestroyed !== null) {
throw new Error('Next.js is no longer available.', {
cause: this.isDestroyed,
})
}
if (this.childProcess === undefined) {
throw new Error('No child process available')
}
}
// TODO: block these in deploy mode // TODO: block these in deploy mode
public async hasFile(filename: string) { public async hasFile(filename: string) {
return existsSync(path.join(this.testDir, filename)) return existsSync(path.join(this.testDir, filename))
...@@ -733,6 +755,12 @@ export class NextInstance { ...@@ -733,6 +755,12 @@ export class NextInstance {
public async browser( public async browser(
...args: Parameters<OmitFirstArgument<typeof webdriver>> ...args: Parameters<OmitFirstArgument<typeof webdriver>>
): Promise<Playwright> { ): Promise<Playwright> {
try {
this.throwIfUnavailable()
} catch (error) {
Error.captureStackTrace(error, this.browser)
throw error
}
return webdriver(this.url, ...args) return webdriver(this.url, ...args)
} }
...@@ -743,6 +771,12 @@ export class NextInstance { ...@@ -743,6 +771,12 @@ export class NextInstance {
public async browserWithResponse( public async browserWithResponse(
...args: Parameters<OmitFirstArgument<typeof webdriver>> ...args: Parameters<OmitFirstArgument<typeof webdriver>>
): Promise<{ browser: Playwright; response: Response }> { ): Promise<{ browser: Playwright; response: Response }> {
try {
this.throwIfUnavailable()
} catch (error) {
Error.captureStackTrace(error, this.browserWithResponse)
throw error
}
const [url, options = {}] = args const [url, options = {}] = args
let resolveResponse: (response: Response) => void let resolveResponse: (response: Response) => void
...@@ -785,6 +819,12 @@ export class NextInstance { ...@@ -785,6 +819,12 @@ export class NextInstance {
public async render$( public async render$(
...args: Parameters<OmitFirstArgument<typeof renderViaHTTP>> ...args: Parameters<OmitFirstArgument<typeof renderViaHTTP>>
): Promise<ReturnType<typeof cheerio.load>> { ): Promise<ReturnType<typeof cheerio.load>> {
try {
this.throwIfUnavailable()
} catch (error) {
Error.captureStackTrace(error, this.render$)
throw error
}
const html = await renderViaHTTP(this.url, ...args) const html = await renderViaHTTP(this.url, ...args)
return cheerio.load(html) return cheerio.load(html)
} }
...@@ -795,6 +835,12 @@ export class NextInstance { ...@@ -795,6 +835,12 @@ export class NextInstance {
public async render( public async render(
...args: Parameters<OmitFirstArgument<typeof renderViaHTTP>> ...args: Parameters<OmitFirstArgument<typeof renderViaHTTP>>
) { ) {
try {
this.throwIfUnavailable()
} catch (error) {
Error.captureStackTrace(error, this.render)
throw error
}
return renderViaHTTP(this.url, ...args) return renderViaHTTP(this.url, ...args)
} }
...@@ -809,6 +855,12 @@ export class NextInstance { ...@@ -809,6 +855,12 @@ export class NextInstance {
pathname: string, pathname: string,
opts?: import('node-fetch').RequestInit opts?: import('node-fetch').RequestInit
) { ) {
try {
this.throwIfUnavailable()
} catch (error) {
Error.captureStackTrace(error, this.fetch)
throw error
}
return fetchViaHTTP(this.url, pathname, null, opts) return fetchViaHTTP(this.url, pathname, null, opts)
} }
......
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