rsnext/test/e2e/cancel-request/pages/api/edge-api.ts
Justin Ridgewell 3906374740
Try to fix flakey socket hang up failures in stream cancel tests (#53318)
### What?

I think the flakiness from the new stream cancel tests is due to a uncaught error thrown from the priming. If it is during the priming, then we need to also explicitly check if we restart the test and reset the `streamable`.

### Why?

Flakey tests suck.

### How?

- Adds a `on('error', reject)` to catch the socket error and associate it with the test
- Explicitly checks for the `?write=` param to see if we're priming or getting results
2023-07-28 22:59:24 +00:00

29 lines
856 B
TypeScript

import { Streamable } from '../../streamable'
export const config = {
runtime: 'edge',
}
let streamable: ReturnType<typeof Streamable> | undefined
export default async function handler(req: Request): Promise<Response> {
// Consume the entire request body.
// This is so we don't confuse the request close with the connection close.
await req.text()
const write = new URL(req.url!, 'http://localhost/').searchParams.get('write')
if (write) {
const s = (streamable = Streamable(+write!))
req.signal.onabort = () => {
s.abort()
}
return new Response(s.stream)
}
// The 2nd request should render the stats. We don't use a query param
// because edge rendering will create a different bundle for that.
const old = streamable!
streamable = undefined
const i = await old.finished
return new Response(`${i}`)
}