mirror of
https://github.com/denoland/deno.git
synced 2024-12-11 10:07:54 -05:00
1e2581e57b
This PR adds the experimental `scheduler` APIs in Node's `timers/promises` module. See https://nodejs.org/api/timers.html#timerspromisesschedulerwaitdelay-options Fixes https://github.com/denoland/deno/issues/24800
110 lines
2.4 KiB
TypeScript
110 lines
2.4 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { assert, fail } from "@std/assert";
|
|
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);
|
|
}
|
|
});
|
|
|
|
Deno.test("[node/timers setImmediate]", async () => {
|
|
{
|
|
const { clearImmediate, setImmediate } = timers;
|
|
const imm = setImmediate(() => {});
|
|
clearImmediate(imm);
|
|
}
|
|
|
|
{
|
|
const imm = timers.setImmediate(() => {});
|
|
timers.clearImmediate(imm);
|
|
}
|
|
|
|
{
|
|
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);
|
|
}
|
|
});
|
|
|
|
Deno.test("[node/timers/promises setTimeout]", () => {
|
|
const { setTimeout } = timersPromises;
|
|
const p = setTimeout(0);
|
|
|
|
assert(p instanceof Promise);
|
|
return p;
|
|
});
|
|
|
|
Deno.test("[node/timers/promises scheduler.wait]", async () => {
|
|
const { scheduler } = timersPromises;
|
|
let resolved = false;
|
|
timers.setTimeout(() => (resolved = true), 20);
|
|
const p = scheduler.wait(20);
|
|
|
|
assert(p instanceof Promise);
|
|
await p;
|
|
assert(resolved);
|
|
});
|
|
|
|
Deno.test("[node/timers/promises scheduler.yield]", async () => {
|
|
const { scheduler } = timersPromises;
|
|
let resolved = false;
|
|
timers.setImmediate(() => resolved = true);
|
|
|
|
const p = scheduler.yield();
|
|
assert(p instanceof Promise);
|
|
await p;
|
|
|
|
assert(resolved);
|
|
});
|
|
|
|
// 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();
|
|
});
|
|
|
|
Deno.test("[node/timers setImmediate returns Immediate object]", () => {
|
|
const { clearImmediate, setImmediate } = timers;
|
|
|
|
const imm = setImmediate(() => {});
|
|
imm.unref();
|
|
imm.ref();
|
|
imm.hasRef();
|
|
clearImmediate(imm);
|
|
});
|