2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-29 05:34:37 -04:00
|
|
|
|
2024-02-12 21:05:10 -05:00
|
|
|
import { assert, fail } from "@std/assert/mod.ts";
|
2023-03-29 05:34:37 -04:00
|
|
|
import * as timers from "node:timers";
|
|
|
|
import * as timersPromises from "node:timers/promises";
|
|
|
|
|
|
|
|
Deno.test("[node/timers setTimeout]", () => {
|
|
|
|
{
|
|
|
|
const { clearTimeout, setTimeout } = timers;
|
|
|
|
const id = setTimeout(() => {});
|
|
|
|
clearTimeout(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const id = timers.setTimeout(() => {});
|
|
|
|
timers.clearTimeout(id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[node/timers setInterval]", () => {
|
|
|
|
{
|
|
|
|
const { clearInterval, setInterval } = timers;
|
|
|
|
const id = setInterval(() => {});
|
|
|
|
clearInterval(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const id = timers.setInterval(() => {});
|
|
|
|
timers.clearInterval(id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-20 03:52:50 -04:00
|
|
|
Deno.test("[node/timers setImmediate]", async () => {
|
2023-03-29 05:34:37 -04:00
|
|
|
{
|
|
|
|
const { clearImmediate, setImmediate } = timers;
|
2024-03-08 18:58:43 -05:00
|
|
|
const imm = setImmediate(() => {});
|
|
|
|
clearImmediate(imm);
|
2023-03-29 05:34:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-03-08 18:58:43 -05:00
|
|
|
const imm = timers.setImmediate(() => {});
|
|
|
|
timers.clearImmediate(imm);
|
2023-03-29 05:34:37 -04:00
|
|
|
}
|
2024-03-20 03:52:50 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
const deffered = Promise.withResolvers<void>();
|
|
|
|
const imm = timers.setImmediate(
|
|
|
|
(a, b) => {
|
|
|
|
assert(a === 1);
|
|
|
|
assert(b === 2);
|
|
|
|
deffered.resolve();
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
);
|
|
|
|
await deffered;
|
|
|
|
timers.clearImmediate(imm);
|
|
|
|
}
|
2023-03-29 05:34:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[node/timers/promises setTimeout]", () => {
|
|
|
|
const { setTimeout } = timersPromises;
|
|
|
|
const p = setTimeout(0);
|
|
|
|
|
|
|
|
assert(p instanceof Promise);
|
|
|
|
return p;
|
|
|
|
});
|
2023-07-02 15:11:34 -04:00
|
|
|
|
|
|
|
// Regression test for https://github.com/denoland/deno/issues/17981
|
|
|
|
Deno.test("[node/timers refresh cancelled timer]", () => {
|
|
|
|
const { setTimeout, clearTimeout } = timers;
|
|
|
|
const p = setTimeout(() => {
|
|
|
|
fail();
|
|
|
|
}, 1);
|
|
|
|
clearTimeout(p);
|
|
|
|
p.refresh();
|
|
|
|
});
|
2024-03-08 18:58:43 -05:00
|
|
|
|
|
|
|
Deno.test("[node/timers setImmediate returns Immediate object]", () => {
|
|
|
|
const { clearImmediate, setImmediate } = timers;
|
|
|
|
|
|
|
|
const imm = setImmediate(() => {});
|
|
|
|
imm.unref();
|
|
|
|
imm.ref();
|
|
|
|
imm.hasRef();
|
|
|
|
clearImmediate(imm);
|
|
|
|
});
|