summaryrefslogtreecommitdiff
path: root/cli/tests/unit
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tests/unit')
-rw-r--r--cli/tests/unit/net_test.ts77
-rw-r--r--cli/tests/unit/test_util.ts2
2 files changed, 78 insertions, 1 deletions
diff --git a/cli/tests/unit/net_test.ts b/cli/tests/unit/net_test.ts
index 6769f6301..c7a081bf4 100644
--- a/cli/tests/unit/net_test.ts
+++ b/cli/tests/unit/net_test.ts
@@ -7,6 +7,7 @@ import {
assertThrows,
deferred,
delay,
+ execCode,
} from "./test_util.ts";
let isCI: boolean;
@@ -807,3 +808,79 @@ Deno.test(
assertEquals(res, "hello world!");
},
);
+
+Deno.test(
+ { permissions: { read: true, run: true } },
+ async function netListenUnref() {
+ const [statusCode, _output] = await execCode(`
+ async function main() {
+ const listener = Deno.listen({ port: 3500 });
+ listener.unref();
+ await listener.accept(); // This doesn't block the program from exiting
+ }
+ main();
+ `);
+ assertEquals(statusCode, 0);
+ },
+);
+
+Deno.test(
+ { permissions: { read: true, run: true } },
+ async function netListenUnref() {
+ const [statusCode, _output] = await execCode(`
+ async function main() {
+ const listener = Deno.listen({ port: 3500 });
+ await listener.accept();
+ listener.unref();
+ await listener.accept(); // The program exits here
+ throw new Error(); // The program doesn't reach here
+ }
+ main();
+ const conn = await Deno.connect({ port: 3500 });
+ conn.close();
+ `);
+ assertEquals(statusCode, 0);
+ },
+);
+
+Deno.test(
+ { permissions: { read: true, run: true, net: true } },
+ async function netListenUnrefAndRef() {
+ const p = execCode(`
+ async function main() {
+ const listener = Deno.listen({ port: 3500 });
+ listener.unref();
+ listener.ref(); // This restores 'ref' state of listener
+ await listener.accept();
+ console.log("accepted")
+ }
+ main();
+ `);
+ // TODO(kt3k): This is racy. Find a correct way to
+ // wait for the server to be ready
+ setTimeout(async () => {
+ const conn = await Deno.connect({ port: 3500 });
+ conn.close();
+ }, 200);
+ const [statusCode, output] = await p;
+ assertEquals(statusCode, 0);
+ assertEquals(output.trim(), "accepted");
+ },
+);
+
+Deno.test(
+ { permissions: { net: true } },
+ async function netListenUnrefConcurrentAccept() {
+ const timer = setTimeout(() => {}, 1000);
+ const listener = Deno.listen({ port: 3500 });
+ listener.accept().catch(() => {});
+ listener.unref();
+ // Unref'd listener still causes Busy error
+ // on concurrent accept calls.
+ await assertRejects(async () => {
+ await listener.accept(); // The program exits here
+ }, Deno.errors.Busy);
+ listener.close();
+ clearTimeout(timer);
+ },
+);
diff --git a/cli/tests/unit/test_util.ts b/cli/tests/unit/test_util.ts
index b18ad9550..36ef8c0e7 100644
--- a/cli/tests/unit/test_util.ts
+++ b/cli/tests/unit/test_util.ts
@@ -30,7 +30,7 @@ export function pathToAbsoluteFileUrl(path: string): URL {
const decoder = new TextDecoder();
-export async function execCode(code: string) {
+export async function execCode(code: string): Promise<[number, string]> {
const p = Deno.run({
cmd: [
Deno.execPath(),