summaryrefslogtreecommitdiff
path: root/cli/tests/unit/http_test.ts
diff options
context:
space:
mode:
authorBert Belder <bertbelder@gmail.com>2021-10-09 22:37:19 +0200
committerBert Belder <bertbelder@gmail.com>2021-10-17 19:50:42 +0200
commitff932b411d63269dbd4d30ea6bd0aa5160fd8aff (patch)
tree5dad617aea815c4145262860d6e3b5115224ab92 /cli/tests/unit/http_test.ts
parentff95fc167d7124f3c7f2c6951070e2c40701cf32 (diff)
fix(core): poll async ops eagerly (#12385)
Currently all async ops are polled lazily, which means that op initialization code is postponed until control is yielded to the event loop. This has some weird consequences, e.g. ```js let listener = Deno.listen(...); let conn_promise = listener.accept(); listener.close(); // `BadResource` is thrown. A reasonable error would be `Interrupted`. let conn = await conn_promise; ``` JavaScript promises are expected to be eagerly evaluated. This patch makes ops actually do that.
Diffstat (limited to 'cli/tests/unit/http_test.ts')
-rw-r--r--cli/tests/unit/http_test.ts47
1 files changed, 15 insertions, 32 deletions
diff --git a/cli/tests/unit/http_test.ts b/cli/tests/unit/http_test.ts
index 0f23a2bb5..080b94a1d 100644
--- a/cli/tests/unit/http_test.ts
+++ b/cli/tests/unit/http_test.ts
@@ -892,15 +892,13 @@ unitTest(
respondPromise,
]);
+ httpConn.close();
listener.close();
assert(errors.length >= 1);
for (const error of errors) {
assertEquals(error.name, "Http");
- assertEquals(
- error.message,
- "connection closed before message completed",
- );
+ assert(error.message.includes("connection"));
}
},
);
@@ -975,44 +973,29 @@ unitTest(
unitTest(
{ permissions: { net: true } },
async function droppedConnSenderNoPanic() {
- async function server(listener: Deno.Listener) {
+ async function server() {
+ const listener = Deno.listen({ port: 8000 });
const conn = await listener.accept();
const http = Deno.serveHttp(conn);
-
- for (;;) {
- const req = await http.nextRequest();
- if (req == null) break;
-
- nextloop()
- .then(() => {
- http.close();
- return req.respondWith(new Response("boom"));
- })
- .catch(() => {});
- }
-
+ const evt = await http.nextRequest();
+ http.close();
try {
- http.close();
+ await evt!.respondWith(new Response("boom"));
} catch {
- "nop";
+ // Ignore error.
}
-
listener.close();
}
async function client() {
- const resp = await fetch("http://127.0.0.1:8000/");
- await resp.body?.cancel();
- }
-
- function nextloop() {
- return new Promise((resolve) => setTimeout(resolve, 0));
+ try {
+ const resp = await fetch("http://127.0.0.1:8000/");
+ await resp.body?.cancel();
+ } catch {
+ // Ignore error
+ }
}
- async function main() {
- const listener = Deno.listen({ port: 8000 });
- await Promise.all([server(listener), client()]);
- }
- await main();
+ await Promise.all([server(), client()]);
},
);