diff options
author | Luca Casonato <hello@lcas.dev> | 2023-09-26 11:12:12 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-26 02:12:12 +0000 |
commit | a879f8c9fa5a2dd6da00b67711c025c300e9b600 (patch) | |
tree | 14810595642a54b8f394bd477a5943945a5605ce | |
parent | 26f431fd14e96cb631042aa3feaf4267cb235f38 (diff) |
test: unflake serve_test/httpServerTcpCancellation (#20672)
Previously could flake on the op sanitizer because the
`await makeTempFile()` promise could leak out of the test. Now we ensure
the request is fully handled before returning.
-rw-r--r-- | cli/tests/unit/serve_test.ts | 47 |
1 files changed, 21 insertions, 26 deletions
diff --git a/cli/tests/unit/serve_test.ts b/cli/tests/unit/serve_test.ts index 830817146..b2b05d00c 100644 --- a/cli/tests/unit/serve_test.ts +++ b/cli/tests/unit/serve_test.ts @@ -2715,29 +2715,28 @@ for (const url of ["text", "file", "stream"]) { signal: ac.signal, onListen: onListen(listeningPromise), handler: async (req: Request) => { - waitForRequest.resolve(); - await waitForAbort; - // Allocate the request body - let _body = req.body; + let respBody = null; if (req.url.includes("/text")) { - return new Response("text"); + respBody = "text"; } else if (req.url.includes("/file")) { - return new Response((await makeTempFile(1024)).readable); + respBody = (await makeTempFile(1024)).readable; } else if (req.url.includes("/stream")) { - return new Response( - new ReadableStream({ - start(controller) { - _body = null; - controller.enqueue(new Uint8Array([1])); - }, - cancel(reason) { - streamCancelled!.resolve(reason); - }, - }), - ); + respBody = new ReadableStream({ + start(controller) { + controller.enqueue(new Uint8Array([1])); + }, + cancel(reason) { + streamCancelled!.resolve(reason); + }, + }); } else { fail(); } + waitForRequest.resolve(); + await waitForAbort; + // Allocate the request body + req.body; + return new Response(respBody); }, }); @@ -2746,24 +2745,20 @@ for (const url of ["text", "file", "stream"]) { // Create a POST request and drop it once the server has received it const conn = await Deno.connect({ port: servePort }); const writer = conn.writable.getWriter(); - writer.write(new TextEncoder().encode(`POST /${url} HTTP/1.0\n\n`)); + await writer.write(new TextEncoder().encode(`POST /${url} HTTP/1.0\n\n`)); await waitForRequest; - writer.close(); + await writer.close(); - // Give it a few milliseconds for the serve machinery to work - await new Promise((r) => setTimeout(r, 10)); waitForAbort.resolve(); - // Give it a few milliseconds for the serve machinery to work - await new Promise((r) => setTimeout(r, 10)); - // Wait for cancellation before we shut the server down if (streamCancelled !== undefined) { await streamCancelled; } - // Since the handler has a chance of creating resources or running async ops, we need to use a - // graceful shutdown here to ensure they have fully drained. + // Since the handler has a chance of creating resources or running async + // ops, we need to use a graceful shutdown here to ensure they have fully + // drained. await server.shutdown(); await server.finished; |