summaryrefslogtreecommitdiff
path: root/cli/tests/unit/net_test.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-12-28 10:29:48 +0100
committerGitHub <noreply@github.com>2022-12-28 10:29:48 +0100
commitbece1ce05704d9a81efd2e04a22ee721795903e7 (patch)
tree1da9f477eef90e3837a2366bec0bdd9f8a79b3c0 /cli/tests/unit/net_test.ts
parent7ce2b58bcf412924464578f3469c210b34894c8b (diff)
feat(unstable): Add Deno.Conn.ref()/unref() (#17170)
This commit adds "Deno.Conn.ref()" and "Deno.Conn.unref()" methods. These methods can be used to make connection block or not block the event loop from finishing. Refing/unrefing only influences "read" operations - ie. scheduling writes to a connection _do_ keep event loop alive. Required for https://github.com/denoland/deno/issues/16710
Diffstat (limited to 'cli/tests/unit/net_test.ts')
-rw-r--r--cli/tests/unit/net_test.ts49
1 files changed, 49 insertions, 0 deletions
diff --git a/cli/tests/unit/net_test.ts b/cli/tests/unit/net_test.ts
index b4a21578e..930e4f052 100644
--- a/cli/tests/unit/net_test.ts
+++ b/cli/tests/unit/net_test.ts
@@ -906,6 +906,55 @@ Deno.test({
listener.close();
});
+Deno.test(
+ { permissions: { net: true, read: true, run: true } },
+ async function netConnUnref() {
+ const listener = Deno.listen({ port: 3500 });
+ const intervalId = setInterval(() => {}); // This keeps event loop alive.
+
+ const program = execCode(`
+ async function main() {
+ const conn = await Deno.connect({ port: 3500 });
+ conn.unref();
+ await conn.read(new Uint8Array(10)); // The program exits here
+ throw new Error(); // The program doesn't reach here
+ }
+ main();
+ `);
+ const conn = await listener.accept();
+ const [statusCode, _output] = await program;
+ conn.close();
+ listener.close();
+ clearInterval(intervalId);
+ assertEquals(statusCode, 0);
+ },
+);
+
+Deno.test(
+ { permissions: { net: true, read: true, run: true } },
+ async function netConnUnrefReadable() {
+ const listener = Deno.listen({ port: 3500 });
+ const intervalId = setInterval(() => {}); // This keeps event loop alive.
+
+ const program = execCode(`
+ async function main() {
+ const conn = await Deno.connect({ port: 3500 });
+ conn.unref();
+ const reader = conn.readable.getReader();
+ await reader.read(); // The program exits here
+ throw new Error(); // The program doesn't reach here
+ }
+ main();
+ `);
+ const conn = await listener.accept();
+ const [statusCode, _output] = await program;
+ conn.close();
+ listener.close();
+ clearInterval(intervalId);
+ assertEquals(statusCode, 0);
+ },
+);
+
Deno.test({ permissions: { net: true } }, async function netTcpReuseAddr() {
const listener1 = Deno.listen({
hostname: "127.0.0.1",