diff options
Diffstat (limited to 'cli/tests/unit/net_test.ts')
-rw-r--r-- | cli/tests/unit/net_test.ts | 77 |
1 files changed, 77 insertions, 0 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); + }, +); |