summaryrefslogtreecommitdiff
path: root/cli/tests/unit/timers_test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tests/unit/timers_test.ts')
-rw-r--r--cli/tests/unit/timers_test.ts120
1 files changed, 120 insertions, 0 deletions
diff --git a/cli/tests/unit/timers_test.ts b/cli/tests/unit/timers_test.ts
index fc6f9af4d..ec9b6f757 100644
--- a/cli/tests/unit/timers_test.ts
+++ b/cli/tests/unit/timers_test.ts
@@ -9,6 +9,8 @@ import {
unreachable,
} from "./test_util.ts";
+const decoder = new TextDecoder();
+
Deno.test(async function functionParameterBindingSuccess() {
const promise = deferred();
let count = 0;
@@ -573,3 +575,121 @@ Deno.test(
await p;
},
);
+
+async function execCode(code: string) {
+ const p = Deno.run({
+ cmd: [
+ Deno.execPath(),
+ "eval",
+ "--unstable",
+ code,
+ ],
+ stdout: "piped",
+ });
+ const [status, output] = await Promise.all([p.status(), p.output()]);
+ p.close();
+ return [status.code, decoder.decode(output)];
+}
+
+Deno.test({
+ name: "unrefTimer",
+ permissions: { run: true },
+ fn: async () => {
+ const [statusCode, output] = await execCode(`
+ const timer = setTimeout(() => console.log("1"));
+ Deno.unrefTimer(timer);
+ `);
+ assertEquals(statusCode, 0);
+ assertEquals(output, "");
+ },
+});
+
+Deno.test({
+ name: "unrefTimer - mix ref and unref 1",
+ permissions: { run: true },
+ fn: async () => {
+ const [statusCode, output] = await execCode(`
+ const timer1 = setTimeout(() => console.log("1"), 200);
+ const timer2 = setTimeout(() => console.log("2"), 400);
+ const timer3 = setTimeout(() => console.log("3"), 600);
+ Deno.unrefTimer(timer3);
+ `);
+ assertEquals(statusCode, 0);
+ assertEquals(output, "1\n2\n");
+ },
+});
+
+Deno.test({
+ name: "unrefTimer - mix ref and unref 2",
+ permissions: { run: true },
+ fn: async () => {
+ const [statusCode, output] = await execCode(`
+ const timer1 = setTimeout(() => console.log("1"), 200);
+ const timer2 = setTimeout(() => console.log("2"), 400);
+ const timer3 = setTimeout(() => console.log("3"), 600);
+ Deno.unrefTimer(timer1);
+ Deno.unrefTimer(timer2);
+ `);
+ assertEquals(statusCode, 0);
+ assertEquals(output, "1\n2\n3\n");
+ },
+});
+
+Deno.test({
+ name: "unrefTimer - unref interval",
+ permissions: { run: true },
+ fn: async () => {
+ const [statusCode, output] = await execCode(`
+ let i = 0;
+ const timer1 = setInterval(() => {
+ console.log("1");
+ i++;
+ if (i === 5) {
+ Deno.unrefTimer(timer1);
+ }
+ }, 10);
+ `);
+ assertEquals(statusCode, 0);
+ assertEquals(output, "1\n1\n1\n1\n1\n");
+ },
+});
+
+Deno.test({
+ name: "unrefTimer - unref then ref 1",
+ permissions: { run: true },
+ fn: async () => {
+ const [statusCode, output] = await execCode(`
+ const timer1 = setTimeout(() => console.log("1"), 10);
+ Deno.unrefTimer(timer1);
+ Deno.refTimer(timer1);
+ `);
+ assertEquals(statusCode, 0);
+ assertEquals(output, "1\n");
+ },
+});
+
+Deno.test({
+ name: "unrefTimer - unref then ref",
+ permissions: { run: true },
+ fn: async () => {
+ const [statusCode, output] = await execCode(`
+ const timer1 = setTimeout(() => {
+ console.log("1");
+ Deno.refTimer(timer2);
+ }, 10);
+ const timer2 = setTimeout(() => console.log("2"), 20);
+ Deno.unrefTimer(timer2);
+ `);
+ assertEquals(statusCode, 0);
+ assertEquals(output, "1\n2\n");
+ },
+});
+
+Deno.test({
+ name: "unrefTimer - invalid calls do nothing",
+ permissions: { run: true },
+ fn: () => {
+ Deno.unrefTimer(NaN);
+ Deno.refTimer(NaN);
+ },
+});